public async Task StartAsync(CancellationToken cancellationToken) { NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" } }; StdSchedulerFactory factory = new StdSchedulerFactory(props); _scheduler = await factory.GetScheduler(); var components = BootstrapClassLoader.LoadComponents(); foreach (var component in components) { if (!component.ServiceType.IsAssignableFrom(typeof(IJob))) { continue; } var implementationType = component.ImplementationType; var method = implementationType.GetMethod("Execute", new[] { typeof(IJobExecutionContext) }); if (method == null) { continue; } var scheduled = method.GetCustomAttribute <ScheduledAttribute>(); if (scheduled == null) { continue; } // define the job and tie it to our HelloJob class IJobDetail job = JobBuilder.Create(implementationType) .WithIdentity($"job{implementationType.FullName}", "group1") .Build(); // Trigger the job to run now, and then repeat every 10 seconds ITrigger trigger = TriggerBuilder.Create() .WithIdentity($"trigger{implementationType.FullName}", "group1") .WithCronSchedule(scheduled.Cron) .Build(); // Tell quartz to schedule the job using our trigger await _scheduler.ScheduleJob(job, trigger); } if (!_scheduler.IsStarted || _scheduler.IsShutdown) { await _scheduler.Start(); } // and start it off }
static ServiceCollectionServiceExtensions() { components = BootstrapClassLoader.LoadComponents(); // serilog 设置 Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .WriteTo.File("logs/log.txt", restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error, rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true) .CreateLogger(); }