public async Task Execute(HangfireBackgroundTaskWrapper task, PerformContext context, CancellationToken token)
        {
            Guard.NotNull(nameof(task), task);

            var attempt = context.GetJobParameter <int?>("RetryCount");

            try
            {
                await this._taskExecutor.Execute(
                    task.Envelope,
                    s =>
                {
                    s.SetTag("hangfire.jobId", context.BackgroundJob.Id);
                    s.SetTag("hangfire.retryAttempt", (attempt ?? 1).ToString());
                },
                    token);
            }
            catch (Exception e)
            {
                if (this._errorLogger.ShouldIgnore(e))
                {
                    return;
                }

                throw;
            }
        }
Exemplo n.º 2
0
        public void When_No_Properties_Enqueued_Then_Sets_DisplayName()
        {
            // Act
            var displayName = new HangfireBackgroundTaskWrapper(new BackgroundTaskEnvelope(new NoPropertiesTask())).ToString();

            // Assert
            displayName.Should().Be("NoPropertiesTask()");
        }
Exemplo n.º 3
0
        public void When_Properties_Enqueued_Then_Sets_DisplayName()
        {
            // Act
            var displayName = new HangfireBackgroundTaskWrapper(new BackgroundTaskEnvelope(new PropertiesTask
            {
                Prop1 = "A prop"
            })).ToString();

            // Assert
            displayName.Should().Be("PropertiesTask(\"Prop1\":\"A prop\")");
        }
Exemplo n.º 4
0
        public async Task Execute(HangfireBackgroundTaskWrapper task, PerformContext context, CancellationToken token)
        {
            Guard.NotNull(nameof(task), task);

            var attempt = context.GetJobParameter <int?>("RetryCount");

            await this._taskExecutor.Execute(
                task.Envelope,
                s =>
            {
                s.SetTag("messaging.system", "hangfire");
                s.SetTag("messaging.destination_kind", "queue");
                s.SetTag("messaging.message_id", context.BackgroundJob.Id);

                s.SetTag("hangfire.retryAttempt", (attempt ?? 1).ToString());
            },
                token);
        }
Exemplo n.º 5
0
        private string CreateCore(BackgroundTaskEnvelope task, Func <string, IState> createState)
        {
            var queue = GetQueueForTask(task.Task);

            if (this._logger.IsEnabled(LogLevel.Debug))
            {
                this._logger.LogDebug("Enqueuing task {TaskType} to {Queue}", task.GetType().Name, queue);
            }

            var hangfireBackgroundTaskEnvelope = new HangfireBackgroundTaskWrapper(task);

            task.Headers = new Dictionary <string, string>();

            using var activity = TaskExecutor.ActivitySource.StartActivity($"{hangfireBackgroundTaskEnvelope.Envelope.Task.GetType()} send", ActivityKind.Producer);

            ActivityContext contextToInject = default;

            if (activity != null)
            {
                contextToInject = activity.Context;
            }
            else if (Activity.Current != null)
            {
                contextToInject = Activity.Current.Context;
            }

            TaskExecutor.Propagator.Inject(
                new PropagationContext(contextToInject, Baggage.Current),
                task.Headers,
                (c, k, v) => c[k] = v);

            activity?.SetTag("messaging.system", "hangfire");
            activity?.SetTag("messaging.destination", queue);
            activity?.SetTag("messaging.destination_kind", "queue");

            var id = this._jobClient.Create(
                Job.FromExpression <HangfireTaskExecutor>(e => e.Execute(
                                                              hangfireBackgroundTaskEnvelope,
                                                              null,
                                                              CancellationToken.None)),
                createState(queue));

            return(id);
        }