using System;
using System.Threading;
using Hangfire;
using Hangfire.MemoryStorage;
// https://www.hangfire.io/
// https://docs.hangfire.io/en/latest/getting-started/index.html
namespace TopNugetPackages
{
public class MainHangFireDriver
{
// Probably need to use a REAL storage class such as SQL Server to make work.
public MainHangFireDriver()
{
GlobalConfiguration.Configuration.UseMemoryStorage();
//GlobalConfiguration.Configuration
// .UseSqlServerStorage(@"Server=.\SQLEXPRESS; Database=Hangfire.Sample; Integrated Security=True");
}
public void TheJob()
{
Console.WriteLine("Inside TheJob");
}
// Schedular, and scheduling services, for timers and background task
public void DoIt()
{
Console.WriteLine("Hangfire Starts");
// Fire-and-forget jobs are executed only once and almost immediately after creation.
var jobId0 = BackgroundJob.Enqueue(() => TheJob());
var jobId1 = BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget!"));
Thread.Sleep(1000);
// Delayed jobs are executed only once too, but not immediately, after a certain time interval.
var jobId2 = BackgroundJob.Schedule(() => Console.WriteLine("Delayed!"), TimeSpan.FromMilliseconds(100));
Thread.Sleep(1000);
// Recurring jobs fire many times on the specified CRON schedule.
RecurringJob.AddOrUpdate("JobId", () => Console.WriteLine("Recurring!"), Cron.Minutely);
Thread.Sleep(1000);
// Continuations are executed when its parent job has been finished.
BackgroundJob.ContinueJobWith(jobId1, () => Console.WriteLine("Continuation!"));
Thread.Sleep(1000);
Console.WriteLine("Hangfire Ends");
}
}
}