Quartz.NET IOptionsSnapshot

时间:2017-08-17 10:56:20

标签: c# dependency-injection quartz.net

目标是能够更改appsettings.json中的设置并让使用此值的作业自动反映更改并使用新值,但我无法让它工作。这是我最近的尝试:

appsettings.json

"Settings": {
    "GameServerVersion": 15
},

Settings.cs

public class Settings
{
    public int GameServerVersion { get; set; }
}

Startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();
        this.Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    private IJobManager JobManager { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.AddCors();
        services.AddOptions();

        services.Configure<Settings>(this.Configuration.GetSection("Settings"));
        services.AddSingleton<ServersJob>();
        services.AddSingleton<JobFactory>();
        services.AddSingleton<IJobManager, JobManager>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {

        app.UseCors(builder => builder.AllowAnyOrigin());
        app.UseLogger();
        app.UseMvc();

        this.JobManager = app.ApplicationServices.GetService<IJobManager>();
        this.JobManager.StartAsync();
    }

JobManager.cs

public interface IJobManager
{
    void StartAsync();

    void StopAsync();
}


public class JobManager : IJobManager
{
    private static IScheduler scheduler;

    private readonly Settings settings;

    private readonly JobFactory jobFactory;

    public JobManager(IOptionsSnapshot<Settings> settingsAccessor, JobFactory jobFactory)
    {
        settings = settingsAccessor.Value;
        this.jobFactory = jobFactory;
    }

    public async void StartAsync()
    {
        StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
        scheduler = await schedulerFactory.GetScheduler();
        scheduler.JobFactory = this.jobFactory;

        IJobDetail serversJob = JobBuilder.Create<ServersJob>()
            .WithIdentity("serversJob", "MainGroup")
            .Build();

        ITrigger serversTrigger = TriggerBuilder.Create()
            .WithIdentity("serversTrigger", "MainGroup")
            .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())
            .ForJob("serversJob", "MainGroup")
            .Build();

        await scheduler.ScheduleJob(serversJob, serversTrigger);

        await scheduler.Start();
    }

    public async void StopAsync()
    {
        // Some logic
    }
}

JobFactory.cs

public class JobFactory : IJobFactory
{
    private readonly IServiceProvider Container;

    public JobFactory(IServiceProvider container)
    {
        this.Container = container;
    }

    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        return this.Container.GetService(bundle.JobDetail.JobType) as IJob;
    }

    public void ReturnJob(IJob job)
    {
        var disposable = job as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }
}

Serversjob.cs

public readonly Settings settings;

public ServersJob(IOptionsSnapshot<Settings> options)
{
    settings = options.Value;
}

public async Task Execute(IJobExecutionContext context)
{
    int gameServerVersion = settings.GameServerVersion;
    return;
}

这样可以正常工作,但作业并未反映所做的任何更改。我错过了明显的(可能)吗?提前谢谢。

0 个答案:

没有答案