Exemple #1
0
 public XxlJobExecutor(XxlJobOptions options,
                       IRestClient httpClient,
                       ILogger <XxlJobExecutor> logger)
 {
     _options    = options;
     _httpClient = httpClient;
     _logger     = logger;
 }
 public XxlJobMiddleware(ILogger <XxlJobMiddleware> logger,
                         XxlJobOptions options,
                         XxlJobRouteTable routeTable,
                         RequestDelegate next)
 {
     _logger     = logger;
     _options    = options;
     _routeTable = routeTable;
     _next       = next;
 }
        public static IServiceCollection AddXxlJob(this IServiceCollection services, Action <XxlJobOptions> action)
        {
            var options = new XxlJobOptions();

            action?.Invoke(options);
            services.AddSingleton(options);

            // xxl job service
            services.Add(new ServiceDescriptor(typeof(IXxlJobExecutor), typeof(XxlJobExecutor), ServiceLifetime.Singleton));
            services.AddSingleton <IProcessor, ExecutorRegistryProcessor>();
            services.AddTransient <IRestClient>(p =>
            {
                var conf = p.GetRequiredService <XxlJobOptions>();
                return(new RestClient(conf.AdminUrl));
            });
            services.AddSingleton <IJobDispatcher, JobDispatcher>();
            services.AddSingleton <IJobSender, XxlJobSender>();
            services.AddTransient <XxlJobQueue>();
            services.AddSingleton <IJobLoggerStore, LiteDBLoggerStore>();

            // route
            var routeTable = new XxlJobRouteTable();

            services.AddSingleton(routeTable);
            foreach (var route in routeTable)
            {
                services.AddScoped(route.TypeController);
            }

            // host service
            services.AddHostedService <XxlJobBackgroundService>();

            // job handler
            services.Add(new ServiceDescriptor(typeof(IXxlJobServicesProvider <>), typeof(XxlJobServicesProvider <>), ServiceLifetime.Singleton));
            services.ScanDynamicJobExecutorHandler(options.HandlerServiceLifetime);
            return(services);
        }