I have a console application.
And I am using the latest version of the following 2 packages:
- Hangfire.Core 1.8.20
- Hangfire.MemoryStorage 1.8.1.1
And I have the following simple console application to run a simple function, with the following CRON:
*/2 */1 * * * *
Which is supposed to run every 2 seconds.
But it never fires.
Any idea?
using System.Linq.Expressions;
using Hangfire;
using Hangfire.MemoryStorage;
namespace TestJob
{
internal class Program
{
static void Main(string args)
{
var jobManager = new JobManager();
jobManager.Start();
Console.WriteLine("Hello, World!");
Console.Read();
}
}
class JobManager
{
public void Start()
{
GlobalConfiguration.Configuration.UseMemoryStorage();
var pulseCron = "*/2 */1 * * * *";
AddJob(BackgroundJobs.Pulse, () => RunPulseJobAsync(), pulseCron);
}
private void AddJob(string jobId, Expression<Func<Task>> methodCall, string jobCron)
{
RecurringJob.AddOrUpdate(jobId, methodCall, jobCron);
}
[Queue(JobConstants.CriticalQueue)]
public async Task RunPulseJobAsync()
{
var jobRunner = new PulseJob();
await jobRunner.StartAsync();
}
}
public sealed class PulseJob() : IJobRunner
{
public string Id => BackgroundJobs.Pulse;
public async Task StartAsync()
=> Console.WriteLine(DateTime.Now);
}
public interface IJobRunner
{
public string Id { get; }
Task StartAsync();
}
public static class JobConstants
{
public const string CriticalQueue = "critical";
}
public static class BackgroundJobs
{
public const string Pulse = "Pulse";
}
}
Even I followed Processing Jobs in a Console Application — Hangfire Documentation to add the BackgroundJobServer, it still does not trigger:
private BackgroundJobServer server;
public void Start()
{
GlobalConfiguration.Configuration.UseMemoryStorage();
server = new BackgroundJobServer();
var pulseCron = "*/2 */1 * * * *";
AddJob(BackgroundJobs.Pulse, () => RunPulseJobAsync(), pulseCron);
}
If I use exactly what the official document says, then it runs, despite it runs every 15 seconds:
using System.Linq.Expressions;
using Hangfire;
using Hangfire.MemoryStorage;
namespace TestJob
{
internal class Program
{
static void Main(string[] args)
{
var jobManager = new JobManager();
jobManager.Start();
Console.WriteLine("Hello, World!");
Console.Read();
}
}
class JobManager2
{
BackgroundJobServer server;
public void Start()
{
GlobalConfiguration.Configuration.UseMemoryStorage();
RecurringJob.AddOrUpdate("Test", () => DoSomething(), "*/2 */1 * * * *");
server = new BackgroundJobServer();
}
public void DoSomething()
{
Console.WriteLine("2- " + DateTime.Now);
}
}
}
So, to conclude, what I have noticed are 3 things:
- the minimum interval is every 15 seconds.
- it does not work with queue.
- it does not work with the job container class with parameters in constructor.