public void Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; var rockContext = new RockContext(); var daysBack = dataMap.GetString("DaysBack").AsInteger(); if (daysBack == 0) { daysBack = 2; } int syncCount = 0; for (var i = 0; i < daysBack; i++) { TwilioDownloader twilioDownloader = new TwilioDownloader(); syncCount += twilioDownloader.SyncItems(RockDateTime.Today.AddDays(i * -1)); rockContext.SaveChanges(); context.Result = string.Format($"Synced {syncCount} text messages between {RockDateTime.Today.AddDays( 1 - daysBack ).ToString( "M/d/yyy" )} and {RockDateTime.Today.ToString( "M/d/yyy" )}"); } }
/// <summary> /// Starts the refund process. /// </summary> private void Sync() { long totalMilliseconds = 0; long syncTotal = 0; var startDate = drpDateRange.DateRange.Start; var endDate = drpDateRange.DateRange.End; var importTask = new Task(() => { // wait a little so the browser can render and start listening to events System.Threading.Thread.Sleep(1000); _hubContext.Clients.All.showButtons(this.SignalRNotificationKey, false); Stopwatch stopwatch = Stopwatch.StartNew(); if (startDate != null || endDate != null && startDate.Value < endDate.Value) { var syncDate = startDate.Value; TwilioDownloader twilioDownloader = new TwilioDownloader(); while (syncDate <= endDate) { OnProgress("Syncing for " + syncDate.ToString("M/d/yyyy")); var syncCount = twilioDownloader.SyncItems(syncDate); results["Complete"] += string.Format("Synced {0} messages from {1}{2}", syncCount, syncDate.ToString("M/d/yyyy"), Environment.NewLine); syncDate = syncDate.AddDays(1); syncTotal += syncCount; } } stopwatch.Stop(); totalMilliseconds = stopwatch.ElapsedMilliseconds; _hubContext.Clients.All.showButtons(this.SignalRNotificationKey, true); }); importTask.ContinueWith((t) => { if (t.IsFaulted) { foreach (var exception in t.Exception.InnerExceptions) { LogException(exception); } OnProgress("ERROR: " + t.Exception.Message); } else { var time = TimeSpan.FromMilliseconds(totalMilliseconds); OnProgress(string.Format("Synced {0} messages between {1} and {2}. [{3}]", syncTotal, startDate.Value.ToString("M/d/yyyy"), endDate.Value.ToString("M/d/yyyy"), time.Humanize())); } }); importTask.Start(); }