private bool OnJobError(JobExecutionContext context, Exception ex) { var errorHandler = Volatile.Read(ref JobError); if (errorHandler == null) { return(false); } var errorArgs = new JobErrorEventArgs(context, ex); errorHandler.Invoke(this, errorArgs); if (errorArgs.Rethrow) { return(false); } return(true); }
internal JobErrorEventArgs(JobExecutionContext context, Exception exception) { Context = context; Exception = exception; Rethrow = false; }
private void ExecuteJob(IJobFactory factory, IJobRunner runner, ScheduledJob job) { if (!OnJobStarting(job)) { return; } runner.Run(async() => { if (job.OverlapHandling == OverlapHandling.Skip && _activeJobs.Contains(job)) { return; } Lock(job); var activeJob = new ActiveJob(job, _cts.Token); _activeJobs.Add(activeJob); try { var context = new JobExecutionContext(this, activeJob.Token); OnJobStarted(context); try { if (!job.IsAnonymous) { using (var scope = factory.CreateScope()) { var instance = scope.Create(job.Type); await instance.ExecuteAsync(context); var supportsDispose = typeof(IDisposable).IsAssignableFrom(instance.GetType()); if (supportsDispose) { var disposable = (IDisposable)instance; disposable.Dispose(); } } } else { await job.Handler(context); } } catch (Exception ex) { if (OnJobError(context, ex)) { throw; } } OnJobFinished(job); } finally { _activeJobs.Remove(activeJob); Unlock(job); } }); }
private void OnJobStarted(JobExecutionContext context) { var startedHandler = Volatile.Read(ref JobStarted); startedHandler?.Invoke(this, new JobStartedEventArgs(context)); }
internal JobStartedEventArgs(JobExecutionContext context) { Context = context; }