public TaskExecutionInfo ProcessTaskDefinition(SingleTaskDefinition taskDefinition) { var lockInfo = LockInfo.Empty(taskDefinition.Identifier); var taskTypeInfo = _taskManager.GetTaskInfo(taskDefinition.Identifier); //links this task's cancellation token to the manager's token var ctSource = CancellationTokenSource.CreateLinkedTokenSource(_listenerCtSource.Token); try { //Inter-process lock. Ignores the task if it doesn't acquire lock if (taskTypeInfo == null || !_taskManager.LockManager.TryRenewLock(lockInfo, taskTypeInfo.LockCycle, retryLock: true)) { return(new TaskExecutionInfo(_managedThreadPool.CreateCompletedTask(), ctSource, lockInfo, taskDefinition)); } var executionInfoLocal = new TaskExecutionInfo(null, ctSource, lockInfo, taskDefinition); var taskBody = CreateTaskBody(executionInfoLocal, taskTypeInfo); var task = _managedThreadPool.QueueUserWorkItem((o) => taskBody(), null); executionInfoLocal.SetTask(task); return(executionInfoLocal); } catch (Exception ex) { ex.LogException(); return(new TaskExecutionInfo(_managedThreadPool.CreateCompletedTask(), ctSource, lockInfo, taskDefinition)); } }
public int EnqueueSingleTask <P>(Guid identifier, P parameter, long delay = 0) { if (delay < 0) { throw new ArgumentException("delay"); } var info = this.GetTaskInfo(identifier); if (info == null) { throw new ArgumentException(String.Format("Task identifier [{0}] not registered.", identifier.ToString())); } if (info.ParamType != typeof(P)) { throw new ArgumentException("Parameter type mismatch. Review registered Task and parameter types."); } var paramStr = JsonConvert.SerializeObject(parameter); using (var tr = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) using (var context = CreateRepository()) { var scheduledAt = this.GetUtcNow().AddMilliseconds(delay).SetFractionalSecondPrecision(4); var data = new SingleTaskDefinition() { Identifier = info.Identifier, JsonParameters = paramStr, ScheduledAt = scheduledAt, }; data.SetStatus(Distributed.SingleTaskStatus.Scheduled); context.SingleTasks.Add(data); context.SaveChanges(); return(data.Id); } }
internal TaskExecutionInfo(IThreadTaskInfo task, CancellationTokenSource cancelTokenSource, LockInfo lockInfo, SingleTaskDefinition taskDefinition) { if (lockInfo == null) { throw new ArgumentNullException("lockInfo is null."); } if (taskDefinition == null) { throw new ArgumentNullException("taskDefinition is null."); } if (cancelTokenSource == null) { throw new ArgumentNullException("cancelTokenSource is null."); } //Task may be null when first initialized. _task = task; _lockInfo = lockInfo; _taskDefinition = taskDefinition; _cancelTokenSource = cancelTokenSource; _lastAliveSignalAt = null; }
internal int EnqueueSingleTask(Guid identifier, int?recurrentTaskId, long delay, bool throwOnNotFound = true) { var info = this.GetTaskInfo(identifier); if (info == null) { if (throwOnNotFound) { throw new ApplicationException(String.Format("Task identifier {0} not registered.", identifier)); } else { return(0); } } using (var tr = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) using (var context = CreateRepository()) { var scheduledAt = this.GetUtcNow().AddMilliseconds(delay).SetFractionalSecondPrecision(4); var data = new SingleTaskDefinition() { Identifier = info.Identifier, JsonParameters = null, ScheduledAt = scheduledAt, RecurrentTaskId = recurrentTaskId }; data.SetStatus(Distributed.SingleTaskStatus.Scheduled); context.SingleTasks.Add(data); context.SaveChanges(); return(data.Id); } }