public BackgroundJobServer(
            [NotNull] BackgroundJobServerOptions options,
            [NotNull] JobStorage storage,
            [NotNull] IEnumerable <IBackgroundProcess> additionalProcesses,
            [CanBeNull] IJobFilterProvider filterProvider,
            [CanBeNull] JobActivator activator,
            [CanBeNull] IBackgroundJobFactory factory,
            [CanBeNull] IBackgroundJobPerformer performer,
            [CanBeNull] IBackgroundJobStateChanger stateChanger)
        {
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (additionalProcesses == null)
            {
                throw new ArgumentNullException(nameof(additionalProcesses));
            }

            _options = options;

            var processes = new List <IBackgroundProcessDispatcherBuilder>();

            processes.AddRange(GetRequiredProcesses(filterProvider, activator, factory, performer, stateChanger));
            processes.AddRange(additionalProcesses.Select(x => x.UseBackgroundPool(1)));

            var properties = new Dictionary <string, object>
            {
                { "Queues", options.Queues },
                { "WorkerCount", options.WorkerCount }
            };

            _logger.Info($"Starting Hangfire Server using job storage: '{storage}'");

            storage.WriteOptionsToLog(_logger);

            _logger.Info("Using the following options for Hangfire Server:\r\n" +
                         $"    Worker count: {options.WorkerCount}\r\n" +
                         $"    Listening queues: {String.Join(", ", options.Queues.Select(x => "'" + x + "'"))}\r\n" +
                         $"    Shutdown timeout: {options.ShutdownTimeout}\r\n" +
                         $"    Schedule polling interval: {options.SchedulePollingInterval}");

            _processingServer = new BackgroundProcessingServer(
                storage,
                processes,
                properties,
                GetProcessingServerOptions());
        }
Exemplo n.º 2
0
 public Worker(
     [NotNull] IEnumerable<string> queues,
     [NotNull] IBackgroundJobPerformer performer, 
     [NotNull] IBackgroundJobStateChanger stateChanger)
 {
     if (queues == null) throw new ArgumentNullException("queues");
     if (performer == null) throw new ArgumentNullException("performer");
     if (stateChanger == null) throw new ArgumentNullException("stateChanger");
     
     _queues = queues.ToArray();
     _performer = performer;
     _stateChanger = stateChanger;
     _workerId = Guid.NewGuid().ToString();
 }
        public ContinuationsSupportAttribute(
            [NotNull] HashSet<string> knownFinalStates, 
            [NotNull] IBackgroundJobStateChanger stateChanger)
        {
            if (knownFinalStates == null) throw new ArgumentNullException("knownFinalStates");
            if (stateChanger == null) throw new ArgumentNullException("stateChanger");

            _knownFinalStates = knownFinalStates;
            _stateChanger = stateChanger;

            // Ensure this filter is the last filter in the chain to start
            // continuations on the last candidate state only.
            Order = 1000;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DelayedJobScheduler"/>
        /// class with a specified polling interval and given state changer.
        /// </summary>
        /// <param name="pollingDelay">Delay between scheduler runs.</param>
        /// <param name="stateChanger">State changer to use for background jobs.</param>
        /// <param name="queues">The queues that the scheduler will create jobs for.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stateChanger"/> is null.</exception>
        public DelayedJobScheduler(TimeSpan pollingDelay, [NotNull] IBackgroundJobStateChanger stateChanger, params string[] queues)
        {
            if (stateChanger == null)
            {
                throw new ArgumentNullException(nameof(stateChanger));
            }
            if (queues == null)
            {
                throw new ArgumentNullException(nameof(queues));
            }

            _stateChanger = stateChanger;
            _queues       = queues;
            _pollingDelay = pollingDelay;
        }
        public BackgroundJobServerHostedService(
            [NotNull] JobStorage storage,
            [NotNull] BackgroundJobServerOptions options,
            [NotNull] IEnumerable <IBackgroundProcess> additionalProcesses,
            [CanBeNull] IBackgroundJobFactory factory,
            [CanBeNull] IBackgroundJobPerformer performer,
            [CanBeNull] IBackgroundJobStateChanger stateChanger)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
            _storage = storage ?? throw new ArgumentNullException(nameof(storage));

            _additionalProcesses = additionalProcesses;

            _factory      = factory;
            _performer    = performer;
            _stateChanger = stateChanger;
        }
Exemplo n.º 6
0
        public static (BackgroundJobServer server, IRecurringJobManager recurringJobManager, IBackgroundJobClient backgroundJobClient) StartHangfireServer(
            BackgroundJobServerOptions options,
            string connectionString,
            IApplicationLifetime applicationLifetime,
            IJobFilterProvider jobFilters,
            JobActivator jobActivator,
            IBackgroundJobFactory backgroundJobFactory,
            IBackgroundJobPerformer backgroundJobPerformer,
            IBackgroundJobStateChanger backgroundJobStateChanger,
            IBackgroundProcess[] additionalProcesses
            )
        {
            JobStorage storage;

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                storage = new MemoryStorage();
            }
            else if (ConnectionStringHelper.IsSQLite(connectionString))
            {
                storage = new SQLiteStorage(connectionString);
            }
            else
            {
                storage = new SqlServerStorage(connectionString);
            }

            var server = new BackgroundJobServer(options, storage, additionalProcesses,
                                                 options.FilterProvider ?? jobFilters,
                                                 options.Activator ?? jobActivator,
                                                 backgroundJobFactory,
                                                 backgroundJobPerformer,
                                                 backgroundJobStateChanger);

            applicationLifetime.ApplicationStopping.Register(() => server.SendStop());
            applicationLifetime.ApplicationStopped.Register(() => server.Dispose());

            var recurringJobManager = new RecurringJobManager(storage, backgroundJobFactory);

            var backgroundJobClient = new BackgroundJobClient(storage, backgroundJobFactory, backgroundJobStateChanger);

            return(server, recurringJobManager, backgroundJobClient);
        }
Exemplo n.º 7
0
        public ContinuationsSupportAttribute(
            [NotNull] HashSet <string> knownFinalStates,
            [NotNull] IBackgroundJobStateChanger stateChanger)
        {
            if (knownFinalStates == null)
            {
                throw new ArgumentNullException(nameof(knownFinalStates));
            }
            if (stateChanger == null)
            {
                throw new ArgumentNullException(nameof(stateChanger));
            }

            _knownFinalStates = knownFinalStates;
            _stateChanger     = stateChanger;

            // Ensure this filter is the last filter in the chain to start
            // continuations on the last candidate state only.
            Order = 1000;
        }
Exemplo n.º 8
0
        public WorkerTask(string[] queues, IAsyncBackgroundJobPerformer performer, IBackgroundJobStateChanger stateChanger)
        {
            if (queues == null || queues.Length == 0)
            {
                throw new ArgumentNullException(nameof(queues));
            }
            if (performer == null)
            {
                throw new ArgumentNullException(nameof(performer));
            }
            if (stateChanger == null)
            {
                throw new ArgumentNullException(nameof(stateChanger));
            }

            _queues       = queues;
            _performer    = performer;
            _stateChanger = stateChanger;
            _workerId     = Guid.NewGuid().ToString();
        }
Exemplo n.º 9
0
        private IEnumerable <IBackgroundProcessDispatcherBuilder> GetRequiredProcesses(
            [CanBeNull] IJobFilterProvider filterProvider,
            [CanBeNull] JobActivator activator,
            [CanBeNull] IBackgroundJobFactory factory,
            [CanBeNull] IBackgroundJobPerformer performer,
            [CanBeNull] IBackgroundJobStateChanger stateChanger)
        {
            var processes        = new List <IBackgroundProcessDispatcherBuilder>();
            var timeZoneResolver = _options.TimeZoneResolver ?? new DefaultTimeZoneResolver();

            if (factory == null && performer == null && stateChanger == null)
            {
                filterProvider = filterProvider ?? _options.FilterProvider ?? JobFilterProviders.Providers;
                activator      = activator ?? _options.Activator ?? JobActivator.Current;

                factory      = new BackgroundJobFactory(filterProvider);
                performer    = new BackgroundJobPerformer(filterProvider, activator, _options.TaskScheduler);
                stateChanger = new BackgroundJobStateChanger(filterProvider);
            }
            else
            {
                if (factory == null)
                {
                    throw new ArgumentNullException(nameof(factory));
                }
                if (performer == null)
                {
                    throw new ArgumentNullException(nameof(performer));
                }
                if (stateChanger == null)
                {
                    throw new ArgumentNullException(nameof(stateChanger));
                }
            }

            processes.Add(new Worker(_options.Queues, performer, stateChanger).UseBackgroundPool(_options.WorkerCount));
            processes.Add(new DelayedJobScheduler(_options.SchedulePollingInterval, stateChanger).UseBackgroundPool(1));
            processes.Add(new RecurringJobScheduler(factory, _options.SchedulePollingInterval, timeZoneResolver).UseBackgroundPool(1));

            return(processes);
        }
        internal static bool GetInternalServices(
            IServiceProvider provider,
            out IBackgroundJobFactory factory,
            out IBackgroundJobStateChanger stateChanger,
            out IBackgroundJobPerformer performer)
        {
            factory      = provider.GetService <IBackgroundJobFactory>();
            performer    = provider.GetService <IBackgroundJobPerformer>();
            stateChanger = provider.GetService <IBackgroundJobStateChanger>();

            if (factory != null && performer != null && stateChanger != null)
            {
                return(true);
            }

            factory      = null;
            performer    = null;
            stateChanger = null;

            return(false);
        }
        public SpecifyQueueBackgroundJobClient(
            [NotNull] JobStorage storage,
            [NotNull] IBackgroundJobFactory factory,
            [NotNull] IBackgroundJobStateChanger stateChanger)
        {
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (stateChanger == null)
            {
                throw new ArgumentNullException(nameof(stateChanger));
            }

            _storage      = storage;
            _stateChanger = stateChanger;
            _factory      = factory;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BackgroundJobClient"/> class
        /// with the specified storage, background job factory and state changer.
        /// </summary>
        ///
        /// <param name="storage">Job storage to use for background jobs.</param>
        /// <param name="factory">Factory to create background jobs.</param>
        /// <param name="stateChanger">State changer to change states of background jobs.</param>
        ///
        /// <exception cref="ArgumentNullException"><paramref name="storage"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="factory"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="stateChanger"/> is null.</exception>
        public BackgroundJobClient(
            [NotNull] JobStorage storage,
            [NotNull] IBackgroundJobFactory factory,
            [NotNull] IBackgroundJobStateChanger stateChanger)
        {
            if (storage == null)
            {
                throw new ArgumentNullException("storage");
            }
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (stateChanger == null)
            {
                throw new ArgumentNullException("stateChanger");
            }

            _storage      = storage;
            _stateChanger = stateChanger;
            _factory      = factory;
        }
Exemplo n.º 13
0
        public HangfireServer(
            BackgroundJobServerOptions options,
            JobStorage jobStorage,
            IEnumerable <IBackgroundProcess> backgroundProcesses,
            IBackgroundJobFactory jobFactory,
            IBackgroundJobPerformer jobPerformer,
            IBackgroundJobStateChanger jobStateChanger,
            ILogger <HangfireServer> logger,
            IJobFilterProvider jobFilterProvider = default,
            JobActivator activator = default
            )
        {
            _options             = options;
            _jobStorage          = jobStorage;
            _backgroundProcesses = backgroundProcesses;
            _jobFactory          = jobFactory;
            _jobPerformer        = jobPerformer;
            _jobStateChanger     = jobStateChanger;
            _jobFilterProvider   = options.FilterProvider ?? jobFilterProvider;
            _activator           = options.Activator ?? activator;

            _logger = logger;
        }
Exemplo n.º 14
0
        private IEnumerable <IBackgroundProcess> GetRequiredProcesses(
            [NotNull] IJobFilterProvider filterProvider,
            [NotNull] JobActivator activator,
            [CanBeNull] IBackgroundJobFactory factory,
            [CanBeNull] IBackgroundJobPerformer performer,
            [CanBeNull] IBackgroundJobStateChanger stateChanger)
        {
            var processes = new List <IBackgroundProcess>();

            factory      = factory ?? new BackgroundJobFactory(filterProvider);
            performer    = performer ?? new BackgroundJobPerformer(filterProvider, activator);
            stateChanger = stateChanger ?? new BackgroundJobStateChanger(filterProvider);

            for (var i = 0; i < _options.WorkerCount; i++)
            {
                processes.Add(new Worker(_options.Queues, performer, stateChanger));
            }

            processes.Add(new DelayedJobScheduler(_options.SchedulePollingInterval, stateChanger));
            processes.Add(new RecurringJobScheduler(factory));

            return(processes);
        }
Exemplo n.º 15
0
        public Worker(
            [NotNull] IEnumerable <string> queues,
            [NotNull] IBackgroundJobPerformer performer,
            [NotNull] IBackgroundJobStateChanger stateChanger)
        {
            if (queues == null)
            {
                throw new ArgumentNullException(nameof(queues));
            }
            if (performer == null)
            {
                throw new ArgumentNullException(nameof(performer));
            }
            if (stateChanger == null)
            {
                throw new ArgumentNullException(nameof(stateChanger));
            }

            _queues       = queues.ToArray();
            _performer    = performer;
            _stateChanger = stateChanger;
            _workerId     = Guid.NewGuid().ToString();
        }
Exemplo n.º 16
0
 public AtomRunningStateElectionFilter(IBackgroundJobStateChanger stateChanger)
 {
     _stateChanger = stateChanger ?? throw new ArgumentNullException(nameof(stateChanger));
     Order         = 900;
 }
 public SubAtomStateElectionFilter(HashSet <string> knownFinalStates, IBackgroundJobStateChanger stateChanger)
 {
     _knownFinalStates = knownFinalStates ?? throw new ArgumentNullException(nameof(knownFinalStates));
     _stateChanger     = stateChanger ?? throw new ArgumentNullException(nameof(stateChanger));
     Order             = 800;
 }
Exemplo n.º 18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BackgroundJobClient"/> class
 /// with the specified storage, background job factory and state changer.
 /// </summary>
 /// 
 /// <param name="storage">Job storage to use for background jobs.</param>
 /// <param name="factory">Factory to create background jobs.</param>
 /// <param name="stateChanger">State changer to change states of background jobs.</param>
 /// 
 /// <exception cref="ArgumentNullException"><paramref name="storage"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="factory"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="stateChanger"/> is null.</exception>
 public BackgroundJobClient(
     [NotNull] JobStorage storage,
     [NotNull] IBackgroundJobFactory factory,
     [NotNull] IBackgroundJobStateChanger stateChanger)
 {
     if (storage == null) throw new ArgumentNullException("storage");
     if (factory == null) throw new ArgumentNullException("factory");
     if (stateChanger == null) throw new ArgumentNullException("stateChanger");
     
     _storage = storage;
     _stateChanger = stateChanger;
     _factory = factory;
 }
Exemplo n.º 19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DelayedJobScheduler"/>
        /// class with a specified polling interval and given state changer.
        /// </summary>
        /// <param name="pollingDelay">Delay between scheduler runs.</param>
        /// <param name="stateChanger">State changer to use for background jobs.</param>
        /// <param name="schedulerEvent">Event to awake scheuler</param>
        ///
        /// <exception cref="ArgumentNullException"><paramref name="stateChanger"/> is null.</exception>
        public DelayedJobScheduler(TimeSpan pollingDelay, AutoResetEvent schedulerEvent, [NotNull] IBackgroundJobStateChanger stateChanger)
        {
            if (stateChanger == null)
            {
                throw new ArgumentNullException(nameof(stateChanger));
            }

            _stateChanger   = stateChanger;
            _pollingDelay   = pollingDelay;
            _schedulerEvent = schedulerEvent;
        }
 public ApplicationInsightsBackgroundJobStateChanger([NotNull] IBackgroundJobStateChanger inner)
 {
     _inner = inner ?? throw new ArgumentNullException(nameof(inner));
 }
Exemplo n.º 21
0
 public CustomBackgroundJobStateChanger([NotNull] IBackgroundJobStateChanger inner)
 {
     _inner = inner ?? throw new ArgumentNullException(nameof(inner));
 }
 public ContinuationsSupportAttribute(
     [NotNull] HashSet <string> knownFinalStates,
     [NotNull] IBackgroundJobStateChanger stateChanger)
     : this(false, knownFinalStates, stateChanger)
 {
 }
Exemplo n.º 23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DelayedJobScheduler"/>
        /// class with a specified polling interval and given state changer.
        /// </summary>
        /// <param name="pollingDelay">Delay between scheduler runs.</param>
        /// <param name="stateChanger">State changer to use for background jobs.</param>
        /// 
        /// <exception cref="ArgumentNullException"><paramref name="stateChanger"/> is null.</exception>
        public DelayedJobScheduler(TimeSpan pollingDelay, [NotNull] IBackgroundJobStateChanger stateChanger)
        {
            if (stateChanger == null) throw new ArgumentNullException("stateChanger");

            _stateChanger = stateChanger;
            _pollingDelay = pollingDelay;
        }
Exemplo n.º 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DelayedJobScheduler"/>
 /// class with a specified polling interval and given state changer.
 /// </summary>
 /// <param name="pollingDelay">Delay between scheduler runs.</param>
 /// <param name="stateChanger">State changer to use for background jobs.</param>
 /// <exception cref="ArgumentNullException"><paramref name="stateChanger"/> is null.</exception>
 public DelayedJobScheduler(TimeSpan pollingDelay, [NotNull] IBackgroundJobStateChanger stateChanger)
     : this(pollingDelay, stateChanger, EnqueuedState.DefaultQueue)
 {
 }