예제 #1
0
        public IActionResult RetrievePayment([FromBody] CreateChargeRequest request)
        {
            CreateChargeResponse response = new CreateChargeResponse();

            int waitTimeMillis = 0;

            if (slow)
            {
                waitTimeMillis = random.Next(0, 60) * 1000;
            }

            if (random.NextDouble() > 0.8d)
            {
                response.errorCode = "Credit card expired";
            }

            Console.WriteLine("Charge on credit card will take " + waitTimeMillis / 1000 + " seconds");
            Thread.Sleep(waitTimeMillis);
            response.transactionId = Guid.NewGuid().ToString();
            return(Ok(response));
        }
            public void Execute(ExternalTask externalTask, ref Dictionary <string, object> resultVariables)
            {
                string customerId             = (string)externalTask.Variables["customerId"].Value;
                long   amount                 = (long)externalTask.Variables["amount"].Value;
                CreateChargeResponse response = circuitBreakerPolicy.Execute <CreateChargeResponse>(
                    () => RestApiClient.InvokeRestApi(customerId, amount, TimeSpan.FromSeconds(1)));

                if (!string.IsNullOrEmpty(response.errorCode))
                {
                    // raise error to be handled in BPMN model in case there was an error in credit card handling
                    throw new UnrecoverableBusinessErrorException("Error_CreditCardError", "Could not charge credit card");
                }

                resultVariables.Add("transactionId", response.transactionId);

                string traceId = (string)externalTask.Variables["traceId"].Value;

                if (semaphors.ContainsKey(traceId))
                {
                    semaphors[traceId].Release();
                }
            }