Permalink
Browse files

Added support for HostinEnvironment

  • Loading branch information...
1 parent d4b472d commit a4af8b2aece2f8978dd6300146d6c6f7967fe7d0 @chaliy committed Oct 23, 2011
View
34 src/InlineScheduler/Scheduler.cs
@@ -10,24 +10,16 @@ public class Scheduler
{
private readonly WorkBag _work;
private bool _stopped;
- private readonly DateTime _sartTime;
-
- public SchedulerStats Stats
- {
- get
- {
- var stats = StatsHelper.GatherOveralStatistics(_work);
- stats.IsStopped = _stopped;
- stats.StartTime = _sartTime;
- return stats;
- }
- }
+ private readonly DateTime _sartTime;
public Scheduler(IWorkContext context = null)
{
+ //HostingEnvironment
_work = new WorkBag(context);
_stopped = true;
_sartTime = DateTime.Now;
+
+ // TODO Should use timer
Task.Factory.StartNew(() =>
{
while (true)
@@ -53,7 +45,25 @@ public Scheduler(IWorkContext context = null)
});
}
+ public SchedulerStats Stats
+ {
+ get
+ {
+ var stats = StatsHelper.GatherOveralStatistics(_work);
+ stats.IsStopped = _stopped;
+ stats.StartTime = _sartTime;
+ return stats;
+ }
+ }
+
public bool IsStopped { get { return _stopped; } }
+ public bool IsRunningJobsNow
+ {
+ get
+ {
+ return Stats.RunningJobs > 0;
+ }
+ }
public void Stop()
{
View
37 src/Server/Advanced/Hosting/HostingRegistration.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Web.Hosting;
+using System.Threading;
+using InlineScheduler.Server.Utility;
+
+namespace InlineScheduler.Server.Advanced.Hosting
+{
+ public class HostingRegistration : IRegisteredObject
+ {
+ // For more details http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx
+
+ private readonly Scheduler _underline;
+
+ public HostingRegistration(Scheduler underline)
+ {
+ _underline = underline;
+ }
+
+ public void Stop(bool immediate)
+ {
+ if (immediate)
+ {
+ _underline.Stop();
+ Do.TryWaitUntil(() => _underline.IsRunningJobsNow, TimeSpan.FromSeconds(15));
+ }
+ HostingEnvironment.UnregisterObject(this);
+ }
+
+ public static void Register(Scheduler scheduler)
+ {
+ HostingEnvironment.RegisterObject(new HostingRegistration(scheduler));
+ }
+ }
+}
View
3 src/Server/InlineSchedulerServer.cs
@@ -2,6 +2,7 @@
using InlineScheduler.Server.Advanced.Service;
using Microsoft.ApplicationServer.Http;
using WebApiContrib.Formatters.JsonNet;
+using InlineScheduler.Server.Advanced.Hosting;
namespace InlineScheduler.Server
{
@@ -24,6 +25,8 @@ public static void Init(Scheduler scheduler, string prefix = "Scheduler/")
config.Formatters.Insert(0, new JsonNetFormatter());
RouteTable.Routes.MapServiceRoute<SchedulerService>(prefix, config);
+
+ HostingRegistration.Register(scheduler);
}
}
View
2 src/Server/Server.csproj
@@ -86,11 +86,13 @@
<Compile Include="..\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
+ <Compile Include="Advanced\Hosting\HostingRegistration.cs" />
<Compile Include="Advanced\Service\Content\Accessor.cs" />
<Compile Include="Advanced\Service\SchedulerService.cs" />
<Compile Include="Advanced\Service\Temp\JsonNetFormatter.cs" />
<Compile Include="InlineSchedulerServer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Utility\Do.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Advanced\Service\Content\js\app.coffee">
View
24 src/Server/Utility/Do.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+
+namespace InlineScheduler.Server.Utility
+{
+ static class Do
+ {
+ public static void TryWaitUntil(Func<bool> check, TimeSpan timeout)
+ {
+ var iterations = timeout.TotalMilliseconds / 200;
+ for (var i = 0; i < iterations; i++)
+ {
+ if (!check())
+ {
+ return;
+ }
+ Thread.Sleep(200);
+ }
+ }
+ }
+}

0 comments on commit a4af8b2

Please sign in to comment.