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; } }
public void When_No_Properties_Enqueued_Then_Sets_DisplayName() { // Act var displayName = new HangfireBackgroundTaskWrapper(new BackgroundTaskEnvelope(new NoPropertiesTask())).ToString(); // Assert displayName.Should().Be("NoPropertiesTask()"); }
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\")"); }
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); }
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); }