Beispiel #1
0
        private async Task <IState> PerformJob(BackgroundProcessContext context, IStorageConnection connection, string jobId)
        {
            try
            {
                var jobData = connection.GetJobData(jobId);
                if (jobData == null)
                {
                    // Job expired just after moving to a processing state. This is an
                    // unreal scenario, but shit happens. Returning null instead of throwing
                    // an exception and rescuing from en-queueing a poisoned jobId back
                    // to a queue.
                    return(null);
                }

                jobData.EnsureLoaded();

                var backgroundJob = new BackgroundJob(jobId, jobData.Job, jobData.CreatedAt);

                var jobToken       = new ServerJobCancellationToken(connection, jobId, context.ServerId, _workerId, context.CancellationToken);
                var performContext = new PerformContext(connection, backgroundJob, jobToken);

                var latency  = (DateTime.UtcNow - jobData.CreatedAt).TotalMilliseconds;
                var duration = Stopwatch.StartNew();

                var result = await _performer.PerformAsync(performContext).ConfigureAwait(false);

                duration.Stop();

                // SHOULD BE: return new SucceededState(result, (long)latency, duration.ElapsedMilliseconds);
                return(CreateSucceededState(result, (long)latency, duration.ElapsedMilliseconds));
            }
            catch (JobAbortedException)
            {
                // Background job performance was aborted due to a
                // state change, so it's idenfifier should be removed
                // from a queue.
                return(null);
            }
            catch (JobPerformanceException ex)
            {
                return(new FailedState(ex.InnerException)
                {
                    Reason = ex.Message
                });
            }
            catch (Exception ex)
            {
                if (ex is OperationCanceledException && context.IsShutdownRequested)
                {
                    throw;
                }

                return(new FailedState(ex)
                {
                    Reason = "An exception occurred during processing of a background job."
                });
            }
        }
            private Task PerformJobAsync()
            {
                const TaskContinuationOptions options = TaskContinuationOptions.NotOnCanceled | TaskContinuationOptions.ExecuteSynchronously;

                try
                {
                    return(_innerPerformer.PerformAsync(_context).ContinueWith(FillPerformedContext, this, options));
                }
                catch (Exception ex)
                {
                    // fill performedContext with exception if failed to create task at all
                    _performedContext = new PerformedContext(_context, null, false, ex);
                    return(Task.FromResult(0));
                }
            }