Ejemplo n.º 1
0
        public bool Add(ActiveJob job)
        {
            _locker.EnterWriteLock();

            try
            {
                return(_jobs.Add(job));
            }
            finally
            {
                _locker.ExitWriteLock();
            }
        }
Ejemplo n.º 2
0
        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);
                }
            });
        }