Ejemplo n.º 1
0
        public async virtual Task <IRequestResult> Work()
        {
            // Examine the total set of processable units, pending + failure handler set.
            // Select the message based on the nature of the subscriptions quality attributes
            // For now, do the simple thing
            IsWorking = true;
            bool ok = true;

            try {
                if (!HasWork().Success)
                {
                    if (BundlePrototype.QualityConstraints.EndureQuietude.HasValue)
                    {
                        await Task.Delay((int)BundlePrototype.QualityConstraints.EndureQuietude.Value);
                    }
                }
                else
                {
                    var candidate = QueryFailureHandlers();
                    IRequestResult <IProcessableUnit> result = await DispatchMessage(candidate ?? NextUnit());

                    if (result.Containee.IsNotNull())
                    {
                        var conclusion = WorkPolicy.Analyze(result);
                        var handler    = PolicyAnalysisHandler;
                        var action     = PolicyHandlers.FirstOrDefault(h => h.Item1(conclusion));
                        action.IsNotNull(() => action.Item2(result.Containee, handler));
                    }
                }
            }
            finally {
                IsWorking = false;
            }
            return(Result(ok));
        }
        public async Task <AuthoriseResponse> AuthoriseAsync(AuthoriseRequest request)
        {
            var json         = JsonSerializer.Serialize(request);
            var content      = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
            var pollyContext = new Context();

            var httpResponse = await PolicyHandlers.CountRetryPolicy()
                               .ExecuteAsync(c => _client.PostAsync("api/authorise", content), pollyContext);

            if (!httpResponse.IsSuccessStatusCode)
            {
                var errorResponse = AuthoriseResponse.CreateFailedResponse(
                    request, pollyContext.GetRetryCount());

                return(errorResponse);
            }

            var result = await httpResponse.Content.DeserializeAsync <AuthoriseResponse>();

            result.RetryAttempts = pollyContext.GetRetryCount();

            return(result);
        }