Skip to content

Commit

Permalink
fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid8000 committed Nov 18, 2017
1 parent 9fcbaa2 commit d0c8f4c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@

* Make it Azure Web Job-hostable

## 2.1.1

* Fix it so that it works as Azure Web Job


[scardetto]: https://github.com/scardetto
65 changes: 58 additions & 7 deletions Topper/ServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,65 @@ public static void Run(ServiceConfiguration configuration)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));

HostFactory.Run(factory =>
if (IsAzureWebJob)
{
factory.UseLibLog();
RunAsAzureWebJob(configuration);
return;
}

RunAsTopShelf(configuration);
}

static void RunAsAzureWebJob(ServiceConfiguration configuration)
{
try
{
var topperService = new TopperService(configuration);
var keepRunning = true;

topperService.StartupFailed += exception =>
{
Log.ErrorException("Startup failed", exception);
Volatile.Write(ref keepRunning, false);
};

factory.OnException(exception =>
DetectShutdownInAzureWebJobs(() =>
{
Log.ErrorException("Unhandled exception", exception);
Volatile.Write(ref keepRunning, false);
});

try
{
Log.Info("Starting topper service(s)");
topperService.Start();

Log.Info("Running...");

while (Volatile.Read(ref keepRunning))
{
Thread.Sleep(100);
}

Log.Info("Exiting...");
}
finally
{
Log.Info("Stopping topper service(s)");
topperService.Stop();
}
}
catch (Exception exception)
{
Log.ErrorException("Unhandled exception in", exception);
}
}

static void RunAsTopShelf(ServiceConfiguration configuration)
{
HostFactory.Run(factory =>
{
factory.UseLibLog();
factory.OnException(exception => { Log.ErrorException("Unhandled exception", exception); });
factory.Service<TopperService>(config =>
{
config.WhenStarted((service, control) =>
Expand All @@ -48,12 +98,13 @@ public static void Run(ServiceConfiguration configuration)
});
config.WhenStopped(service => service.Stop());
config.ConstructUsing(() => new TopperService(configuration));
config.AfterStartingService(DetectShutdownInAzureWebJobs);
});
});
}

static void DetectShutdownInAzureWebJobs(HostStartedContext context)
static bool IsAzureWebJob => !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("WEBJOBS_SHUTDOWN_FILE"));

static void DetectShutdownInAzureWebJobs(Action stopAction)
{
var filePath = Environment.GetEnvironmentVariable("WEBJOBS_SHUTDOWN_FILE");

Expand All @@ -73,7 +124,7 @@ static void DetectShutdownInAzureWebJobs(HostStartedContext context)
if (File.Exists(filePath))
{
Log.Info($"Detected Azure Web Job file {filePath} - shutting down");
context.Stop();
stopAction();
}
}
catch { }
Expand Down
6 changes: 6 additions & 0 deletions Topper/Topper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@
<DocumentationFile>bin\Release\Topper.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Data.Services.Client, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.Services.Client.5.6.4\lib\net40\Microsoft.Data.Services.Client.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Spatial, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
4 changes: 2 additions & 2 deletions Topper/Topper.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<tags>idisposable topshelf host windows-service</tags>
<!--<iconUrl>https://github.com/mookid8000/Rebus/raw/master/artwork/little_rebusbus2_copy-200x200.png</iconUrl>-->
<dependencies>
<dependency id="Topshelf" version="4.0.3" />
<dependency id="Topshelf.LibLog" version="1.2.0" />
<dependency id="Topshelf" version="4.0.3"/>
<dependency id="Topshelf.LibLog" version="1.2.0"/>
</dependencies>
</metadata>
<files>
Expand Down
4 changes: 4 additions & 0 deletions Topper/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<assemblyIdentity name="Topshelf" publicKeyToken="b800c4cfcdeea87b" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
4 changes: 4 additions & 0 deletions Toppertest/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<assemblyIdentity name="Topshelf" publicKeyToken="b800c4cfcdeea87b" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

0 comments on commit d0c8f4c

Please sign in to comment.