Skip to content

Instantly share code, notes, and snippets.

@bradphelan
Last active January 8, 2018 15:05
Show Gist options
  • Save bradphelan/964a9458582fada46af689acac96ed34 to your computer and use it in GitHub Desktop.
Save bradphelan/964a9458582fada46af689acac96ed34 to your computer and use it in GitHub Desktop.
using Microsoft.Azure;
using Microsoft.WindowsAzure;
using Owin;
using Weingartner.Distribution.Website.Accounts;
using Weingartner.Distribution.Website.Storage;
using Weingartner.Distribution.Website.Storage.Azure;
namespace Weingartner.Distribution.Website.Hosting.Azure
{
public class AzureStartup
{
public void Configuration(IAppBuilder app)
{
var db = Data.DefaultEntitlementsDB;
IStorage storage = new AzureStorage(CloudConfigurationManager.GetSetting("StorageConnectionString"));
var bootstrapper = new Bootstrapper(db, storage, WebSocket.Instance, FeedbackStorage.CSharp.StoreFeedback(db));
new Startup(bootstrapper).Configuration(app);
}
}
}
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using Microsoft.WindowsAzure.ServiceRuntime;
using Serilog;
namespace Weingartner.Distribution.Website.Hosting.Azure
{
public class WorkerRole : RoleEntryPoint
{
private readonly CancellationTokenSource _CancellationTokenSource = new CancellationTokenSource();
private readonly ManualResetEvent _RunCompleteEvent = new ManualResetEvent(false);
private IDisposable _App;
public override void Run()
{
try
{
RunAsync(_CancellationTokenSource.Token).Wait();
}
finally
{
_RunCompleteEvent.Set();
}
}
public override bool OnStart()
{
var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Secured"];
var baseUri = $"{endpoint.Protocol}://{endpoint.IPEndpoint}";
Logging.Log.SetupRemoteServer(baseUri);
ServicePointManager.DefaultConnectionLimit = 12;
Log.Information("Weingartner.Distribution.Azure.Host is starting");
try
{
_App = WebApp.Start<AzureStartup>(baseUri);
}
catch (Exception e)
{
Log.Fatal(e, "WebApp couldn't be started");
}
return base.OnStart();
}
public override void OnStop()
{
Log.Information("Weingartner.Distribution.Azure.Host is stopping");
_CancellationTokenSource.Cancel();
_RunCompleteEvent.WaitOne();
_App?.Dispose();
base.OnStop();
Log.Information("Weingartner.Distribution.Azure.Host has stopped");
}
private static async Task RunAsync(CancellationToken cancellationToken)
{
PingServer.pingForever(cancellationToken, WebSocket.Instance, TimeSpan.FromMinutes( 1 ));
while (true)
{
await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
Log.Information("Working");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment