public virtual Task InitializeAsync()
 {
     // We create server so that we can test that jobs are executed properly.
     _server = CreateServer();
     // We create client to enqueue new jobs.
     _client = CreateClient();
     return(Task.CompletedTask);
 }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _processingServer = _factory != null && _performer != null && _stateChanger != null
#pragma warning disable 618
                ? new BackgroundJobServer(_options, _storage, _additionalProcesses, null, null, _factory, _performer, _stateChanger)
#pragma warning restore 618
                : new BackgroundJobServer(_options, _storage, _additionalProcesses);

            return(Task.CompletedTask);
        }
        public BackgroundTaskService(
            IBackgroundProcessingServer backgroundProcessingServer,
            IRecurringJobManager recurringJobManager,
            IBackgroundTaskRegistration backgroundTaskRegistration,
            IEventAggregator eventAggregator)
        {
            _recurringJobManager        = recurringJobManager;
            _backgroundProcessingServer = backgroundProcessingServer;
            _backgroundTaskRegistration = backgroundTaskRegistration;
            _eventAggregator            = eventAggregator;

            _eventAggregator.GetEvent <AfterTaskRegistrationChanged>()
            .Subscribe(() =>
            {
                Refresh();
            }, true);

            Refresh();
        }
        public virtual void ConfigureServices(TenantBuilderContext context, IServiceCollection services)
        {
            var hangfireServicesAdded = context.RootServiceProvider.GetService <IGlobalConfiguration>() != null;

            //configuration
            var configuration = new HangfireConfiguration();

            context.Configuration.Bind("Hangfire", configuration);
            ConfigureHangfire(context, configuration);
            services.AddSingleton(configuration);

            //dashboard options
            var dashboardOptions = context.RootServiceProvider.GetService <DashboardOptions>()?.Clone() ?? new DashboardOptions();

            ConfigureHangfireDashboard(context, dashboardOptions);
            services.AddSingleton(dashboardOptions);

            //background processing server
            IBackgroundProcessingServer processingServer = null;

            //Storage
            if (configuration.Enabled)
            {
                var storageDetails = HangfireJobStorage.GetJobStorage(configuration.ConnectionString, options => {
                    options.PrepareSchemaIfNecessary = false;
                    options.EnableHeavyMigrations    = false;
                    options.EnableLongPolling        = configuration.EnableLongPolling;
                    options.SchemaName = configuration.SchemaName;
                });

                JobStorage storage = storageDetails.JobStorage;
                configuration.ExistingConnection = storageDetails.ExistingConnection;

                services.AddSingleton(storage);
                services.AddHangfireServerServices();

                Func <IServiceProvider, Action <IBackgroundProcessingServer> > processingServerSetter = ((sp) => (x) => { processingServer = x; });
                services.AddSingleton(processingServerSetter);
                Func <IServiceProvider, IBackgroundProcessingServer> processingServerAccessor = ((sp) => processingServer);
                services.AddSingleton(processingServerAccessor);

                var backgroundServerOptions = context.RootServiceProvider.GetService <BackgroundJobServerOptions>()?.Clone() ?? new BackgroundJobServerOptions();

                backgroundServerOptions.ServerName     = configuration.ServerName ?? backgroundServerOptions.ServerName;
                backgroundServerOptions.Activator      = new MultiTenantJobActivator(context.RootServiceProvider.GetRequiredService <MultitenantContainer>(), context.TenantId);
                backgroundServerOptions.FilterProvider = context.RootServiceProvider.GetService <IJobFilterProvider>() ?? new JobFilterCollection();

                ConfigureHangfireServer(context, backgroundServerOptions);

                services.AddSingleton(backgroundServerOptions);
            }
            else
            {
                if (hangfireServicesAdded)
                {
                    services.AddSingleton((JobStorage) new NoopJobStorage());
                    services.AddHangfireServerServices();
                }
            }

            //background processes
            ConfigureBackgroundProcesses(context, services);
        }