I'm trying to evaluate Hangfire as a potential task scheduling solution for my latest project.
When playing around with recurring tasks, I noticed that when I try to console-write the current time, the same time gets written to the console every time.
Can someone please show me how to force hangfire to re-execute the tasks every time they're run? Google was no help, probably because I'm having trouble finding the right words to search for.
The "update system information" task would be useless this way.
My Code:
using System;
using System.Globalization;
using Hangfire;
using Microsoft.Owin.Hosting;
namespace HangfireTests
{
public class HangfireTests
{
public HangfireTests()
{
}
public void Start()
{
WebApp.Start<Startup>("http://localhost:8888");
Console.WriteLine("Server started... press any key to shut down");
RecurringJob.AddOrUpdate("systeminfo",
() => Console.WriteLine($"updating system information {GetTime()}"), Cron.Minutely);
RecurringJob.AddOrUpdate("checktask",
() => Console.WriteLine($"checking tasks {GetTime()}"), Cron.Minutely);
}
public static string GetTime()
{
return DateTime.Now.ToString(CultureInfo.InvariantCulture);
}
public void Stop()
{
}
}
}
If you enqueue the GetTime function instead of the WriteLine, it will be evaluated at execution time. This will give you the time when the job is executed.
using System;
using System.Globalization;
using Hangfire;
using Microsoft.Owin.Hosting;
namespace HangfireTests
{
public class HangfireTests
{
public HangfireTests()
{
}
public void Start()
{
WebApp.Start<Startup>("http://localhost:8888");
Console.WriteLine("Server started... press any key to shut down");
RecurringJob.AddOrUpdate("systeminfo",
() => GetTime(), Cron.Minutely);
RecurringJob.AddOrUpdate("checktask",
() => GetTime(), Cron.Minutely);
}
public static string GetTime()
{
var now = DateTime.Now.ToString(CultureInfo.InvariantCulture);
Console.WriteLine(now);
return now;
}
public void Stop()
{
}
}
}