public RemoteTaskQueue(ILog logger, ISerializer serializer, IGlobalTime globalTime, ICassandraCluster cassandraCluster, IRtqSettings rtqSettings, IRtqTaskDataRegistry taskDataRegistry, IRtqProfiler rtqProfiler) { QueueKeyspace = rtqSettings.QueueKeyspace; TaskTtl = rtqSettings.TaskTtl; Logger = logger.ForContext("CassandraDistributedTaskQueue"); Serializer = serializer; GlobalTime = globalTime; TaskDataRegistry = taskDataRegistry; enableContinuationOptimization = rtqSettings.EnableContinuationOptimization; minTicksHolder = new RtqMinTicksHolder(cassandraCluster, rtqSettings); TaskMinimalStartTicksIndex = new TaskMinimalStartTicksIndex(cassandraCluster, serializer, rtqSettings, new OldestLiveRecordTicksHolder(minTicksHolder), Logger); var taskMetaStorage = new TaskMetaStorage(cassandraCluster, serializer, rtqSettings, Logger); EventLogRepository = new EventLogRepository(serializer, cassandraCluster, rtqSettings, minTicksHolder); childTaskIndex = new ChildTaskIndex(cassandraCluster, rtqSettings, serializer, taskMetaStorage); HandleTasksMetaStorage = new HandleTasksMetaStorage(taskMetaStorage, TaskMinimalStartTicksIndex, EventLogRepository, GlobalTime, childTaskIndex, taskDataRegistry, Logger); TaskDataStorage = new TaskDataStorage(cassandraCluster, rtqSettings, Logger); TaskExceptionInfoStorage = new TaskExceptionInfoStorage(cassandraCluster, serializer, rtqSettings, Logger); HandleTaskCollection = new HandleTaskCollection(HandleTasksMetaStorage, TaskDataStorage, TaskExceptionInfoStorage, rtqProfiler); lazyRemoteLockCreator = new Lazy <IRemoteLockCreator>(() => { var remoteLockImplementationSettings = CassandraRemoteLockImplementationSettings.Default(rtqSettings.QueueKeyspace, RtqColumnFamilyRegistry.LocksColumnFamilyName); var remoteLockImplementation = new CassandraRemoteLockImplementation(cassandraCluster, serializer, remoteLockImplementationSettings); var remoteLockerMetrics = new RemoteLockerMetrics($"{rtqSettings.QueueKeyspace}_{RtqColumnFamilyRegistry.LocksColumnFamilyName}"); return(new RemoteLocker(remoteLockImplementation, remoteLockerMetrics, Logger)); }); Profiler = rtqProfiler; }
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); } }
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); } }
public RemoteTaskInfo[] GetTaskInfos([NotNull, ItemNotNull] string[] taskIds) { if (taskIds.Any(string.IsNullOrWhiteSpace)) { throw new InvalidOperationException(string.Format("Every taskId must be non-empty: {0}", string.Join(", ", taskIds))); } var tasks = HandleTaskCollection.GetTasks(taskIds); var taskExceptionInfos = TaskExceptionInfoStorage.Read(tasks.Select(x => x.Meta).ToArray()); return(tasks.Select(task => { var taskType = TaskDataRegistry.GetTaskType(task.Meta.Name); var taskData = (IRtqTaskData)Serializer.Deserialize(taskType, task.Data); return new RemoteTaskInfo(task.Meta, taskData, taskExceptionInfos[task.Meta.Id]); }).ToArray()); }