/// <summary>
        /// Pushes the given <see cref="IBackgroundTask" /> that has been wrapped in an <see cref="BackgroundTaskEnvelope" />
        /// to a <see cref="IApiOperationExecutor" />.
        /// </summary>
        /// <param name="taskEnvelope">The task to be executed.</param>
        /// <param name="configureSpan">An action that will be called with an <see cref="IApmSpan" /> for the service-specific
        /// provider to add tags.</param>
        /// <param name="token">A cancellation token that indicates this method should be aborted.</param>
        /// <returns>A <see cref="Task" /> representing the execution of the given task.</returns>
        public async Task Execute(
            BackgroundTaskEnvelope taskEnvelope,
            Action <IApmSpan> configureSpan,
            CancellationToken token)
        {
            Guard.NotNull(nameof(taskEnvelope), taskEnvelope);

            using var nestedContainer = this._rootServiceProvider.CreateScope();

            var apiContext = new ApiOperationContext(
                nestedContainer.ServiceProvider,
                this._apiOperationExecutor.DataModel,
                taskEnvelope.Task,
                token);

            using var span = this._apmTool.StartOperation(
                      apiContext.Descriptor,
                      SpanKinds.Consumer,
                      taskEnvelope.ApmContext);

            configureSpan(span);

            apiContext.ApmSpan = span;

            var result = await this._apiOperationExecutor.ExecuteAsync(apiContext);

            if (result is UnhandledExceptionOperationResult unhandledExceptionOperationResult)
            {
                span.RecordException(unhandledExceptionOperationResult.Exception);

                unhandledExceptionOperationResult.Rethrow();
            }
        }
Exemple #2
0
        /// <summary>
        /// Pushes the given <see cref="IBackgroundTask" /> that has been wrapped in an <see cref="BackgroundTaskEnvelope" />
        /// to a <see cref="IApiOperationExecutor" />.
        /// </summary>
        /// <param name="taskEnvelope">The task to be executed.</param>
        /// <param name="configureActivity">An action that will be called with an <see cref="Activity" /> for the service-specific
        /// provider to add tags.</param>
        /// <param name="token">A cancellation token that indicates this method should be aborted.</param>
        /// <returns>A <see cref="Task" /> representing the execution of the given task.</returns>
        public async Task Execute(
            BackgroundTaskEnvelope taskEnvelope,
            Action <Activity> configureActivity,
            CancellationToken token)
        {
            Guard.NotNull(nameof(taskEnvelope), taskEnvelope);

            var parentContext = Propagator.Extract(default, taskEnvelope.Headers, ExtractTraceContextFromBasicProperties);
Exemple #3
0
        private async Task <string> TrackAsync(BackgroundTaskEnvelope task, Func <Task <string> > fn)
        {
            task.ApmContext = new Dictionary <string, string>();

            using var op = this._apmTool.Start(
                      SpanKinds.Producer,
                      "task.enqueue",
                      "queue");

            op.InjectContext(task.ApmContext);

            return(await fn());
        }
        /// <inheritdoc />
        public IScheduledBackgroundTask ContinueWith(IBackgroundTask backgroundTask, BackgroundTaskContinuationOptions options = BackgroundTaskContinuationOptions.OnlyOnSucceededState)
        {
            if (this._children == null)
            {
                this._children = new List <ChildScheduledBackgroundTask>();
            }

            // Copy over the metadata from this parent task, as we know this must have been executed in the
            // same context as this one.
            var backgroundTaskEnvelope = new BackgroundTaskEnvelope(backgroundTask)
            {
                Headers = this._taskEnvelope.Headers,
            };

            var scheduledBackgroundTask = new ChildScheduledBackgroundTask(backgroundTaskEnvelope, options, this._scheduler);

            this._children.Add(scheduledBackgroundTask);

            return(scheduledBackgroundTask);
        }
 public ScheduledBackgroundTask(BackgroundTaskEnvelope taskEnvelope, TimeSpan?delay, BackgroundTaskScheduler scheduler)
 {
     this._taskEnvelope = taskEnvelope;
     this._delay        = delay;
     this._scheduler    = scheduler;
 }
Exemple #6
0
 /// <summary>
 /// Initialises a new instance of the <see cref="HangfireBackgroundTaskWrapper" /> class.
 /// </summary>
 /// <param name="envelope">The task envelope to wrap.</param>
 public HangfireBackgroundTaskWrapper(BackgroundTaskEnvelope envelope)
 {
     this.Envelope = envelope;
 }
 public ChildScheduledBackgroundTask(BackgroundTaskEnvelope taskEnvelope, BackgroundTaskContinuationOptions option, BackgroundTaskScheduler scheduler)
 {
     this._taskEnvelope = taskEnvelope;
     this._option       = option;
     this._scheduler    = scheduler;
 }
Exemple #8
0
 /// <inheritdoc />
 /// <remarks>
 /// This method will set the request ID and baggage from the current ambient <see cref="Activity" /> on to the
 /// background task.
 /// </remarks>
 public Task <string> EnqueueChildAsync(BackgroundTaskEnvelope task, string parentId, BackgroundTaskContinuationOptions continuationOptions)
 {
     return(this.TrackAsync(task, () => this._innerProvider.EnqueueChildAsync(task, parentId, continuationOptions)));
 }
Exemple #9
0
 /// <inheritdoc />
 /// <remarks>
 /// This method will set the request ID and baggage from the current ambient <see cref="Activity" /> on to the
 /// background task.
 /// </remarks>
 public Task <string> ScheduleAsync(BackgroundTaskEnvelope task, TimeSpan delay)
 {
     return(this.TrackAsync(task, () => this._innerProvider.ScheduleAsync(task, delay)));
 }
Exemple #10
0
 /// <inheritdoc />
 /// <remarks>
 /// This method will set the request ID and baggage from the current ambient <see cref="Activity" /> on to the
 /// background task.
 /// </remarks>
 public Task <string> EnqueueAsync(BackgroundTaskEnvelope task)
 {
     return(this.TrackAsync(task, () => this._innerProvider.EnqueueAsync(task)));
 }