Add Hangfire
Adjust namespace
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using System.Web.Hosting;
|
||||
using Hangfire;
|
||||
using LeafWeb.WebCms.App_Start;
|
||||
using LeafWeb.WebCms.Services.PiscalQueue;
|
||||
using Microsoft.Owin;
|
||||
using Owin;
|
||||
using Umbraco.Web;
|
||||
using ConfigurationManager = System.Configuration.ConfigurationManager;
|
||||
|
||||
[assembly: OwinStartup(typeof(LeafwebOwinStartup))]
|
||||
|
||||
namespace LeafWeb.WebCms.App_Start
|
||||
{
|
||||
public class LeafwebOwinStartup : UmbracoDefaultOwinStartup
|
||||
{
|
||||
public new void Configuration(IAppBuilder app)
|
||||
{
|
||||
base.Configuration(app);
|
||||
new HangfireStartup().Configuration(app);
|
||||
}
|
||||
}
|
||||
|
||||
// https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/59500-Umbraco-and-Background-Tasks
|
||||
public class HangfireStartup
|
||||
{
|
||||
private const string PiscalProcessQueue = "PiscalProcessQueue";
|
||||
|
||||
public void Configuration(IAppBuilder app)
|
||||
{
|
||||
app.UseHangfireDashboard();
|
||||
|
||||
SetupRecurringJobs();
|
||||
}
|
||||
|
||||
private void SetupRecurringJobs()
|
||||
{
|
||||
var queueInterval = ConfigurationManager.AppSettings["ProcessQueueInterval"];
|
||||
// https://discuss.hangfire.io/t/how-to-create-cron-job-that-is-executing-every-15-minutes/533
|
||||
RecurringJob.AddOrUpdate<PiscalQueueManager>(PiscalProcessQueue, p => p.ProcessQueue(), queueInterval);
|
||||
}
|
||||
|
||||
public static void TriggerPiscalProcessQueue()
|
||||
{
|
||||
RecurringJob.Trigger(PiscalProcessQueue);
|
||||
}
|
||||
}
|
||||
|
||||
// http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html
|
||||
public class ApplicationPreload : IProcessHostPreloadClient
|
||||
{
|
||||
public void Preload(string[] parameters)
|
||||
{
|
||||
HangfireBootstrapper.Instance.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public class HangfireBootstrapper : IRegisteredObject
|
||||
{
|
||||
public static readonly HangfireBootstrapper Instance = new HangfireBootstrapper();
|
||||
|
||||
private readonly object _lockObject = new object();
|
||||
private bool _started;
|
||||
|
||||
private BackgroundJobServer _backgroundJobServer;
|
||||
|
||||
private HangfireBootstrapper()
|
||||
{
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (_started) return;
|
||||
_started = true;
|
||||
|
||||
HostingEnvironment.RegisterObject(this);
|
||||
|
||||
// TODO: use Umbraco Connection string?
|
||||
var cs = Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString;
|
||||
|
||||
GlobalConfiguration
|
||||
.Configuration
|
||||
.UseSqlServerStorage("LeafWebContext");
|
||||
// Specify other options here
|
||||
|
||||
_backgroundJobServer = new BackgroundJobServer();
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
lock (_lockObject)
|
||||
{
|
||||
|
||||
_backgroundJobServer?.Dispose();
|
||||
|
||||
HostingEnvironment.UnregisterObject(this);
|
||||
}
|
||||
}
|
||||
|
||||
void IRegisteredObject.Stop(bool immediate)
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using LeafWeb.Core.DAL;
|
||||
using LeafWeb.Core.DAL;
|
||||
using Umbraco.Core;
|
||||
|
||||
namespace WebCms.App_Start
|
||||
namespace LeafWeb.WebCms.App_Start
|
||||
{
|
||||
public class RegisterDataService : ApplicationEventHandler
|
||||
public class RegisterServices : ApplicationEventHandler
|
||||
{
|
||||
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
|
||||
{
|
||||
DataService.RegisterInitializer();
|
||||
HangfireBootstrapper.Instance.Start();
|
||||
|
||||
base.ApplicationStarted(umbracoApplication, applicationContext);
|
||||
}
|
||||
Reference in New Issue
Block a user