-
Notifications
You must be signed in to change notification settings - Fork 4
/
JobScheduler.cs
39 lines (36 loc) · 1.22 KB
/
JobScheduler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
namespace TimeTriggerService
{
public class JobScheduler
{
private IScheduler scheduler;
private IEnumerable<IJob> jobs;
public JobScheduler(IScheduler schedulerParam, IEnumerable<IJob> jobsParam)
{
this.scheduler = schedulerParam;
this.jobs = jobsParam;
}
public IScheduler Start()
{
foreach (var job in jobs)
{
var task = job.GetType();
var jobInfo = Attribute.GetCustomAttribute(task, typeof(JobInfoAttribute)) as JobInfoAttribute;
var jobName = jobInfo == null ? task.Name : jobInfo.Name;
var jobDetail = new JobDetailImpl(jobName, null, task);
var trigger =
TriggerBuilder.Create()
.WithIdentity($"{jobName}Trigger", null)
.WithCronSchedule(jobInfo.CronExpression)
.ForJob(jobDetail)
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
}
scheduler.Start();
return scheduler;
}
}
}