Example #1
0
        /// <summary>
        /// Process jobs
        /// </summary>
        /// <returns></returns>
        private async Task ProcessAsync(JobProcessingInstructionModel jobProcessInstruction,
                                        CancellationToken ct)
        {
            try {
                // Stop worker heartbeat to start the job heartbeat process
                _heartbeatTimer.Change(-1, -1); // Stop worker heartbeat

                _logger.Information("Worker: {WorkerId} processing job: {JobId}, mode: {ProcessMode}",
                                    WorkerId, jobProcessInstruction.Job.Id, jobProcessInstruction.ProcessMode);

                // Execute processor
                while (true)
                {
                    _jobProcess = null;
                    ct.ThrowIfCancellationRequested();
                    using (_jobProcess = new JobProcess(this, jobProcessInstruction,
                                                        _lifetimeScope, _logger)) {
                        await _jobProcess.WaitAsync(ct).ConfigureAwait(false); // Does not throw
                    }

                    // Check if the job is to be continued with new configuration settings
                    if (_jobProcess.JobContinuation == null)
                    {
                        _jobProcess = null;
                        break;
                    }

                    jobProcessInstruction = _jobProcess.JobContinuation;
                    if (jobProcessInstruction?.Job?.JobConfiguration == null ||
                        jobProcessInstruction?.ProcessMode == null)
                    {
                        _logger.Information("Job continuation invalid, continue listening...");
                        _jobProcess = null;
                        break;
                    }
                    _logger.Information("Processing job continuation...");
                }
            }
            catch (OperationCanceledException) {
                _logger.Information("Processing cancellation received ...");
                _jobProcess = null;
            }
            finally {
                _logger.Information("Worker: {WorkerId}, Job: {JobId} processing completed ... ",
                                    WorkerId, jobProcessInstruction.Job.Id);
                if (!ct.IsCancellationRequested)
                {
                    _heartbeatTimer.Change(0, -1); // restart worker heartbeat
                }
            }
        }
Example #2
0
        /// <summary>
        /// Process jobs
        /// </summary>
        /// <returns></returns>
        private async Task ProcessAsync(JobProcessingInstructionModel jobProcessInstruction,
                                        CancellationToken ct)
        {
            try {
                // Stop worker heartbeat to start the job heartbeat process
                _heartbeatTimer.Change(-1, -1); // Stop worker heartbeat

                _logger.Information("Starting to process new job...");
                // Execute processor
                while (true)
                {
                    _jobProcess = null;
                    ct.ThrowIfCancellationRequested();
                    using (_jobProcess = new JobProcess(this, jobProcessInstruction,
                                                        _lifetimeScope, _logger)) {
                        await _jobProcess.WaitAsync(); // Does not throw
                    }

                    // Check if the job is to be continued with new configuration settings
                    if (_jobProcess.JobContinuation == null)
                    {
                        _jobProcess = null;
                        break;
                    }

                    jobProcessInstruction = _jobProcess.JobContinuation;
                    if (jobProcessInstruction?.Job?.JobConfiguration == null ||
                        jobProcessInstruction?.ProcessMode == null)
                    {
                        _logger.Information("Job continuation invalid, continue listening...");
                        _jobProcess = null;
                        break;
                    }
                    _logger.Information("Processing job continuation...");
                }
            }
            finally {
                _logger.Information("Job processing completed...");
                if (!ct.IsCancellationRequested)
                {
                    _heartbeatTimer.Change(0, -1); // restart worker heartbeat
                }
            }
        }