public async Task <bool> DoTransactionReversal(string mfbCode, string uniqueIdentifier)
        {
            //Here we log this and return true
            try
            {
                //check if it has been sent before
                //return true if so
                logger.Log($"Log {uniqueIdentifier} for {mfbCode}");
                int reversalCount = await reversalDAO.ReversalExists(mfbCode, uniqueIdentifier);

                //uow.Repository<Reversals>().GetAsync(filter: x => x.UniqueIdentifier == uniqueIdentifier && x.MFBCode == mfbCode);
                if (reversalCount > 0)
                {
                    logger.Log($"Exists {uniqueIdentifier} for {mfbCode}");
                    return(true);
                }
                //int result = await uow.Repository<Reversals>().InsertAsync(new Reversals() { UniqueIdentifier = uniqueIdentifier, MFBCode = mfbCode, DateLogged = DateTime.Now });
                await reversalDAO.Insert(mfbCode, uniqueIdentifier);

                logger.Log($"Done Logging {uniqueIdentifier} for {mfbCode}");
                return(true);
            }
            catch (Exception ex)
            {
                //log the error
                logger.Log($"Error {uniqueIdentifier} for {mfbCode} - {ex.Message}. {ex.StackTrace}");
                return(false);
            }
        }
Example #2
0
        async void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.timer.Stop();
            try
            {
                logger.Log($"Begin Reversals Processing");
                IEnumerable <Reversals> results = await guow.Repository <Reversals>().GetTopNAsync(x => x.ReversalStatus == ReversalStatus.Pending, null, "", 20);

                //new ReversalsRepository("Reversals").GetPendingReversals();
                logger.Log($"Reversals to process {results.Count()}");
                await new ReversalsRepository("Reversals").BulkUpdate(results.ToList());
                Parallel.ForEach(results, async reversal =>
                {
                    logger.Log($"Process");
                    bool result = await Policy
                                  .Handle <Exception>()
                                  //.CircuitBreakerAsync(7, TimeSpan.FromSeconds(40)
                                  .WaitAndRetryForeverAsync(
                        retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                        async(exception, timespan) =>
                    {
                        //log error here and to grafana
                        logger.Log($"An Error occured: {exception.Message} - {exception.StackTrace}");
                        reversal.RetryCount    += 1;
                        reversal.ReversalStatus = ReversalStatus.Processing;
                        await guow.Repository <Reversals>().UpdateAsync(reversal);
                    })
                                  .ExecuteAsync(async() =>
                                                await ReversalService.ProcessReversal(reversal.MFBCode, reversal.UniqueIdentifier));
                    if (result)
                    {
                        logger.Log($"Done Processing");
                        //Update that it has been processed
                        reversal.ReversalStatus = ReversalStatus.Successful;
                        reversal.DateProcessed  = DateTime.Now;
                        await guow.Repository <Reversals>().UpdateAsync(reversal);
                        //new ReversalsRepository("Reversals").Update(reversal);
                        logger.Log($"Done Updating");
                    }
                    //else
                    //{
                    //    logger.Log($"Wasnt successful");
                    //    //Update that it has been processed
                    //    reversal.RetryCount += 1;
                    //    reversal.ReversalStatus = ReversalStatus.Failed;
                    //    await guow.Repository<Reversals>().UpdateAsync(reversal);
                    //    //new ReversalsRepository("Reversals").Update(reversal);
                    //    logger.Log($"Done Updating");
                    //}
                });

                this.timer.Interval = results == null || results.Count() == 0 ?
                                      10 * Convert.ToInt64(ConfigurationManager.AppSettings["BankOne.ReversalEngine.TimerInterval"]) :
                                      Convert.ToInt64(ConfigurationManager.AppSettings["BankOne.ReversalEngine.TimerInterval"]);
            }
            catch (Exception ex)
            {
                logger.Log($"Processing Error - {ex.Message}. {ex.StackTrace}");
            }
            finally
            {
                this.timer.Start();
            }
        }
        protected override void OnStart(string[] args)
        {
            logger.Log(ASTERIKS);
            logger.Log("Starting BankOne.ReversalEngine...");
            _server = WebApp.Start <Startup>(url: System.Configuration.ConfigurationManager.AppSettings["BankOne.ReversalEngine.BaseAddress"]);
            logger.Log("Loaded BankOne.ReversalEngine Web Api...");

            logger.Log("Started BankOne.ReversalEngine timer...");
            //Initialize the section that completes ReversalEngine to be posted.
            reversalService.Start();
            logger.Log("Started BankOne.ReversalEngine.");
            logger.Log(ASTERIKS);
        }