예제 #1
0
        /// <summary>
        /// Create execution record
        /// </summary>
        public async Task <Guid> CreateExecutionRecordAsync(Guid executionId, Action <ScheduledJobExecution> prepare)
        {
            try
            {
                if (executionId == Guid.Empty)
                {
                    throw new Exception($"{nameof(executionId)} must not be empty");
                }

                using (var unitOfWork = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
                {
                    var existingExecution = await JobExecutionRepository.GetAll().FirstOrDefaultAsync(ex => ex.Id == executionId);

                    ScheduledJobExecution jobExecution = null;

                    if (existingExecution != null && existingExecution.Status == ExecutionStatus.Enqueued)
                    {
                        jobExecution        = existingExecution;
                        jobExecution.Status = ExecutionStatus.InProgress;
                    }
                    else
                    {
                        var job = await JobRepository.GetAsync(Id);

                        var trigger = GetTrigger();
                        jobExecution = new ScheduledJobExecution()
                        {
                            Id = existingExecution != null
                                ? Guid.NewGuid()
                                : executionId,
                            Job             = job,
                            StartedOn       = DateTime.Now,
                            Status          = ExecutionStatus.Enqueued,
                            Trigger         = trigger,
                            ParentExecution = existingExecution
                        };
                    }

                    prepare?.Invoke(jobExecution);

                    await JobExecutionRepository.InsertAsync(jobExecution);

                    await unitOfWork.CompleteAsync();

                    return(jobExecution.Id);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e.Message, e);
                throw;
            }
        }