wankeda
2025-04-07 4da42c12efde190b208f18b7dd51d3e89986837d
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_Core/Quartzjob/JobSetup.cs
@@ -10,13 +10,12 @@
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));
@@ -24,6 +23,7 @@
            services.AddSingleton<IJobFactory, JobFactory>();
            //将SchedulerCenterServer注入到服务中
            services.AddSingleton<ISchedulerCenter, SchedulerCenterServer>();
            //任务注入
            var baseType = typeof(IJob);
            //获取当前应用程序域的相对搜索路径
@@ -43,6 +43,46 @@
            {
                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;
        }
    }
}