private async Task <OrdersSynchronizationPushNotification> Enqueue(OrdersSynchronizationRequest request) { var notification = new OrdersSynchronizationPushNotification(_userNameResolver.GetCurrentUserName()) { Title = "Sending orders to AvaTax", Description = "Starting process..." }; await _pushNotificationManager.SendAsync(notification); var jobId = BackgroundJob.Enqueue <OrdersSynchronizationJob>(x => x.RunManually(request.OrderIds, notification, JobCancellationToken.Null, null)); notification.JobId = jobId; return(notification); }
public async Task RunManually(string[] orderIds, OrdersSynchronizationPushNotification notification, IJobCancellationToken cancellationToken, PerformContext context) { var ordersFeed = new InMemoryIndexDocumentChangeFeed(orderIds, IndexDocumentChangeType.Modified, BatchSize); void ProgressCallback(AvaTaxOrdersSynchronizationProgress x) { notification.Description = x.Message; notification.Errors = x.Errors; notification.ErrorCount = notification.Errors.Count; notification.TotalCount = x.TotalCount ?? 0; notification.ProcessedCount = x.ProcessedCount ?? 0; notification.JobId = context.BackgroundJob.Id; _pushNotificationManager.Send(notification); } try { await PerformOrderSynchronization(ordersFeed, ProgressCallback, cancellationToken); } catch (JobAbortedException) { //do nothing } catch (Exception ex) { notification.ErrorCount++; notification.Errors.Add(ex.ToString()); } finally { notification.Finished = DateTime.UtcNow; notification.Description = "Process finished " + (notification.Errors.Any() ? "with errors" : "successfully"); _pushNotificationManager.Send(notification); } }