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); } }
private async Task ExecuteInternal(IJobExecutionContext context, Type jobHandlerType, CancellationToken?cancellationToken = null) { var scheduledTask = await _scheduledTaskStore.GetById(context.JobDetail.Key.ToString()); if (scheduledTask?.Enabled == false) { return; } if (scheduledTask is 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 = Environment.MachineName, Enabled = true }; } scheduledTask.ExecutionCount++; scheduledTask.LastExecution = context.FireTimeUtc; scheduledTask.NextExecution = context.NextFireTimeUtc; scheduledTask.Status = ScheduledTaskStatus.Running; scheduledTask.WorkerId = Environment.MachineName; await _scheduledTaskStore.Save(scheduledTask); try { await _taskHandlerActivator.Invoke(jobHandlerType, scheduledTask.State, cancellationToken ?? context.CancellationToken); } catch (Exception exception) { scheduledTask.Errors = exception.ToString(); scheduledTask.LastErrorDate = DateTimeOffset.UtcNow; _logger.LogError("An error occured while executing task '{TaskHandlerName}'. Exception is: {Exception}", jobHandlerType.Name, exception); } finally { scheduledTask.Status = ScheduledTaskStatus.Idle; await _scheduledTaskStore.Save(scheduledTask); } }