Quantz.net database-based task scheduling management (Only. Jobs ),
I. Preface:
The advantages and disadvantages of various scheduling components will not be discussed here. quantz.net is used because it can execute tasks in seconds.
The Only. Jobs project stores Jobs in the database and starts a special Job management task to schedule the scheduling of each Job cyclically.
Projects are divided into the Web management system, Service Running end, and data provision logic processing layer.
SqlSugar 4.1.1.5 is used for data access. Currently, MySql and SqlServer are supported.
Use quantz.net + Topshelf to install windows Services.
2. project structure:
One-to-one introduction:
Only. Jobs. Items is the created test Job project.
Only. Jobs is a scheduling service project that integrates Topshelf and can be installed as a windows service.
Only. Jobs. Core is the project's Core layer that provides external data operations.
Only. Jobs. Web is the scheduling task management and status monitoring management system.
Let's take a look at the overall effect.
Run the Only. Jobs. Web project:
In the task management module, you can stop and enable jobs.
Introduction to three-Table Structure
BackgroundJob is the task information storage table
BackgroundJobLog is the task execution trace log table
IV. Implementation
Use the task management module to configure the project where the developed Job is located and store it in the BackgroundJob table.
Create a ManagerJob class in the Only. Jobs project and manage Job scheduling tasks.
1 [DisallowConcurrentExecution] 2 public class ManagerJob: IJob 3 {4 private readonly ILog _ logger = LogManager. getLogger (typeof (ManagerJob); 5 6 public void Execute (IJobExecutionContext context) 7 {8 Version Ver = System. reflection. assembly. getExecutingAssembly (). getName (). version; 9 _ logger. infoFormat ("ManagerJob Execute begin Ver. "+ Ver. toString (); 10 try11 {12 new QuartzManager (). jobScheduler (context. schedger); 13 _ logger. infoFormat ("ManagerJob Executing... "); 14} 15 catch (Exception ex) 16 {17 JobExecutionException e2 = new JobExecutionException (ex); 18 e2.RefireImmediately = true; 19} 20 finally21 {22 _ logger. infoFormat ("ManagerJob Execute end"); 23} 24} 25}View Code
JobScheduler method:
1 /// <summary> 2 // Job status Control 3 /// </summary> 4 /// <param name = "Scheduler"> </param> 5 public void JobScheduler (IScheduler Scheduler) 6 {7 List <BackgroundJobInfo> list = new BackgroundJobService (). geAllowScheduleJobInfoList (); 8 if (list! = Null & list. count> 0) 9 {10 foreach (BackgroundJobInfo jobInfo in list) 11 {12 JobKey jobKey = new JobKey (jobInfo. backgroundJobId. toString (), jobInfo. backgroundJobId. toString () + "Group"); 13 if (Scheduler. checkExists (jobKey) = false) 14 {15 if (jobInfo. state = 1 | jobInfo. state = 3) 16 {17 ScheduleJob (schedjob, jobInfo); 18 if (schedjob. checkExists (jobKey) = false) 19 {20 new BackgroundJobService (). updateBackgroundJobState (jobInfo. backgroundJobId, 0); 21} 22 else23 {24 new BackgroundJobService (). updateBackgroundJobState (jobInfo. backgroundJobId, 1); 25} 26} 27 else if (jobInfo. state = 5) 28 {29 new BackgroundJobService (). updateBackgroundJobState (jobInfo. backgroundJobId, 0); 30} 31} 32 else33 {34 if (jobInfo. state = 5) 35 {36 Scheduler. deleteJob (jobKey); 37 new BackgroundJobService (). updateBackgroundJobState (jobInfo. backgroundJobId, 0); 38} 39 else if (jobInfo. state = 3) 40 {41 new BackgroundJobService (). updateBackgroundJobState (jobInfo. backgroundJobId, 1); 42} 43} 44} 45} 46}View Code
Others are simply not described here.
Next, let's take a look at the running effect:
Start the Only. Jobs Project
It's just a tool project, although it's still a brick.
Code uploaded to github
Https://github.com/mamingbo/Only.Jobs welcome to download for use.