|
|
@@ -536,7 +536,8 @@ namespace BulkPrinting
|
|
536
|
536
|
if (Globals.LogUploader != null)
|
|
537
|
537
|
Globals.LogUploader.Cancel();
|
|
538
|
538
|
|
|
539
|
|
- CancelLogDownloadWorker();
|
|
|
539
|
+ if (Globals.LogDownloader != null)
|
|
|
540
|
+ Globals.LogDownloader.Cancel();
|
|
540
|
541
|
|
|
541
|
542
|
if (Globals.LogUploader != null)
|
|
542
|
543
|
{
|
|
|
@@ -544,14 +545,12 @@ namespace BulkPrinting
|
|
544
|
545
|
Globals.LogUploader = null;
|
|
545
|
546
|
}
|
|
546
|
547
|
|
|
547
|
|
- if (Globals.LogDownloadThread != null)
|
|
|
548
|
+ if (Globals.LogDownloader != null)
|
|
548
|
549
|
{
|
|
549
|
|
- Globals.LogDownloadThread.Join();
|
|
550
|
|
- Globals.LogDownloadThread = null;
|
|
|
550
|
+ Globals.LogDownloader.Join();
|
|
|
551
|
+ Globals.LogDownloader = null;
|
|
551
|
552
|
}
|
|
552
|
553
|
|
|
553
|
|
- Globals.LogDownloadThreadCancelled = false;
|
|
554
|
|
-
|
|
555
|
554
|
Globals.DB.Close();
|
|
556
|
555
|
Globals.DB.Dispose();
|
|
557
|
556
|
Globals.DB = null;
|
|
|
@@ -614,99 +613,12 @@ namespace BulkPrinting
|
|
614
|
613
|
}
|
|
615
|
614
|
}
|
|
616
|
615
|
|
|
617
|
|
- public static void CancelLogDownloadWorker()
|
|
618
|
|
- {
|
|
619
|
|
- lock (Globals.LogDownloadThreadLock)
|
|
620
|
|
- {
|
|
621
|
|
- Globals.LogDownloadThreadCancelled = true;
|
|
622
|
|
- Monitor.Pulse(Globals.LogDownloadThreadLock);
|
|
623
|
|
- }
|
|
624
|
|
- }
|
|
625
|
|
-
|
|
626
|
|
- public static void LogDownloadWorker()
|
|
627
|
|
- {
|
|
628
|
|
- const int vendorEventPageSize = 1000;
|
|
629
|
|
- const int retryInterval = 30000;
|
|
630
|
|
-
|
|
631
|
|
- var db = Globals.DB;
|
|
632
|
|
- //using (var db = OpenDBConnection())
|
|
633
|
|
- {
|
|
634
|
|
- bool cancelled = false;
|
|
635
|
|
- int maxRemoteId = GetSavedParameterAsInt(db, "SyncBackwardsFromLogId");
|
|
636
|
|
- while (!cancelled && (maxRemoteId > 0))
|
|
637
|
|
- {
|
|
638
|
|
- var EventPage = new Page<VendorEvent>();
|
|
639
|
|
- if (RESTRequest(ref EventPage, String.Format("/api/vendorevents/?maxRemoteId={0}&pageSize={1}", maxRemoteId, vendorEventPageSize)))
|
|
640
|
|
- {
|
|
641
|
|
- lock (db.WriteLock)
|
|
642
|
|
- {
|
|
643
|
|
- using (var trans = db.BeginTransaction())
|
|
644
|
|
- {
|
|
645
|
|
- using (var insertCommand = db.CreateCommand(
|
|
646
|
|
- "INSERT INTO Logs (Id, UserId, VoucherId, EventDate, EventType, Retry) " +
|
|
647
|
|
- "VALUES (@id, @userid, @voucherid, @eventdate, @eventtype, @retry)",
|
|
648
|
|
- trans))
|
|
649
|
|
- using (var updateCommand = db.CreateCommand(
|
|
650
|
|
- "UPDATE Parameters SET Value=@Value WHERE Key='SyncBackwardsFromLogId'", trans))
|
|
651
|
|
- {
|
|
652
|
|
- foreach (VendorEvent Event in EventPage.Items)
|
|
653
|
|
- {
|
|
654
|
|
- if (!Event.RemoteId.HasValue)
|
|
655
|
|
- {
|
|
656
|
|
- throw new Exception("Downloaded event with no remote ID");
|
|
657
|
|
- }
|
|
658
|
|
- else if (Event.RemoteId.Value > maxRemoteId)
|
|
659
|
|
- {
|
|
660
|
|
- throw new Exception("Downloaded event remote ID was not in descending order");
|
|
661
|
|
- }
|
|
662
|
|
-
|
|
663
|
|
- insertCommand.Parameters.Clear();
|
|
664
|
|
- insertCommand.Parameters.AddWithValue("@id", Event.RemoteId);
|
|
665
|
|
- insertCommand.Parameters.AddWithValue("@userid", Event.UserId);
|
|
666
|
|
- insertCommand.Parameters.AddWithValue("@voucherid", Event.VoucherId);
|
|
667
|
|
- insertCommand.Parameters.AddWithValue("@eventdate", Event.EventDate.UtcDateTime);
|
|
668
|
|
- insertCommand.Parameters.AddWithValue("@eventtype", Event.EventType);
|
|
669
|
|
- insertCommand.Parameters.AddWithValue("@retry", Event.Retry);
|
|
670
|
|
- insertCommand.ExecuteNonQuery();
|
|
671
|
|
-
|
|
672
|
|
- maxRemoteId = Event.RemoteId.Value - 1;
|
|
673
|
|
- }
|
|
674
|
|
-
|
|
675
|
|
- updateCommand.Parameters.Clear();
|
|
676
|
|
- updateCommand.Parameters.AddWithValue("@Value", maxRemoteId.ToString());
|
|
677
|
|
- updateCommand.ExecuteNonQuery();
|
|
678
|
|
- }
|
|
679
|
|
-
|
|
680
|
|
- trans.Commit();
|
|
681
|
|
- }
|
|
682
|
|
- }
|
|
683
|
|
-
|
|
684
|
|
- lock (Globals.LogDownloadThreadLock)
|
|
685
|
|
- {
|
|
686
|
|
- cancelled = Globals.LogDownloadThreadCancelled;
|
|
687
|
|
- }
|
|
688
|
|
- }
|
|
689
|
|
- else
|
|
690
|
|
- {
|
|
691
|
|
- lock (Globals.LogDownloadThreadLock)
|
|
692
|
|
- {
|
|
693
|
|
- if (!Globals.LogDownloadThreadCancelled)
|
|
694
|
|
- {
|
|
695
|
|
- Monitor.Wait(Globals.LogDownloadThreadLock, retryInterval);
|
|
696
|
|
- }
|
|
697
|
|
- cancelled = Globals.LogDownloadThreadCancelled;
|
|
698
|
|
- }
|
|
699
|
|
- }
|
|
700
|
|
- }
|
|
701
|
|
- }
|
|
702
|
|
- }
|
|
703
|
|
-
|
|
704
|
616
|
public static void TriggerLogDownload()
|
|
705
|
617
|
{
|
|
706
|
|
- if (Globals.LogDownloadThread == null)
|
|
|
618
|
+ if (Globals.LogDownloader == null)
|
|
707
|
619
|
{
|
|
708
|
|
- Globals.LogDownloadThread = new Thread(() => LogDownloadWorker());
|
|
709
|
|
- Globals.LogDownloadThread.Start();
|
|
|
620
|
+ Globals.LogDownloader = new LogDownloader(Globals.DB);
|
|
|
621
|
+ Globals.LogDownloader.Start();
|
|
710
|
622
|
}
|
|
711
|
623
|
}
|
|
712
|
624
|
|