Why is hangfire triggering multiple times

Viewed 135

I configured hangfire to run 2 cron jobs. In the recurring jobs tab I can see the jobs and the next run time is correct but I have received over 15 emails that are triggered by the function hangfire is calling and I can't see the history of those calls on the succeeded jobs tab on hangfire. I want the function to be called once everyday. PS: there is just one server showing on hangfire

recurring jobs tab

```recurringJobManager.AddOrUpdate("RecurringDeposits", () => serviceProvider.GetService<IRecurringDeposits>().AutomatedDepositsAsync(), Cron.Daily(8, 0));
recurringJobManager.AddOrUpdate("RecurringWithdrawals", () => serviceProvider.GetService<IRecurringWithdrawals>().AutomatedWithdrawalsAsync(), ```

the function

```public async Task AutomatedDepositsAsync()
        {
            var coreAccounts = await _dbContext.CustomerAccounts
                .Include(x => x.Customer)
                  .Where(x => x.IsActive
                   && (x.Product.Code == coreAccountCode || x.Product.Code == privateGroupCode || x.Product.Code == publicGroupCode || x.Product.Code == myPurseAccountCode)
                   && x.RecurringCredit && x.CreditInterval != 0 && x.PlanEndDate == null)
                  .ToListAsync();
            if(coreAccounts != null)
            {
                foreach (var acct in coreAccounts)
                {
                    var firstTran = await _dbContext.xxx
                      .Where(x => x.IsApproved
                       && x.Channel == "Card" && x.AccountId == xx.Id)
                      .OrderBy(x => x.IsApprovedDate)
                      .FirstOrDefaultAsync();

                    var card = _dbContext.CustomerCards
                        .Where(x => x.UserId == acct.Customer.Id && x.IsDeleted == false)
                        .OrderBy(x => x.CreationTime)
                        .LastOrDefault();
                    if (card==null)
                    {
                        continue;
                    }


                    if (acct.CreditInterval == 1 && DateTime.UtcNow.Date > firstTran.IsApprovedDate)
                    {

                          await DebitCustomerCard(firstTran, acct.Customer.Email, acct, card);
                       
                    }
                    else if (acct.CreditInterval == 7)
                    {

                        var firstDepositDate = firstTran.IsApprovedDate;
                        var firstDepositDay = firstDepositDate.Value.DayOfWeek;
                        var Today = DateTime.UtcNow.DayOfWeek;

                        if(DateTime.UtcNow.Date > firstDepositDate && firstDepositDay == Today)
                        {
                    
                               await DebitCustomerCard(firstTran, acct.Customer.Email, acct, card);
                        }


                    }
                    else if (acct.CreditInterval == 30)
                    {
                        var firstDepositDate = firstTran.IsApprovedDate;
                        var firstDepositDay = firstDepositDate.Value.Day;
                        var currentMonth = DateTime.UtcNow.Month;
                        var currentyear = DateTime.UtcNow.Year;

                        CultureInfo provider = CultureInfo.InvariantCulture;
                        var NextDepositDate = DateTime.ParseExact(firstDepositDay + "/" + currentMonth + "/" + currentyear, "dd/MM/yyyy", provider);

                        if(DateTime.UtcNow.Date > firstDepositDate && DateTime.UtcNow.Date == NextDepositDate.Date)
                        {
                            
                               await DebitCustomerCard(firstTran, acct.Customer.Email, acct, card);
                        }


                    }
                }
            }
           

        }

      
        public async Task DebitCustomerCard(CustomerContribution firstTransaction, string custEmail, CustomerAccount custAcct, CustomerCard Card)
        {

            var contribution = await _dbContext.Contributions.AddAsync(new CustomerContribution
            {
                Account = firstTransaction.Account,
                Amount = firstTransaction.Amount,
                Tenor = firstTransaction.Tenor,
                Rate = firstTransaction.Rate,
                ExpectedReturn = firstTransaction.ExpectedReturn,
                ChannelCode = Card.AuthCode,
                Channel = "Card",
             });
            await _dbContext.SaveChangesAsync();


            if (contribution == null)
            {
                //do something
                return;
            }
            var reference = $"CRD-{DateTime.UtcNow:yyyyMMddHHmmssffff}{Utils.GenerateRandomString(4)}";
            var transactionLog = new TransactionLog
            {
                Account = firstTransaction.Account,
                Amount = firstTransaction.Amount,
                TransactionDate = DateTime.UtcNow,
                Type = TransactionType.SingleDebit,
                Reference = reference,
                Narration = "Automatic Deposit"
            };

            await _dbContext.TransactionLogs.AddAsync(transactionLog);
           await _dbContext.SaveChangesAsync();

            var postingResponse = await _postingService
            .ChargeCustomerAccount(new xxxxx.ChargeCustomerRequest
            {
                Amount = firstTransaction.Amount,
                AuthCode = Card.AuthCode,
                Email = custEmail,
                Reference = reference
            });

            if (postingResponse.OperationResult != OperationResult.Success)
            {
                //do something
                return;

            }
            transactionLog.ExtReference = postingResponse.Data?.Reference;
            transactionLog.Status = postingResponse.OperationResult.ToString();
            _dbContext.TransactionLogs.Update(transactionLog);

            custAcct.AvailableBalance += firstTransaction.Amount;
            custAcct.TotBalance += firstTransaction.Amount;
            custAcct.TotDeposit += firstTransaction.Amount;
            _dbContext.CustomerAccounts.Update(custAcct);

            contribution.Entity.IsApproved = true;
            contribution.Entity.IsApprovedDate = DateTime.UtcNow;
            _dbContext.Contributions.Update(contribution.Entity);
            await _dbContext.SaveChangesAsync();
        }
```
0 Answers
Related