Нема описа

DownloadedLogSummariser.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Threading;
  3. namespace BulkPrinting
  4. {
  5. public class DownloadedLogSummariser : ThreadHelper
  6. {
  7. private const int _retryInterval = 30000;
  8. private DBHelper _db;
  9. private Action _refreshAction;
  10. public DownloadedLogSummariser(DBHelper db, Action refreshAction)
  11. {
  12. _db = db;
  13. _refreshAction = refreshAction;
  14. }
  15. protected override void Run()
  16. {
  17. bool isCancelled = false;
  18. while (!isCancelled)
  19. {
  20. bool doRefresh = false;
  21. while (!isCancelled)
  22. {
  23. lock (_db.WriteLock)
  24. {
  25. using (var trans = _db.BeginTransaction())
  26. {
  27. using (var selectCommand = _db.CreateCommand(
  28. "SELECT l.*, v.BatchId FROM Logs l " +
  29. "LEFT JOIN Voucher v ON l.VoucherId=v.Id " +
  30. "WHERE Summarised=0 " +
  31. "LIMIT 1000",
  32. trans))
  33. using (var updateLogsCommand = _db.CreateCommand(
  34. "UPDATE Logs SET Summarised=1 WHERE Id=@Id"))
  35. using (var updateVoucherCommand = Utility.CreateVoucherEventCountUpdateCommand(_db, trans))
  36. using (var updateBatchCommand = Utility.CreateBatchEventCountUpdateCommand(_db, trans))
  37. {
  38. updateLogsCommand.Parameters.AddWithValue("@Id", null);
  39. using (var reader = selectCommand.ExecuteReader())
  40. {
  41. var rowCount = 0;
  42. while (reader.Read())
  43. {
  44. rowCount++;
  45. doRefresh = true;
  46. int printCountDelta, reprintCountDelta, exportCountDelta, reExportCountDelta;
  47. if ((reader["VoucherId"] != null) &&
  48. Utility.CalculateEventCountDeltas(
  49. (VendorEvent.VendorEventType)int.Parse((string)reader["EventType"]),
  50. (bool)reader["Retry"],
  51. out printCountDelta,
  52. out reprintCountDelta,
  53. out exportCountDelta,
  54. out reExportCountDelta))
  55. {
  56. Utility.ExecuteEventCountUpdateQuery(updateVoucherCommand, (int)reader["VoucherId"],
  57. printCountDelta, reprintCountDelta, exportCountDelta, reExportCountDelta);
  58. if (reader["BatchId"] != null)
  59. {
  60. Utility.ExecuteEventCountUpdateQuery(updateBatchCommand, (int)reader["BatchId"],
  61. printCountDelta, reprintCountDelta, exportCountDelta, reExportCountDelta);
  62. }
  63. }
  64. var logId = (long)reader["Id"];
  65. updateLogsCommand.Parameters["@Id"].Value = (int)logId;
  66. updateLogsCommand.ExecuteNonQuery();
  67. }
  68. if (rowCount == 0)
  69. {
  70. break;
  71. }
  72. }
  73. }
  74. trans.Commit();
  75. }
  76. }
  77. lock (_lock)
  78. {
  79. isCancelled = _cancelled;
  80. }
  81. }
  82. if (! isCancelled && doRefresh)
  83. {
  84. doRefresh = false;
  85. _refreshAction();
  86. }
  87. // We sleep and try again on the offchance that we got triggered too early.
  88. lock (_lock)
  89. {
  90. if (!_cancelled)
  91. {
  92. Monitor.Wait(_lock, _retryInterval);
  93. }
  94. isCancelled = _cancelled;
  95. }
  96. }
  97. }
  98. }
  99. }