Example #1
0
        public Worker(
            [NotNull] WorkerContext context,
            [NotNull] JobStorage storage,
            [NotNull] IJobPerformanceProcess process,
            [NotNull] IStateMachineFactory stateMachineFactory)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage");
            }
            if (process == null)
            {
                throw new ArgumentNullException("process");
            }
            if (stateMachineFactory == null)
            {
                throw new ArgumentNullException("stateMachineFactory");
            }

            _context             = context;
            _storage             = storage;
            _process             = process;
            _stateMachineFactory = stateMachineFactory;
        }
Example #2
0
 public Worker(
     [NotNull] WorkerContext context,
     [NotNull] JobStorage storage, 
     [NotNull] IJobPerformanceProcess process, 
     [NotNull] IStateMachineFactory stateMachineFactory)
 {
     if (context == null) throw new ArgumentNullException("context");
     if (storage == null) throw new ArgumentNullException("storage");
     if (process == null) throw new ArgumentNullException("process");
     if (stateMachineFactory == null) throw new ArgumentNullException("stateMachineFactory");
     
     _context = context;
     _storage = storage;
     _process = process;
     _stateMachineFactory = stateMachineFactory;
 }
Example #3
0
        internal SharedWorkerContext(
            string serverId,
            string[] queues,
            JobStorage storage,
            IJobPerformanceProcess performanceProcess,
            JobActivator activator,
            IStateMachineFactory stateMachineFactory)
        {
            if (serverId == null)
            {
                throw new ArgumentNullException("serverId");
            }
            if (queues == null)
            {
                throw new ArgumentNullException("queues");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage");
            }
            if (performanceProcess == null)
            {
                throw new ArgumentNullException("performanceProcess");
            }
            if (activator == null)
            {
                throw new ArgumentNullException("activator");
            }
            if (stateMachineFactory == null)
            {
                throw new ArgumentNullException("stateMachineFactory");
            }

            ServerId            = serverId;
            Queues              = queues;
            Storage             = storage;
            PerformanceProcess  = performanceProcess;
            Activator           = activator;
            StateMachineFactory = stateMachineFactory;
        }
        internal SharedWorkerContext(
            string serverId,
            string[] queues,
            JobStorage storage,
            IJobPerformanceProcess performanceProcess,
            JobActivator activator,
            IStateMachineFactory stateMachineFactory)
        {
            if (serverId == null) throw new ArgumentNullException("serverId");
            if (queues == null) throw new ArgumentNullException("queues");
            if (storage == null) throw new ArgumentNullException("storage");
            if (performanceProcess == null) throw new ArgumentNullException("performanceProcess");
            if (activator == null) throw new ArgumentNullException("activator");
            if (stateMachineFactory == null) throw new ArgumentNullException("stateMachineFactory");

            ServerId = serverId;
            Queues = queues;
            Storage = storage;
            PerformanceProcess = performanceProcess;
            Activator = activator;
            StateMachineFactory = stateMachineFactory;
        }
Example #5
0
        private void ProcessJob(
            string jobId,
            IStorageConnection connection, 
            IJobPerformanceProcess process,
            CancellationToken shutdownToken)
        {
            var stateMachine = _context.StateMachineFactory.Create(connection);
            var processingState = new ProcessingState(_context.ServerId, _context.WorkerNumber);

            if (!stateMachine.TryToChangeState(
                jobId,
                processingState,
                new[] { EnqueuedState.StateName, ProcessingState.StateName }))
            {
                return;
            }

            // Checkpoint #3. Job is in the Processing state. However, there are
            // no guarantees that it was performed. We need to re-queue it even
            // it was performed to guarantee that it was performed AT LEAST once.
            // It will be re-queued after the JobTimeout was expired.

            IState state;

            try
            {
                var jobData = connection.GetJobData(jobId);
                jobData.EnsureLoaded();

                var cancellationToken = new ServerJobCancellationToken(
                    jobId, connection, _context, shutdownToken);

                var performContext = new PerformContext(
                    _context, connection, jobId, jobData.Job, jobData.CreatedAt, cancellationToken);

                var latency = (DateTime.UtcNow - jobData.CreatedAt).TotalMilliseconds;
                var duration = Stopwatch.StartNew();

                process.Run(performContext, jobData.Job);
                duration.Stop();

                state = new SucceededState((long) latency, duration.ElapsedMilliseconds);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (JobPerformanceException ex)
            {
                state = new FailedState(ex.InnerException)
                {
                    Reason = ex.Message
                };
            }
            catch (Exception ex)
            {
                state = new FailedState(ex)
                {
                    Reason = "Internal HangFire Server exception occurred. Please, report it to HangFire developers."
                };
            }

            // Ignore return value, because we should not do
            // anything when current state is not Processing.
            stateMachine.TryToChangeState(jobId, state, new[] { ProcessingState.StateName });
        }
Example #6
0
        private void ProcessJob(
            string jobId,
            IStorageConnection connection,
            IJobPerformanceProcess process,
            CancellationToken shutdownToken)
        {
            var stateMachine    = _context.StateMachineFactory.Create(connection);
            var processingState = new ProcessingState(_context.ServerId, _context.WorkerNumber);

            if (!stateMachine.TryToChangeState(
                    jobId,
                    processingState,
                    new[] { EnqueuedState.StateName, ProcessingState.StateName }))
            {
                return;
            }

            // Checkpoint #3. Job is in the Processing state. However, there are
            // no guarantees that it was performed. We need to re-queue it even
            // it was performed to guarantee that it was performed AT LEAST once.
            // It will be re-queued after the JobTimeout was expired.

            IState state;

            try
            {
                var jobData = connection.GetJobData(jobId);
                jobData.EnsureLoaded();

                var cancellationToken = new ServerJobCancellationToken(
                    jobId, connection, _context, shutdownToken);

                var performContext = new PerformContext(
                    _context, connection, jobId, jobData.Job, jobData.CreatedAt, cancellationToken);

                var latency  = (DateTime.UtcNow - jobData.CreatedAt).TotalMilliseconds;
                var duration = Stopwatch.StartNew();

                process.Run(performContext, jobData.Job);
                duration.Stop();

                state = new SucceededState((long)latency, duration.ElapsedMilliseconds);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (JobPerformanceException ex)
            {
                state = new FailedState(ex.InnerException)
                {
                    Reason = ex.Message
                };
            }
            catch (Exception ex)
            {
                state = new FailedState(ex)
                {
                    Reason = "Internal Hangfire Server exception occurred. Please, report it to Hangfire developers."
                };
            }

            // Ignore return value, because we should not do
            // anything when current state is not Processing.
            stateMachine.TryToChangeState(jobId, state, new[] { ProcessingState.StateName });
        }