Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Help on Understanding Hangfire Background jobs #6383


User avatar
0
ajayak created

I have added a Hangfire service class which initialize all hangfire jobs in my application.

public class HangfireService
{
    public static void InitializeJobs()
    {
        RecurringJob.AddOrUpdate<DelayedNotificationWorker>(job => job.Start(), Cron.HourInterval(6));
    }
}

Here is DelayedNotificationWorker class:

public class DelayedNotificationWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
{
    private readonly IAppNotifier _appNotifier;
    private readonly UserManager _userManager;
    private readonly IRepository<DelayedNotification, long> _delayedNotificationRepository;

    public DelayedNotificationWorker(
        AbpTimer timer,
        IRepository<DelayedNotification, long> delayedNotificationRepository) : base(timer)
    {
        Timer.Period = 1000 * 60 * 60 * 6; // 6 Hours
        _delayedNotificationRepository = delayedNotificationRepository;
    }

    [UnitOfWork]
    protected override void DoWork()
    {
        // Do some job
    }
}

What is the effect of application restart on the background job? Will it be triggered immediately on restart or wait for 6 hour interval? What is the role of Timer.Period in this class?


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ajayak

    As far as I can see, this part doesn't use Hangfire, this is the BackgroundWorker provided by ABP.

    If so, DelayedNotificationWorker will wait 6 hours after your website restarts. If you want it to run immediately after the restart, you can set Timer.RunOnStart = true; in the constructor of DelayedNotificationWorker.