private LocalTaskQueueingResult DoTryQueueTask([NotNull] TaskIndexRecord taskIndexRecord, [CanBeNull] TaskMetaInformation taskMeta, TaskQueueReason taskQueueReason, bool taskIsBeingTraced) { var taskIsSentToThreadPool = false; if (taskMeta != null && !taskHandlerRegistry.ContainsHandlerFor(taskMeta.Name)) { return(LocalTaskQueueingResult.TaskIsSkippedResult); } if (taskMeta == null && taskIndexRecord.MinimalStartTicks > (Timestamp.Now - HandlerTask.MaxAllowedIndexInconsistencyDuration).Ticks) { logger.Debug("Мета для задачи TaskId = {RtqTaskId} еще не записана, ждем {MaxAllowedIndexInconsistencyDuration}", new { HandlerTask.MaxAllowedIndexInconsistencyDuration, RtqTaskId = taskIndexRecord.TaskId }); return(LocalTaskQueueingResult.TaskIsSkippedResult); } if (!localQueueTaskCounter.TryIncrement(taskQueueReason)) { return(LocalTaskQueueingResult.QueueIsFullResult); } try { var handlerTask = new HandlerTask(taskIndexRecord, taskQueueReason, taskMeta, taskHandlerRegistry, rtqInternals); lock (lockObject) { if (stopped) { return(LocalTaskQueueingResult.QueueIsStoppedResult); } if (hashtable.ContainsKey(taskIndexRecord.TaskId)) { return(LocalTaskQueueingResult.TaskIsSkippedResult); } var taskWrapper = new TaskWrapper(taskIndexRecord.TaskId, taskQueueReason, taskIsBeingTraced, handlerTask, this, logger); var asyncTask = Task.Factory.StartNew(taskWrapper.Run); taskIsSentToThreadPool = true; metricsContext.Meter("TaskIsSentToThreadPool").Mark(); if (!taskWrapper.Finished) { hashtable.Add(taskIndexRecord.TaskId, asyncTask); } } } finally { if (!taskIsSentToThreadPool) { localQueueTaskCounter.Decrement(taskQueueReason); } } return(LocalTaskQueueingResult.SuccessResult); }
public TaskWrapper([NotNull] string taskId, TaskQueueReason taskQueueReason, bool taskIsBeingTraced, [NotNull] HandlerTask handlerTask, [NotNull] LocalTaskQueue localTaskQueue, [NotNull] ILog logger) { this.taskId = taskId; this.taskQueueReason = taskQueueReason; this.taskIsBeingTraced = taskIsBeingTraced; this.handlerTask = handlerTask; this.localTaskQueue = localTaskQueue; this.logger = logger; finished = false; }