Esempio n. 1
0
        public async Task Execute(IJobExecutionContext context)
        {
            if (_configuration.StopWorkerHost())
            {
                return;
            }
            _logger.LogInformation("Dequeue job run at: {Timestamp}", DateTime.UtcNow);
            var jobDataMap     = context.JobDetail.JobDataMap;
            var jobHandlerType = jobDataMap[JobDataKeys.JobHandlerType] as Type;
            var workItem       = await _workItemQueue.Dequeue();

            if (workItem != null)
            {
                jobDataMap[JobDataKeys.BackoffIndex] = 0;
                try {
                    await _taskHandlerActivator.Invoke(jobHandlerType, jobDataMap, context.CancellationToken, workItem.Value);
                } catch (Exception exception) {
                    if (workItem.DequeueCount < 5)
                    {
                        await _workItemQueue.ReEnqueue(workItem); // Re-enqueue to retry.
                    }
                    else
                    {
                        await _workItemQueue.MarkPoison(workItem); // Enqueue to poison enqueue.
                    }
                    _logger.LogError("An error occured while processing work item '{WorkItem}'. Exception is: {Exception}", workItem, exception);
                }
            }
            else
            {
                jobDataMap[JobDataKeys.BackoffIndex] = (int)(jobDataMap[JobDataKeys.BackoffIndex] ?? 0) + 1;
                Reschedule(context);
            }
        }
Esempio n. 2
0
        public async Task Execute(IJobExecutionContext context)
        {
            if (_configuration.StopWorkerHost())
            {
                return;
            }
            _logger.LogInformation("Scheduled job run at: {Timestamp}", DateTime.UtcNow);
            var jobDataMap     = context.JobDetail.JobDataMap;
            var jobHandlerType = jobDataMap[JobDataKeys.JobHandlerType] as Type;
            var scheduledTask  = await _scheduledTaskStore.GetById(context.JobDetail.Key.ToString());

            if (scheduledTask == null)
            {
                scheduledTask = new ScheduledTask <TState> {
                    Id             = context.JobDetail.Key.ToString(),
                    Description    = context.JobDetail.Description,
                    ExecutionCount = 0,
                    Group          = context.JobDetail.Key.Group,
                    LastExecution  = context.FireTimeUtc,
                    NextExecution  = context.NextFireTimeUtc,
                    Progress       = 0,
                    State          = new TState(),
                    Status         = ScheduledTaskStatus.Running,
                    Type           = context.JobDetail.JobType.ToString(),
                    WorkerId       = context.Scheduler.SchedulerName
                };
            }
            scheduledTask.ExecutionCount++;
            scheduledTask.LastExecution = context.FireTimeUtc;
            scheduledTask.NextExecution = context.NextFireTimeUtc;
            scheduledTask.Status        = ScheduledTaskStatus.Running;
            scheduledTask.WorkerId      = context.Scheduler.SchedulerName;
            await _scheduledTaskStore.Save(scheduledTask);

            try {
                await _taskHandlerActivator.Invoke(jobHandlerType, scheduledTask.State, context.CancellationToken);
            } catch (Exception exception) {
                scheduledTask.Errors = exception.ToString();
                _logger.LogError("An error occured while executing task '{TaskHandlerName}'. Exception is: {Exception}", jobHandlerType.Name, exception);
            } finally {
                scheduledTask.Status = ScheduledTaskStatus.Idle;
                await _scheduledTaskStore.Save(scheduledTask);
            }
        }