private ProcessTaskResult DoProcessTask([NotNull] TaskMetaInformation inProcessMeta, [NotNull] byte[] taskData, [NotNull] MetricsContext metricsContext) { metricsContext = metricsContext.SubContext(nameof(DoProcessTask)); using (metricsContext.Timer("Total").NewContext()) { IRtqTaskHandler taskHandler; try { using (metricsContext.Timer("CreateHandlerFor").NewContext()) taskHandler = taskHandlerRegistry.CreateHandlerFor(inProcessMeta.Name); } catch (Exception e) { logger.Error(e, "TaskHandler creation error for: {RtqTaskMeta}", new { RtqTaskMeta = inProcessMeta }); var newExceptionInfoIds = TrуAddExceptionInfo(e, inProcessMeta); using (metricsContext.Timer("TrySwitchToTerminalState").NewContext()) return(new ProcessTaskResult(LocalTaskProcessingResult.Error, TrySwitchToTerminalState(inProcessMeta, TaskState.Fatal, newExceptionInfoIds))); } var task = new Task(inProcessMeta, taskData); using (TaskExecutionContext.ForTask(task)) { var sw = Stopwatch.StartNew(); try { HandleResult handleResult; using (metricsContext.Timer("HandleTask").NewContext()) handleResult = taskHandler.HandleTask(taskProducer, serializer, task); rtqProfiler.ProcessTaskExecutionFinished(inProcessMeta, handleResult, sw.Elapsed); MetricsContext.For(inProcessMeta).Meter("TasksExecuted").Mark(); using (metricsContext.Timer("UpdateTaskMetaByHandleResult").NewContext()) return(UpdateTaskMetaByHandleResult(inProcessMeta, handleResult)); } catch (Exception e) { rtqProfiler.ProcessTaskExecutionFailed(inProcessMeta, sw.Elapsed); MetricsContext.For(inProcessMeta).Meter("TasksExecutionFailed").Mark(); logger.Error(e, "Unhandled exception in TaskHandler for: {RtqTaskMeta}", new { RtqTaskMeta = inProcessMeta }); var taskExceptionInfoId = TrуAddExceptionInfo(e, inProcessMeta); using (metricsContext.Timer("TrySwitchToTerminalState").NewContext()) return(new ProcessTaskResult(LocalTaskProcessingResult.Error, TrySwitchToTerminalState(inProcessMeta, TaskState.Fatal, taskExceptionInfoId))); } } } }