Skip to content

Instantly share code, notes, and snippets.

@abelevtsov
Created July 6, 2016 12:19
Show Gist options
  • Save abelevtsov/cefac3f167bb20da30e2c31eec3a643c to your computer and use it in GitHub Desktop.
Save abelevtsov/cefac3f167bb20da30e2c31eec3a643c to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Web.Http;
using Castle.Core.Internal;
using Castle.Core.Resource;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Hangfire;
using Microsoft.Owin;
using Owin;
using Pysco68.Owin.Logging.NLogAdapter;
using JobScheduler;
using JobScheduler.Interfaces;
using JobScheduler.Properties;
[assembly: OwinStartup(typeof(Startup))]
namespace JobScheduler
{
public class Startup
{
private IEnumerable<IJobRunner> JobRunners { get; set; }
public void Configuration(IAppBuilder app)
{
var container = BootstrapContainer();
JobRunners = container.ResolveAll<IJobRunner>();
var config =
new HttpConfiguration
{
DependencyResolver = new WindsorDependencyResolver(container)
};
// Маршруты веб-API
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = RouteParameter.Optional });
GlobalJobFilters.Filters.Add(
new AutomaticRetryAttribute
{
Attempts = 3
});
var options = new BackgroundJobServerOptions
{
Queues = new[] { "critical", "default" },
};
var dashboardOptions = new DashboardOptions
{
AppPath = "/dashboard",
AuthorizationFilters = new[] { new DashboardAuthorizationFilter() }
};
app.UseHangfireDashboard("/dashboard", dashboardOptions)
.UseHangfireServer(options)
.UseWebApi(config)
.UseNLog();
JobRunners.ForEach(r => r.ProcessRecurringJob());
}
private static IWindsorContainer BootstrapContainer()
{
return
new WindsorContainer(new XmlInterpreter(new FileResource("castle.config")))
.Register(Classes.FromThisAssembly().BasedOn<ApiController>().LifestyleTransient())
.AddFacility<CollectionFacility>()
.RegisterRecurringJob<IApplicationsJob>(Settings.Default.AppImportJobCronExpression);
}
}
}
using Castle.Core.Configuration;
using Castle.MicroKernel;
using Castle.MicroKernel.Resolvers.SpecializedResolvers;
namespace JobScheduler
{
public class CollectionFacility : IFacility
{
public void Init(IKernel kernel, IConfiguration facilityConfig)
{
kernel.Resolver.AddSubResolver(new CollectionResolver(kernel));
}
public void Terminate()
{
}
}
}
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using JobScheduler.Interfaces;
namespace JobScheduler
{
public static class WindsorContainerExtensions
{
public static IWindsorContainer RegisterRecurringJob<T>(this IWindsorContainer container, string cronExpression) where T : IJob
{
container.Register(Component.For<IJobRunner>()
.ImplementedBy<JobRunner<T>>()
.LifeStyle.Transient
.DependsOn(Dependency.OnValue("cronExpression", cronExpression)));
container.Register(Classes.FromAssemblyContaining<T>()
.BasedOn<JobRunner<T>>()
.WithService
.FromInterface());
return container;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
using Castle.Windsor;
namespace JobScheduler
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Justified")]
public class WindsorDependencyResolver : IDependencyResolver, IServiceProvider
{
private readonly IWindsorContainer container;
public WindsorDependencyResolver(IWindsorContainer container)
{
this.container = container;
}
public IDependencyScope BeginScope()
{
return new WindsorDependencyScope(container);
}
public object GetService(Type serviceType)
{
return container.Kernel.HasComponent(serviceType) ? container.Resolve(serviceType) : null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
return container.ResolveAll(serviceType).Cast<object>();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Justified")]
public void Dispose()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment