Thursday, 20 June 2024

JOBS in C#

 Types of Jobs

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!"));

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(
    "myrecurringjob",
    () => Console.WriteLine("Recurring!"),
    Cron.Daily);

Continuations

Continuations are executed when its parent job has been finished.

BackgroundJob.ContinueJobWith(
    jobId,
    () => Console.WriteLine("Continuation!"));

Batches

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

Batch continuation is fired when all background jobs in a parent batch finished.

BatchJob.ContinueBatchWith(batchId, x =>
{
    x.Enqueue(() => Console.WriteLine("Last Job"));
});

No comments:

Post a Comment

7 Common mistakes in Dot Net — You can avoid

  There are many common mistakes made during .NET (ASP.NET, .NET Core) development, which affect performance, security, and code… Code Crack...