C# Cron作业调度器是否每天添加新的quartz作业?
我希望每天凌晨1点运行crobjob,以添加新的Quartz.net作业,这些作业将在同一天运行一次并被删除 到目前为止,我能够添加工作来收听每一场足球比赛,但不知道如何添加cron工作来每天添加这些工作C# Cron作业调度器是否每天添加新的quartz作业?,c#,quartz-scheduler,quartz.net,C#,Quartz Scheduler,Quartz.net,我希望每天凌晨1点运行crobjob,以添加新的Quartz.net作业,这些作业将在同一天运行一次并被删除 到目前为止,我能够添加工作来收听每一场足球比赛,但不知道如何添加cron工作来每天添加这些工作 class LiveScheduledJobs : IScheduledJob { public IScheduler Run() { var schd = GetScheduler(); if (!schd.I
class LiveScheduledJobs : IScheduledJob
{
public IScheduler Run()
{
var schd = GetScheduler();
if (!schd.IsStarted)
schd.Start();
string jobName = "", triggerName = "";
using (var db = new ABCDataContext())
{
var fixtures = db.Fixtures;
string group = "livematch";
foreach (var item in fixtures.Where(blahblah))
{
jobName = "match" + item.Id;
triggerName = "trigger" + item.Id;
int categoryId = db.Fixture_Category_Mappings.FirstOrDefault(f => f.FixtureId == item.Id).CategoryId;
var seasonStage = db.Season_Stage_Mappings.FirstOrDefault(f => f.Id == item.SeasonStageId);
// Define the Job to be scheduled
var job = JobBuilder.Create<LiveMatchJob>()
.WithIdentity(jobName, group )
.UsingJobData("url", item.AutoUrl)
.UsingJobData("seasonId", seasonStage.SeasonId)
.UsingJobData("categoryId", categoryId)
.UsingJobData("stageId", seasonStage.StageId)
.RequestRecovery()
.Build();
DateTimeOffset startTime = new DateTime(item.Time.Year, item.Time.Month, item.Time.Day,
item.Time.Hour, item.Time.Minute, item.Time.Second);
ITrigger triggerLive = TriggerBuilder.Create()
.WithIdentity(triggerName, group)
.StartAt(startTime)
.WithSimpleSchedule(x =>x.WithIntervalInMinutes(2).RepeatForever())
.Build();
// Validate that the job doesn't already exists
if (schd.CheckExists(new JobKey(jobName, group)))
{
schd.DeleteJob(new JobKey(jobName, group));
}
var schedule = schd.ScheduleJob(job, triggerLive);
}
}
return schd;
}
我添加了另一个计划程序:
class CronJobScheduler : IScheduledJob
{
public IScheduler Run()
{
var schd = GetScheduler();
if (!schd.IsStarted)
schd.Start();
var trigger = (ICronTrigger)TriggerBuilder.Create()
.WithIdentity("cronjobTrigger", "cronGroup1")
.WithCronSchedule("0 0 1 1/1 * ? *")
.StartAt(DateTime.UtcNow)
.WithPriority(1)
.Build();
var job = JobBuilder.Create<CronJob>()
.WithIdentity("cronjob", "cronGroup1")
.RequestRecovery()
.Build();
var schedule = schd.ScheduleJob(job, trigger);
return schd;
}
// Get an instance of the Quartz.Net scheduler
private static IScheduler GetScheduler()
{
try
{
var properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "ServerScheduler";
// set remoting expoter
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.scheduler.proxy.address"] = string.Format("tcp://{0}:{1}/{2}", "localhost", "555",
"QuartzScheduler");
// Get a reference to the scheduler
var sf = new StdSchedulerFactory();//properties);
return sf.GetScheduler();
}
catch (Exception ex)
{
Console.WriteLine("Scheduler not available: '{0}'", ex.Message);
throw;
}
}
public void Shutdown(IScheduler schd)
{
schd.Shutdown();
}
}
我添加了另一个计划程序:
class CronJobScheduler : IScheduledJob
{
public IScheduler Run()
{
var schd = GetScheduler();
if (!schd.IsStarted)
schd.Start();
var trigger = (ICronTrigger)TriggerBuilder.Create()
.WithIdentity("cronjobTrigger", "cronGroup1")
.WithCronSchedule("0 0 1 1/1 * ? *")
.StartAt(DateTime.UtcNow)
.WithPriority(1)
.Build();
var job = JobBuilder.Create<CronJob>()
.WithIdentity("cronjob", "cronGroup1")
.RequestRecovery()
.Build();
var schedule = schd.ScheduleJob(job, trigger);
return schd;
}
// Get an instance of the Quartz.Net scheduler
private static IScheduler GetScheduler()
{
try
{
var properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "ServerScheduler";
// set remoting expoter
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.scheduler.proxy.address"] = string.Format("tcp://{0}:{1}/{2}", "localhost", "555",
"QuartzScheduler");
// Get a reference to the scheduler
var sf = new StdSchedulerFactory();//properties);
return sf.GetScheduler();
}
catch (Exception ex)
{
Console.WriteLine("Scheduler not available: '{0}'", ex.Message);
throw;
}
}
public void Shutdown(IScheduler schd)
{
schd.Shutdown();
}
}
您不需要添加额外的计划程序。创建一个作业来安排其他作业 例如,创建从IJob继承的JobSchedulingJob:
public class JobScheduling : IJob
{
public void Execute(IJobExecutionContext context)
{
var schd = context.Scheduler;
scheduleOtherJobs(scheduler);//this is where you schedule the other jobs
}
}
然后,使用CronTrigger计划此JobSchedulingJob每天运行。您不需要添加额外的计划程序。创建一个作业来安排其他作业 例如,创建从IJob继承的JobSchedulingJob:
public class JobScheduling : IJob
{
public void Execute(IJobExecutionContext context)
{
var schd = context.Scheduler;
scheduleOtherJobs(scheduler);//this is where you schedule the other jobs
}
}
然后,使用CronTrigger计划此JobSchedulingJob每天运行。非常好地解释了CRON触发器。如果您澄清您的尝试和失败之处,我相信我可以提供帮助。这些示例用于为一个ForJob(“myJob”、“group1”)运行cron作业。让一个cron作业来添加一组像上面这样的任务怎么样?在上面的示例中,当执行LiveJobMatch时,您希望向日程中添加其他作业吗?或者你想用另一个作业来添加LiveJobMatches吗?我应该像下面的答案那样运行两个调度程序吗?谢谢。您可以这样做,但是,是什么阻止您将Cron作业添加到现有计划中呢?很好地解释了CRON触发器。如果您澄清您的尝试和失败之处,我相信我可以提供帮助。这些示例用于为一个ForJob(“myJob”、“group1”)运行cron作业。让一个cron作业来添加一组像上面这样的任务怎么样?在上面的示例中,当执行LiveJobMatch时,您希望向日程中添加其他作业吗?或者你想用另一个作业来添加LiveJobMatches吗?我应该像下面的答案那样运行两个调度程序吗?谢谢。您可以这样做,但是,是什么阻止您将Cron作业添加到现有计划中呢?