Skip to content

Instantly share code, notes, and snippets.

@nul800sebastiaan
Last active November 19, 2023 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nul800sebastiaan/a944f75642c260b91d053cb65c4699dc to your computer and use it in GitHub Desktop.
Save nul800sebastiaan/a944f75642c260b91d053cb65c4699dc to your computer and use it in GitHub Desktop.
Job scheduler compatible with Umbraco upgrading or doing first boot
using Hangfire;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Services;
namespace UmbraCalendar.Jobs;
public class Scheduler : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Components().Append<SchedulerComponent>();
}
public class SchedulerComponent : IComponent
{
private readonly IRuntimeState _runtimeState;
public SchedulerComponent(IRuntimeState runtimeState)
{
_runtimeState = runtimeState;
}
public void Initialize()
{
// ⚠️ test if the site is running, we shouldn't schedule tasks in any other state
if(_runtimeState.Level < RuntimeLevel.Run) return;
RecurringJob.AddOrUpdate<IHangfireTestService>($"💡 Run test task", x =>
x.RunTestTask(null), "0 */4 * * *");
}
public void Terminate()
{ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment