quartz.net作业未在.net core 2.0中运行

时间:2018-07-26 06:23:57

标签: quartz-core

我正在使用.net core 2.0和quartz.net 3.0.6。

但似乎该作业未在我的示例项目中运行。

下面是我的代码:

石英配置:

public class QuartzConfiguration : IQuartzConfiguration
{
    public IScheduler Scheduler => StdSchedulerFactory.GetDefaultScheduler().Result;
}

ScheduleJobManager:

public class QuartzScheduleJobManager : IQuartzScheduleJobManager
{
    private readonly IQuartzConfiguration _quartzConfiguration;

    public QuartzScheduleJobManager(IQuartzConfiguration quartzConfiguration)
    {
        _quartzConfiguration = quartzConfiguration;
    }

    public void Start()
    {
        _quartzConfiguration.Scheduler.Start();
    }

    public async Task ScheduleAsync<TJob>(Action<JobBuilder> configureJob, Action<TriggerBuilder> configureTrigger) where TJob : IJob
    {
        JobBuilder jobToBuild = JobBuilder.Create<TJob>();
        configureJob(jobToBuild);
        IJobDetail job = jobToBuild.Build();

        TriggerBuilder triggerToBuild = TriggerBuilder.Create();
        configureTrigger(triggerToBuild);
        ITrigger trigger = triggerToBuild.Build();

        await _quartzConfiguration.Scheduler.ScheduleJob(job, trigger);
    }
}

工作:

public class MyJob : IJob
{
    private readonly IHostingEnvironment _environment;

    public MyJob(IHostingEnvironment environment)
    {
        _environment = environment;
    }

    public Task Execute(IJobExecutionContext context)
    {
        string filePath = Path.Combine(_environment.ContentRootPath, "Logs\\Log.txt");

        File.AppendAllText(filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);

        return Task.CompletedTask;
    }
}

Starup:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IQuartzConfiguration, QuartzConfiguration>();
        services.AddSingleton<IQuartzScheduleJobManager, QuartzScheduleJobManager>();

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        IQuartzScheduleJobManager service = app.ApplicationServices.GetService<IQuartzScheduleJobManager>();
        service.Start();

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

控制器:

private readonly IQuartzScheduleJobManager _jobManager;

    public HomeController(IQuartzScheduleJobManager jobManager)
    {
        _jobManager = jobManager;
    }

    public async Task<IActionResult> ScheduleJob()
    {

        await _jobManager.ScheduleAsync<MyJob>(job =>
        {
            job.WithIdentity("MyJobIdentity", "MyJobGroup").WithDescription("A job to write current time");
        }, trigger =>
        {
            trigger.WithIdentity("MyJobIdentityTrigger").WithSimpleSchedule(schedule =>
                {
                    schedule.RepeatForever().WithIntervalInSeconds(5).Build();
                }).StartNow();
        });

        return Content("OK, scheduled!");
    }

在starup.cs中,我启动服务

IQuartzScheduleJobManager服务= app.ApplicationServices.GetService();

service.Start();

在Controller中,我执行了'ScheduleAsync'方法,但是我的工作没有运行。

为什么?

0 个答案:

没有答案