Skip to content

Volo.Abp.BackgroundWorkers.Hangfire HangfireBackgroundWorkerManager RecurringJob registing bug #24661

@Nine4Cool

Description

@Nine4Cool

Is there an existing issue for this?

  • I have searched the existing issues

Description

In HangfireBackgroundWorkerManager , when using RecurringJob.AddOrUpdate to registe a periodicity job, cann't use with queuename.

Additionally, I suggest that RecurringJobId and CronExpression should be set as abstract in abstract class HangfireBackgroundWorkerBase; When we inherit HangfireContextWorkerBase, we need to implement RecurringJobId and CronExpression;
In addition, we also need to consider the situation of task timeout.

This is my adjustment.

public abstract class HangfireBackgroundWorkerBase : BackgroundWorkerBase, IHangfireBackgroundWorker
{
        private readonly IServiceScopeFactory _serviceScopeFactory;
        public abstract string? RecurringJobId { get; set; }
        public abstract string CronExpression { get; set; }
        public TimeZoneInfo? TimeZone { get; set; } = TimeZoneInfo.Utc;
        public string Queue { get; set; }
        protected virtual TimeSpan Timeout { get; } = TimeSpan.FromMinutes(5);

        public HangfireBackgroundWorkerBase(IServiceScopeFactory serviceScopeFactory)
        {
            _serviceScopeFactory = serviceScopeFactory;
        }

        public virtual async Task DoWorkAsync(CancellationToken cancellationToken = default)
        {
            using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            cts.CancelAfter(Timeout);

            using var scope = _serviceScopeFactory.CreateScope();
            try
            {
                await DoWorkAsync(scope.ServiceProvider, cts.Token);
            }
            catch (OperationCanceledException) when (cts.Token.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
            {
                var logger = scope.ServiceProvider.GetRequiredService<ILogger<HangfireBackgroundWorkerBase>>();
                logger.LogWarning("[TIMEOUT] Task '{JobName}' exceeded timeout of {Timeout}.", GetType().Name, Timeout);
                throw;
            }
        }
        protected abstract Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken);
}

Reproduction Steps

No response

Expected behavior

No response

Actual behavior

No response

Regression?

No response

Known Workarounds

No response

Version

10.0.2

User Interface

Common (Default)

Database Provider

EF Core (Default)

Tiered or separate authentication server

None (Default)

Operation System

Windows (Default)

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions