private static void ValidOptions(AixScheduleTaskOptions options) { if (options == null) { throw new Exception("请配置options参数"); } if (string.IsNullOrEmpty(options.Master)) { throw new Exception("请配置options.Master"); } if (options.PreReadSecond <= 0) { throw new Exception("配置options.PreReadSecond 非法"); } if (options.PreReadSecond < 5) { options.PreReadSecond = 5; } if (options.PreReadSecond > 30) { options.PreReadSecond = 30; } if (options.RetryIntervalMillisecond < 10) { options.RetryIntervalMillisecond = 10; } }
public ScheduleTaskService(ILogger <ScheduleTaskService> logger, AixScheduleTaskOptions options, IScheduleTaskLifetime scheduleTaskLifetime, IAixScheduleTaskRepository aixScheduleTaskRepository, IAixDistributionLockRepository aixDistributionLockRepository, IScheduleTaskDistributedLock scheduleTaskDistributedLock, IAixScheduleTaskLogRepository aixScheduleTaskLogRepository, MyMultithreadTaskExecutor taskExecutor, ScheduleTaskExecutor scheduleTaskExecutor, ExpireLogExecutor expireLogExecutor, ErrorTaskExecutor errorTaskExecutor ) { _logger = logger; _options = options; _scheduleTaskLifetime = scheduleTaskLifetime; PreReadSecond = _options.PreReadSecond; _aixScheduleTaskRepository = aixScheduleTaskRepository; _aixDistributionLockRepository = aixDistributionLockRepository; _scheduleTaskDistributedLock = scheduleTaskDistributedLock; _aixScheduleTaskLogRepository = aixScheduleTaskLogRepository; _taskExecutor = taskExecutor; _scheduleTaskExecutor = scheduleTaskExecutor; _expireLogExecutor = expireLogExecutor; _errorTaskExecutor = errorTaskExecutor; }
public static IServiceCollection AddScheduleTask(this IServiceCollection services, Action <AixScheduleTaskOptions> setupAction) { Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true; Dapper.SqlMapper.Settings.CommandTimeout = 30;//秒 //ORMSettings.SetConnectionFactory(new DBConnectionFactory()); var options = new AixScheduleTaskOptions(); if (setupAction != null) { setupAction(options); } ValidOptions(options); services.AddSingleton(options); if (options.DBType == 1) { services.AddSingleton <IAixScheduleTaskRepository, AixScheduleTaskSqlServerRepository>(); services.AddSingleton <IAixDistributionLockRepository, AixDistributionLockSqlServerRepository>(); services.AddSingleton <IAixScheduleTaskLogRepository, AixScheduleTaskLogSqlServerRepository>(); } else if (options.DBType == 2) { services.AddSingleton <IAixScheduleTaskRepository, AixScheduleTaskMySqlRepository>(); services.AddSingleton <IAixDistributionLockRepository, AixDistributionLockMySqlRepository>(); services.AddSingleton <IAixScheduleTaskLogRepository, AixScheduleTaskLogMySqlRepository>(); } else { throw new Exception("请配置DBType,1=SqlServer(默认值) 2=Mysql "); } if (options.ClusterType == 1) { services.AddSingleton <IScheduleTaskDistributedLock, ScheduleTaskDistributedLockEmptyImpl>(); } else { services.AddSingleton <IScheduleTaskDistributedLock, ScheduleTaskDistributedLockDBImpl>(); } services.AddSingleton <IScheduleTaskService, ScheduleTaskService>(); services.AddSingleton <IScheduleTaskAdminService, ScheduleTaskAdminService>(); services.AddSingleton <IScheduleTaskLifetime, ScheduleTaskLifetime>(); services.AddSingleton <ScheduleTaskExecutor>(); services.AddSingleton <ExpireLogExecutor>(); services.AddSingleton <ErrorTaskExecutor>(); services.AddAddMultithreadExecutor(options.ConsumerThreadCount); return(services); }