Пример #1
0
        public Task <ConsumerResult> Consume(byte[] body, object parameters)
        {
            var headers = parameters as Headers;

            if (!headers.KeyExists("Warewolf-Execution-Id"))
            {
                headers["Warewolf-Execution-Id"] = new[] { Guid.NewGuid().ToString() };
            }

            var    empty               = new string[] { };
            var    executionId         = Guid.Parse(headers["Warewolf-Execution-Id"].FirstOrDefault());
            var    customTransactionID = headers["Warewolf-Custom-Transaction-Id", empty].FirstOrDefault();
            string strBody             = System.Text.Encoding.UTF8.GetString(body);

            _logger.StartExecution($"[{executionId}] - {customTransactionID} processing body {strBody} ");
            var startDate = DateTime.UtcNow;

            Task <ConsumerResult> task = null;

            try
            {
                task = _consumer.Consume(body, parameters);
                task.Wait();

                var endDate  = DateTime.UtcNow;
                var duration = endDate - startDate;

                if (task.Result == ConsumerResult.Success)
                {
                    _logger.Info($"[{executionId}] - {customTransactionID} success processing body {strBody}");
                    var executionInfo = new ExecutionInfo(startDate, duration, endDate,
                                                          Warewolf.Triggers.QueueRunStatus.Success, executionId, customTransactionID);
                    var executionEntry = new ExecutionHistory(_resourceId, "", executionInfo, _userName);
                    _logger.ExecutionSucceeded(executionEntry);
                }
                else
                {
                    _logger.Error($"Failed to execute {_resourceId + " [" + executionId + "] " + strBody}");
                    CreateExecutionError(task, executionId, startDate, endDate, duration,
                                         customTransactionID);
                }

                return(Task.Run(() => ConsumerResult.Success));
            }
            catch (Exception)
            {
                var endDate  = DateTime.UtcNow;
                var duration = endDate - startDate;
                _logger.Warn($"[{executionId}] - {customTransactionID} failure processing body {strBody}");
                CreateExecutionError(task, executionId, startDate, endDate, duration,
                                     customTransactionID);

                return(Task.Run(() => ConsumerResult.Failed));
            }
        }
Пример #2
0
 public void OnStateElection(ElectStateContext context)
 {
     if (context.CandidateState is FailedState failedState)
     {
         _hangfireLogger.WarnFormat(
             "Job {0} has been failed due to an exception {1}",
             context.BackgroundJob.Id,
             failedState.Exception);
         _logger.Warn(
             "Job {0} has been failed due to an exception {1}",
             context.BackgroundJob.Id,
             failedState.Exception);
     }
 }
Пример #3
0
        public void OnStateElection(ElectStateContext context)
        {
            if (context.CandidateState is FailedState failedState)
            {
                _hangfireLogger.WarnFormat(
                    "Job {0} has been failed due to an exception {1}",
                    context.BackgroundJob.Id,
                    failedState.Exception);
                _logger.Warn("Job {" + context.BackgroundJob.Id + "} has been failed due to an exception {" + failedState.Exception + "}");

                if (IsReapiting(context.TraversedStates, "Failed"))
                {
                    LogJobPerfomedOnSchedulerException(context.BackgroundJob, failedState.Exception, context.CandidateState.ToString(), context.TraversedStates.ToString());
                    _logger.Warn("Job {" + context.BackgroundJob.Id + "} has been failed before and again now due to {" + failedState.Exception + "}, TraversedStates: {" + context.TraversedStates.ToString() + "}");
                }
            }

            if (IsReapiting(context.TraversedStates, "Enqueued"))
            {
                LogJobPerfomedOnSchedulerException(context.BackgroundJob, null, context.CandidateState.ToString(), context.TraversedStates.ToString());
                _logger.Warn("Job {" + context.BackgroundJob.Id + "} has been enqueued before and again now due to {" + context.CandidateState.Reason + "}, TraversedStates: {" + context.TraversedStates.ToString() + "}");
            }
        }
Пример #4
0
        public Task <ConsumerResult> Consume(byte[] body)
        {
            var    executionId = Guid.NewGuid();
            string strBody     = System.Text.Encoding.UTF8.GetString(body);

            _logger.StartExecution($"processing body {strBody}");
            var startDate = DateTime.UtcNow;

            var task = _consumer.Consume(body);

            task.ContinueWith((requestForwarderResult) =>
            {
                var endDate  = DateTime.UtcNow;
                var duration = endDate - startDate;

                _logger.Warn($"failure processing body {strBody}");
                var executionInfo        = new ExecutionInfo(startDate, duration, endDate, Warewolf.Triggers.QueueRunStatus.Error, executionId);
                var executionEntry       = new ExecutionHistory(_resourceId, "", executionInfo, _userName);
                executionEntry.Exception = requestForwarderResult.Exception;

                _logger.ExecutionFailed(executionEntry);
            }, TaskContinuationOptions.OnlyOnFaulted);

            task.ContinueWith((requestForwarderResult) =>
            {
                var endDate  = DateTime.UtcNow;
                var duration = endDate - startDate;

                if (requestForwarderResult.Result == ConsumerResult.Success)
                {
                    _logger.Info($"success processing body{strBody}");
                    var executionInfo  = new ExecutionInfo(startDate, duration, endDate, Warewolf.Triggers.QueueRunStatus.Success, executionId);
                    var executionEntry = new ExecutionHistory(_resourceId, "", executionInfo, _userName);

                    _logger.ExecutionSucceeded(executionEntry);
                }
            }, TaskContinuationOptions.OnlyOnRanToCompletion);

            if (task.IsFaulted)
            {
                return(Task.Run(() => ConsumerResult.Failed));
            }
            else
            {
                return(Task.Run(() => ConsumerResult.Success));
            }
        }