Beispiel #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));
            }
        }
Beispiel #2
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));
            }
        }