示例#1
0
        private int CreateAndEnqueueJob(QueueJob queueJob, IState state, DbConnection connection)
        {
            var insertedJob = JobEntity.FromQueueJob(queueJob);

            insertedJob.CreatedAt  = DateTime.UtcNow;
            insertedJob.RetryCount = 1;
            var jobId   = this._queryService.InsertJob(insertedJob, connection);
            var stateId = this._queryService.InsertJobState(
                StateEntity.FromIState(state,
                                       insertedJob.Id),
                connection);

            this._queryService.SetJobState(jobId, stateId, state.Name, connection);
            if (state is EnqueuedState)
            {
                this._queryService.InsertJobToQueue(jobId, queueJob.QueueName, connection);
            }
            foreach (var nextJob in queueJob.NextJobs)
            {
                this._queryService.InsertJobCondition(new JobConditionEntity()
                {
                    Finished    = false,
                    JobId       = nextJob,
                    ParentJobId = jobId
                }, connection);
            }
            return(jobId);
        }
示例#2
0
 public override void SetJobState(int jobId, IState state)
 {
     UseTransaction((conn, tran) =>
     {
         var stateId = this._queryService.InsertJobState(StateEntity.FromIState(state, jobId), conn);
         this._queryService.SetJobState(jobId, stateId, state.Name, conn);
     });
 }
示例#3
0
        private bool EnqueueScheduledTaskAndRecalculateNextTime(ScheduleEntity scheduleEntity, string reasonToEnqueue, Func <ScheduledTask, ScheduledTask> caluculateNext, DbConnection connection)
        {
            var scheduledTask = ScheduleEntity.ToScheduleTask(scheduleEntity);

            //if calculate delegate exists, calculate next invocation and save, otherwise (manual trigger), skip
            if (caluculateNext != null)
            {
                scheduledTask = caluculateNext(scheduledTask);
                scheduleEntity.LastInvocation = scheduledTask.LastInvocation;
                scheduleEntity.NextInvocation = scheduledTask.NextInvocation;
                this._queryService.UpdateScheduledItem(scheduleEntity, t => new { t.LastInvocation, t.NextInvocation }, connection);
            }

            //if job scheduled
            if (!string.IsNullOrEmpty(scheduleEntity.JobInvocationData))
            {
                var insertedJob = JobEntity.FromScheduleEntity(scheduledTask);
                insertedJob.ScheduleName = scheduleEntity.Name;
                var jobId   = this._queryService.InsertJob(insertedJob, connection);
                var stateId = this._queryService.InsertJobState(
                    StateEntity.FromIState(new EnqueuedState()
                {
                    EnqueuedAt = DateTime.UtcNow, Queue = scheduledTask.Job.QueueName, Reason = reasonToEnqueue
                },
                                           insertedJob.Id),
                    connection);
                this._queryService.SetJobState(jobId, stateId, EnqueuedState.DefaultName, connection);
                this._queryService.InsertJobToQueue(jobId, scheduledTask.Job.QueueName, connection);
                return(true);
            }
            //if workflow scheduled
            else if (!string.IsNullOrEmpty(scheduleEntity.WorkflowInvocationData))
            {
                var rootJobs = scheduledTask.Workflow.GetRootJobs().ToDictionary(t => t.TempId, null);
                scheduledTask.Workflow.SaveWorkflow((workflowJob) => {
                    workflowJob.QueueJob.ScheduleName = scheduleEntity.Name;
                    return(CreateAndEnqueueJob(
                               queueJob: workflowJob.QueueJob,
                               state: rootJobs.ContainsKey(workflowJob.TempId) ?
                               new EnqueuedState()
                    {
                        EnqueuedAt = DateTime.UtcNow, Queue = workflowJob.QueueJob.QueueName, Reason = reasonToEnqueue
                    } as IState
                            : new AwaitingState()
                    {
                        Reason = "Waiting for other job/s to finish.", CreatedAt = DateTime.UtcNow
                    } as IState,
                               connection: connection
                               ));
                });
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#4
0
 public override void DeleteJob(int jobId)
 {
     UseTransaction((conn, tran) =>
     {
         var stateId = _queryService.InsertJobState(StateEntity.FromIState(new DeletedState(), jobId), conn);
         _queryService.UpdateJob(new JobEntity()
         {
             Id = jobId, StateId = stateId, State = DeletedState.DefaultName, ExpireAt = DateTime.UtcNow.Add(_options.DefaultJobExpiration)
         }, t => new { t.State, t.StateId, t.ExpireAt }, conn);
     });
 }
示例#5
0
 public override void MarkConsequentlyFailedJobs(int failedJobId)
 {
     UseTransaction((conn, tran) =>
     {
         var dependentJobs = this._queryService.GetDependentWorkflowTree(failedJobId, conn);
         var state         = new ConsequentlyFailed("Job marked as failed because one of jobs this job depends on has failed.", failedJobId);
         foreach (var job in dependentJobs)
         {
             var stateId = this._queryService.InsertJobState(StateEntity.FromIState(state, job.Id), conn);
             this._queryService.SetJobState(job.Id, stateId, state.Name, conn);
         }
     });
 }
示例#6
0
 public override void UpgradeFailedToScheduled(int jobId, IState failedState, IState scheduledState, DateTime nextRun, int retryCount)
 {
     UseTransaction((conn, tran) =>
     {
         this._queryService.InsertJobState(StateEntity.FromIState(failedState, jobId), conn);
         var stateId = this._queryService.InsertJobState(StateEntity.FromIState(scheduledState, jobId), conn);
         this._queryService.UpdateJob(
             new JobEntity {
             Id = jobId, NextRetry = nextRun, RetryCount = retryCount, StateId = stateId, State = scheduledState.Name
         },
             t => new { t.RetryCount, t.NextRetry, t.State, t.StateId },
             conn);
     });
 }
示例#7
0
 public override void EnqueueAwaitingWorkflowJobs(int finishedJobId)
 {
     UseTransaction((conn, tran) =>
     {
         var nextJobs = this._queryService.MarkAsFinishedAndGetNextJobs(finishedJobId, conn);
         foreach (var job in nextJobs)
         {
             var stateId = this._queryService.InsertJobState(
                 StateEntity.FromIState(new EnqueuedState()
             {
                 EnqueuedAt = DateTime.UtcNow, Queue = job.Queue, Reason = "Job enqueued after awaited job successfully finished."
             },
                                        job.Id),
                 conn);
             this._queryService.SetJobState(job.Id, stateId, EnqueuedState.DefaultName, conn);
             this._queryService.InsertJobToQueue(job.Id, job.Queue, conn);
         }
     });
 }
示例#8
0
 public override bool EnqueueNextDelayedJob()
 {
     return(UseTransaction <bool>((conn, tran) =>
     {
         var job = this._queryService.EnqueueNextDelayedJob(conn);
         if (job != null)
         {
             var stateId = this._queryService.InsertJobState(StateEntity.FromIState(new EnqueuedState()
             {
                 EnqueuedAt = DateTime.UtcNow, Queue = job.Queue, Reason = "Enqueued by DelayedJobScheduler"
             }, job.JobId), conn);
             this._queryService.SetJobState(job.JobId, stateId, EnqueuedState.DefaultName, conn);
             return true;
         }
         else
         {
             return false;
         }
     }));
 }