Esempio n. 1
0
 private ProcessingContext(ProcessingContext other)
 {
     Provider          = other.Provider;
     Storage           = other.Storage;
     CancellationToken = other.CancellationToken;
 }
 protected virtual CancellationToken GetTokenToWaitOn(ProcessingContext context)
 {
     return(context.CancellationToken);
 }
        private async Task <bool> Step(ProcessingContext context)
        {
            var fetched = default(IFetchedJob);

            using (var scope = _provider.CreateScope())
            {
                var provider   = scope.ServiceProvider;
                var connection = provider.GetRequiredService <IStorageConnection>();

                if ((fetched = await connection.FetchNextJobAsync()) != null)
                {
                    using (fetched)
                        using (var scopedContext = context.CreateScope())
                        {
                            var job = await connection.GetJobAsync(fetched.JobId);

                            var invocationData = Helper.FromJson <InvocationData>(job.Data);
                            var method         = invocationData.Deserialize();
                            var factory        = scopedContext.Provider.GetService <IJobFactory>();

                            var instance = default(object);
                            if (!method.Method.IsStatic)
                            {
                                instance = factory.Create(method.Type);
                            }

                            try
                            {
                                var sp = Stopwatch.StartNew();
                                await _stateChanger.ChangeStateAsync(job, new ProcessingState(), connection);

                                if (job.Retries > 0)
                                {
                                    _logger.JobRetrying(job.Retries);
                                }

                                var result = await ExecuteJob(method, instance);

                                sp.Stop();

                                var newState = default(IState);
                                if (!result.Succeeded)
                                {
                                    var shouldRetry = await UpdateJobForRetryAsync(instance, job, connection);

                                    if (shouldRetry)
                                    {
                                        newState = new ScheduledState();
                                        _logger.JobFailedWillRetry(result.Exception);
                                    }
                                    else
                                    {
                                        newState = new FailedState();
                                        _logger.JobFailed(result.Exception);
                                    }
                                }
                                else
                                {
                                    newState = new SucceededState();
                                }

                                await _stateChanger.ChangeStateAsync(job, newState, connection);

                                fetched.RemoveFromQueue();
                                if (result.Succeeded)
                                {
                                    _logger.JobExecuted(sp.Elapsed.TotalSeconds);
                                }
                            }
                            catch (JobLoadException ex)
                            {
                                _logger.JobCouldNotBeLoaded(job.Id, ex);

                                await _stateChanger.ChangeStateAsync(job, new FailedState(), connection);

                                fetched.RemoveFromQueue();
                            }
                            catch (Exception ex)
                            {
                                _logger.ExceptionOccuredWhileExecutingJob(job.Id, ex);

                                fetched.Requeue();
                            }
                        }
                }
            }
            return(fetched != null);
        }
 public Task ProcessAsync(ProcessingContext context)
 {
     throw new OperationCanceledException();
 }