Search code examples
c#hangfire

How to make some methods depends on another method before run in hangfire?


I have this api endpoint method which uses Hangfire. How do I make sure that PreIngestion() is completed first before IngestA() and IngestB() could be executed?

[HttpGet]
[Route("IngestFiles")]
public IActionResult IngestFiles(string cronExpression = "0")
{
     RecurringJob.AddOrUpdate<IIngestService>(x => x.PreIngestion(), cronExpression, TimeZoneInfo.Local);

     RecurringJob.AddOrUpdate<IIngestService>(x => x.IngestA(), cronExpression, TimeZoneInfo.Local);
     RecurringJob.AddOrUpdate<IIngestService>(x => x.IngestB(), cronExpression, TimeZoneInfo.Local);

     return Ok();
}

I can do it with ContinueJobWith method but I need it to be scheduled.


Solution

  • Since QueueAttribute is not working for the recurring job, you can create a helper method to queue them up as Anton said.

    [HttpGet]
    [Route("IngestFiles")]
    public IActionResult IngestFiles(string cronExpression = "0")
    {
         RecurringJob.AddOrUpdate<IIngestService>(x => IngestHelper.Ingest(x), cronExpression, TimeZoneInfo.Local);
    
         return Ok();
    }
    

    RecurringJobHelper.cs

    public static class RecurringJobHelper
    {
        public static void Ingest(IIngestService ingestService)
        {
            ingestService.PreIngestion();
            ingestService.IngestA();
            ingestService.IngestB();
        }
    }