Base solution for your next web application
Open Closed

configure recurring job with Hangfire #6564


User avatar
0
azmat created

Hi,

I want to configure a recurring job through hangfire, i have already created my job folloiwng the approach mentioned in following link.

https://aspnetboilerplate.com/Pages/Documents/Background-Jobs-And-Workers#create-a-background-job

I can call it from application service using following code, _backgroundJobManager.Enqueue<MortgageStatusService, int>(1);

May i know how i can configure it at the start of application so that it executes after every 15 minutes?

public class MortgageStatusService : BackgroundJob<int>, ITransientDependency { private readonly IRepository<CarMortgage> _carMortgageRepository; private readonly IRepository<CarMortgageLog> _carMortgageLogRepository; private readonly IUnitOfWorkManager _unitOfWorkManager;

    public MortgageStatusService(
        IRepository&lt;CarMortgage&gt; carMortgageRepository,
        IRepository&lt;CarMortgageLog&gt; carMortgageLogRepository,
        IUnitOfWorkManager unitOfWorkManager)
    {
        _carMortgageRepository = carMortgageRepository;
        _carMortgageLogRepository = carMortgageLogRepository;
        _unitOfWorkManager = unitOfWorkManager;
    }

    [UnitOfWork]
    public override void Execute(int args)
    {
        SynchronizeData.Synchronize(_carMortgageRepository, _carMortgageLogRepository);
    }
}

4 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    You can configure it in module's PostInitialize.

  • User Avatar
    0
    azmat created

    that i figured out but i want to know the syntax to configure a job to run after every 15 minutes.

  • User Avatar
    0
    aaron created
    Support Team

    http://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html

  • User Avatar
    0
    azmat created

    I managed to schedule it in PostInitialize using following code.

    RecurringJob.AddOrUpdate&lt;MortgageStatusService>("Synchrnize-With-xxxxxxxx", x => x.Execute(1) , Cron.MinuteInterval(15));