Skip to content

Hangfire :: Background Jobs

Sandesh Kota edited this page Jul 15, 2018 · 8 revisions

Types:

  • Fire-and-forget jobs
    • Fire-and-forget jobs are executed only once and almost immediately after creation.
    var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget!"));
    
    • steps:
      • Serialize method and parameters
      • Create new background job based on the serialized information
      • Save to Job Storage and Queue
      • Return to Caller
      • Hangfire server picks up Job and processes
      • Job status updated
  • Delayed jobs
    • Delayed jobs are executed only once too, but not immediately, after a certain time interval.
    var jobId = BackgroundJob.Schedule( () => Console.WriteLine("Delayed!"), TimeSpan.FromDays(7));
    
  • Recurring jobs
    • Recurring jobs fire many times on the specified CRON schedule.
    RecurringJob.AddOrUpdate( () => Console.WriteLine("Recurring!"), Cron.Daily);
    
  • Continuations
    • Continuations are executed when its parent job has been finished.
    BackgroundJob.ContinueWith( jobId, () => Console.WriteLine("Continuation!"));
    
  • Batches Pro
    • Batch is a group of background jobs that is created atomically and considered as a single entity.
    var batchId = BatchJob.StartNew(x =>
    {
        x.Enqueue(() => Console.WriteLine("Job 1"));
        x.Enqueue(() => Console.WriteLine("Job 2"));
    });
    
  • Batch Continuations Pro
    • Batch continuation is fired when all background jobs in a parent batch finished.
    BatchJob.ContinueWith(batchId, x =>
    {
        x.Enqueue(() => Console.WriteLine("Last Job"));
    });
    

Clone this wiki locally