Esempio n. 1
0
 public TaskManipulationResult TryRerunTask([NotNull] string taskId, TimeSpan delay)
 {
     if (string.IsNullOrWhiteSpace(taskId))
     {
         throw new InvalidOperationException("TaskId is required");
     }
     if (delay.Ticks < 0)
     {
         throw new InvalidOperationException(string.Format("Invalid delay: {0}", delay));
     }
     if (!RemoteLockCreator.TryGetLock(taskId, out var remoteLock))
     {
         return(TaskManipulationResult.Failure_LockAcquiringFails);
     }
     using (remoteLock)
     {
         var task = HandleTaskCollection.TryGetTask(taskId);
         if (task == null)
         {
             return(TaskManipulationResult.Failure_TaskDoesNotExist);
         }
         var taskMeta           = task.Meta;
         var oldTaskIndexRecord = HandleTasksMetaStorage.FormatIndexRecord(taskMeta);
         taskMeta.State             = TaskState.WaitingForRerun;
         taskMeta.MinimalStartTicks = (Timestamp.Now + delay).Ticks;
         HandleTasksMetaStorage.AddMeta(taskMeta, oldTaskIndexRecord);
         if (taskMeta.NeedTtlProlongation())
         {
             taskMeta.SetOrUpdateTtl(TaskTtl);
             HandleTaskCollection.ProlongTaskTtl(taskMeta, task.Data);
         }
         return(TaskManipulationResult.Success);
     }
 }
Esempio n. 2
0
 public TaskManipulationResult TryCancelTask([NotNull] string taskId)
 {
     if (string.IsNullOrWhiteSpace(taskId))
     {
         throw new InvalidOperationException("TaskId is required");
     }
     if (!RemoteLockCreator.TryGetLock(taskId, out var remoteLock))
     {
         return(TaskManipulationResult.Failure_LockAcquiringFails);
     }
     using (remoteLock)
     {
         var task = HandleTaskCollection.TryGetTask(taskId);
         if (task == null)
         {
             return(TaskManipulationResult.Failure_TaskDoesNotExist);
         }
         var taskMeta = task.Meta;
         if (taskMeta.State == TaskState.New || taskMeta.State == TaskState.WaitingForRerun || taskMeta.State == TaskState.WaitingForRerunAfterError || taskMeta.State == TaskState.InProcess)
         {
             var oldTaskIndexRecord = HandleTasksMetaStorage.FormatIndexRecord(taskMeta);
             taskMeta.State = TaskState.Canceled;
             taskMeta.FinishExecutingTicks = Timestamp.Now.Ticks;
             HandleTasksMetaStorage.AddMeta(taskMeta, oldTaskIndexRecord);
             return(TaskManipulationResult.Success);
         }
         return(TaskManipulationResult.Failure_InvalidTaskState);
     }
 }