Hangfire configuration to always run

This commit is contained in:
2015-11-04 00:14:22 -05:00
parent 5651c2876d
commit 42f0c12249
3 changed files with 72 additions and 3 deletions
+7
View File
@@ -45,6 +45,13 @@ namespace MileageTraker.Web
ModelMetadataProviders.Current = new CustomMetadataProvider(); ModelMetadataProviders.Current = new CustomMetadataProvider();
LogManager.GetCurrentClassLogger().Info("Application_Start()"); LogManager.GetCurrentClassLogger().Info("Application_Start()");
HangfireBootstrapper.Instance.Start();
}
protected void Application_End(object sender, EventArgs e)
{
HangfireBootstrapper.Instance.Stop();
} }
} }
+62 -2
View File
@@ -1,10 +1,12 @@
using System; using System;
using System.Web.Hosting;
using Hangfire; using Hangfire;
using Microsoft.Owin; using Microsoft.Owin;
using MileageTraker.Web;
using MileageTraker.Web.Email; using MileageTraker.Web.Email;
using Owin; using Owin;
[assembly: OwinStartup(typeof(MileageTraker.Web.Startup))] [assembly: OwinStartup(typeof (Startup))]
namespace MileageTraker.Web namespace MileageTraker.Web
{ {
@@ -13,7 +15,7 @@ namespace MileageTraker.Web
public void Configuration(IAppBuilder app) public void Configuration(IAppBuilder app)
{ {
GlobalConfiguration.Configuration GlobalConfiguration.Configuration
.UseSqlServerStorage("MileageTrakerContext"); .UseSqlServerStorage("MileageTrakerContext");
app.UseHangfireDashboard(); app.UseHangfireDashboard();
app.UseHangfireServer(); app.UseHangfireServer();
@@ -27,4 +29,62 @@ namespace MileageTraker.Web
"serviceReminderJob", s => s.SendAllNotificationEmails(), Cron.Weekly(DayOfWeek.Monday, 6)); "serviceReminderJob", s => s.SendAllNotificationEmails(), Cron.Weekly(DayOfWeek.Monday, 6));
} }
} }
// 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);
GlobalConfiguration.Configuration
.UseSqlServerStorage("MileageTrakerContext");
// Specify other options here
_backgroundJobServer = new BackgroundJobServer();
}
}
public void Stop()
{
lock (_lockObject)
{
if (_backgroundJobServer != null)
{
_backgroundJobServer.Dispose();
}
HostingEnvironment.UnregisterObject(this);
}
}
void IRegisteredObject.Stop(bool immediate)
{
Stop();
}
}
} }
+3 -1
View File
@@ -343,6 +343,9 @@
<Content Include="fonts\fontawesome-webfont.ttf" /> <Content Include="fonts\fontawesome-webfont.ttf" />
<Content Include="fonts\fontawesome-webfont.eot" /> <Content Include="fonts\fontawesome-webfont.eot" />
<Content Include="fonts\FontAwesome.otf" /> <Content Include="fonts\FontAwesome.otf" />
<Content Include="Logs\empty.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="NLog.config"> <Content Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
@@ -616,7 +619,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="App_Data\Uploads\" /> <Folder Include="App_Data\Uploads\" />
<Folder Include="Logs\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Migrations\201509111820437_FuelLog.resx"> <EmbeddedResource Include="Migrations\201509111820437_FuelLog.resx">