We want to give feasibility for the user from UI to schedule a job from 15 minutes to 23:59 hours of the time range. By which different users may select different time slots to run their jobs.
For example:
Scenario-1: User A wants to schedule the job to run every 15 minutes.
Scenario-2: User B wants to schedule the job to run every 30 minutes.
So on.
I will post my answer for my own question. I used Recurring job with a Unique identifier (i.e., $"userid_{job.UserId}" ) which is the first parameter in Recurring.AddOrUpdate() method by which separate recurring jobs will be created for each user. Please have a look at the below code to understand. Now Hangfire will create 4 recurring jobs for 4 different users with different frequencies.
List<Scheduler> list = new List<Scheduler>
{
new Scheduler { EveryXMinutes = 3, UserId = 1023028, On = true },
new Scheduler { EveryXMinutes = 5, UserId = 1023023, On = true },
new Scheduler { EveryXMinutes = 10, UserId = 1023025, On = true },
new Scheduler { EveryXMinutes = 15, UserId = 1023027, On = true }
};
foreach (var job in list)
{
if (job.On)
{
RecurringJob.AddOrUpdate($"userid_{job.UserId}", () => demoService.RunTaskDemo2(job.UserId), $"*/{job.EveryXMinutes} * * * *");
}
else
{
RecurringJob.RemoveIfExists($"userid_{job.UserId}");
}
}
RunTaskDemo2(int) is a common method which runs for every user
public void RunTaskDemo2(int userid)
{
// Any code of your own
Console.WriteLine("--------------&&&&&----------------------");
Console.WriteLine($"RUNNING TASK FOR USERID {userid} - {i.ToString()}");
Console.WriteLine("--------------^^^^^^^----------------------");
}
I hope this is the solution.