Last active
November 19, 2023 11:39
-
-
Save nul800sebastiaan/a944f75642c260b91d053cb65c4699dc to your computer and use it in GitHub Desktop.
Job scheduler compatible with Umbraco upgrading or doing first boot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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