| | |
| | | |
| | | namespace WIDESEA_Core.QuartzJob |
| | | { |
| | | |
| | | /// <summary> |
| | | /// ä»»å¡è°åº¦ å¯å¨æå¡ |
| | | /// </summary> |
| | | public static class JobSetup |
| | | { |
| | | public static void AddJobSetup(this IServiceCollection services) |
| | | public static void AddJobSetup(this IServiceCollection services, Action<IServiceCollection> configureJobs = null) |
| | | { |
| | | if (services == null) throw new ArgumentNullException(nameof(services)); |
| | | |
| | | //å°JobFactory注å
¥å°æå¡ä¸ |
| | | // å°JobFactory注å
¥å°æå¡ä¸ |
| | | services.AddSingleton<IJobFactory, JobFactory>(); |
| | | //å°SchedulerCenterServer注å
¥å°æå¡ä¸ |
| | | // å°SchedulerCenterServer注å
¥å°æå¡ä¸ |
| | | services.AddSingleton<ISchedulerCenter, SchedulerCenterServer>(); |
| | | //任塿³¨å
¥ |
| | | |
| | | // 任塿³¨å
¥ |
| | | var baseType = typeof(IJob); |
| | | //è·åå½ååºç¨ç¨åºåçç¸å¯¹æç´¢è·¯å¾ |
| | | // è·åå½ååºç¨ç¨åºåçç¸å¯¹æç´¢è·¯å¾ |
| | | var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory; |
| | | //è·åæå®è·¯å¾ä¸çææç¨åºé |
| | | // è·åæå®è·¯å¾ä¸çææç¨åºé |
| | | var referencedAssemblies = System.IO.Directory.GetFiles(path, "WIDESEA_Tasks.dll").Select(Assembly.LoadFrom).ToArray(); |
| | | //è·åææå®ä¹çç±»å |
| | | // è·åææå®ä¹çç±»å |
| | | var types = referencedAssemblies |
| | | .SelectMany(a => a.DefinedTypes) |
| | | .Select(type => type.AsType()) |
| | | //çéåºç»§æ¿èªIJobçç±»å |
| | | // çéåºç»§æ¿èªIJobçç±»å |
| | | .Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray(); |
| | | //è·åææå®ç°äºIJobçç±»å |
| | | // è·åææå®ç°äºIJobçç±»å |
| | | var implementTypes = types.Where(x => x.IsClass).ToArray(); |
| | | //å°ææå®ç°äºIJobçç±»åæ³¨å
¥å°æå¡ä¸ |
| | | // å°ææå®ç°äºIJobçç±»åæ³¨å
¥å°æå¡ä¸ |
| | | foreach (var implementType in implementTypes) |
| | | { |
| | | services.AddTransient(implementType); |
| | | } |
| | | |
| | | // æ·»å 宿¶å¯å¨é
ç½® |
| | | services.AddTransient<StartupJob>(); |
| | | services.AddSingleton(new JobSchedule( |
| | | jobType: typeof(StartupJob), |
| | | cronExpression: "0 0 8 ? * *")); // æ¯å¤©ä¸å8ç¹æ§è¡ |
| | | } |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// å¯å¨ä»»å¡ |
| | | /// </summary> |
| | | public class StartupJob : IJob |
| | | { |
| | | private readonly ISchedulerCenter _schedulerCenter; |
| | | |
| | | public StartupJob(ISchedulerCenter schedulerCenter) |
| | | { |
| | | _schedulerCenter = schedulerCenter; |
| | | } |
| | | |
| | | public async Task Execute(IJobExecutionContext context) |
| | | { |
| | | // å¨è¿éå¯å¨ææéè¦å®æ¶æ§è¡çä»»å¡ |
| | | await _schedulerCenter.StartScheduleAsync(); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// ä»»å¡è°åº¦é
ç½® |
| | | /// </summary> |
| | | public class JobSchedule |
| | | { |
| | | public Type JobType { get; } |
| | | public string CronExpression { get; } |
| | | |
| | | public JobSchedule(Type jobType, string cronExpression) |
| | | { |
| | | JobType = jobType; |
| | | CronExpression = cronExpression; |
| | | } |
| | | } |
| | | } |