protected virtual void MessageReceived(object sender, BasicDeliverEventArgs ea) { using (var scope = ServiceScopeFactory.CreateScope()) { var context = new JobExecutionContext( scope.ServiceProvider, JobConfiguration.JobType, Serializer.Deserialize(ea.Body, typeof(TArgs)) ); try { AsyncHelper.RunSync(() => JobExecuter.ExecuteAsync(context)); ChannelAccessor.Channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); } catch (BackgroundJobExecutionException) { //TODO: Reject like that? ChannelAccessor.Channel.BasicReject(deliveryTag: ea.DeliveryTag, requeue: true); } catch (Exception) { //TODO: Reject like that? ChannelAccessor.Channel.BasicReject(deliveryTag: ea.DeliveryTag, requeue: false); } } }
public async Task Execute(IJobExecutionContext context) { using (var scope = ServiceScopeFactory.CreateScope()) { var args = JsonSerializer.Deserialize <TArgs>(context.JobDetail.JobDataMap.GetString(nameof(TArgs))); var jobType = Options.GetJob(typeof(TArgs)).JobType; var jobContext = new JobExecutionContext(scope.ServiceProvider, jobType, args); try { await JobExecuter.ExecuteAsync(jobContext); } catch (Exception exception) { var jobExecutionException = new JobExecutionException(exception); var retryIndex = context.JobDetail.JobDataMap.GetString(QuartzBackgroundJobManager.JobDataPrefix + QuartzBackgroundJobManager.RetryIndex).To <int>(); retryIndex++; context.JobDetail.JobDataMap.Put(QuartzBackgroundJobManager.JobDataPrefix + QuartzBackgroundJobManager.RetryIndex, retryIndex.ToString()); await BackgroundJobQuartzOptions.RetryStrategy.Invoke(retryIndex, context, jobExecutionException); throw jobExecutionException; } } }