diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 47ceee908af3..0aa9dcb2a991 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -54,13 +54,13 @@ class Kernel extends ConsoleKernel $schedule->job(new ReminderJob)->hourly()->withoutOverlapping()->name('reminder-job')->onOneServer(); /* Returns the number of jobs in the queue */ - $schedule->job(new QueueSize)->everyFiveMinutes()->withoutOverlapping(); + $schedule->job(new QueueSize)->everyFiveMinutes()->withoutOverlapping()->name('queue-size-job')->onOneServer(); /* Checks for large companies and marked them as is_large */ $schedule->job(new CompanySizeCheck)->dailyAt('23:20')->withoutOverlapping()->name('company-size-job')->onOneServer(); /* Pulls in the latest exchange rates */ - $schedule->job(new UpdateExchangeRates)->dailyAt('23:30')->withoutOverlapping(); + $schedule->job(new UpdateExchangeRates)->dailyAt('23:30')->withoutOverlapping()->name('exchange-rate-job')->onOneServer(); /* Runs cleanup code for subscriptions */ $schedule->job(new SubscriptionCron)->dailyAt('00:01')->withoutOverlapping()->name('subscription-job')->onOneServer(); diff --git a/app/Jobs/Cron/RecurringExpensesCron.php b/app/Jobs/Cron/RecurringExpensesCron.php index 174f4decf423..3305100154e4 100644 --- a/app/Jobs/Cron/RecurringExpensesCron.php +++ b/app/Jobs/Cron/RecurringExpensesCron.php @@ -46,39 +46,62 @@ class RecurringExpensesCron nlog('Sending recurring expenses '.Carbon::now()->format('Y-m-d h:i:s')); if (! config('ninja.db.multi_db_enabled')) { - $this->getRecurringExpenses(); + + $recurring_expenses = RecurringExpense::where('next_send_date', '<=', now()->toDateTimeString()) + ->whereNotNull('next_send_date') + ->whereNull('deleted_at') + ->where('status_id', RecurringInvoice::STATUS_ACTIVE) + ->where('remaining_cycles', '!=', '0') + ->whereHas('company', function ($query) { + $query->where('is_disabled', 0); + }) + ->with('company') + ->cursor(); + + nlog(now()->format('Y-m-d').' Generating Recurring Expenses. Count = '.$recurring_expenses->count()); + + $recurring_expenses->each(function ($recurring_expense, $key) { + nlog('Current date = '.now()->format('Y-m-d').' Recurring date = '.$recurring_expense->next_send_date); + + if (! $recurring_expense->company->is_disabled) { + $this->generateExpense($recurring_expense); + } + }); + } else { //multiDB environment, need to foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); - $this->getRecurringExpenses(); + $recurring_expenses = RecurringExpense::where('next_send_date', '<=', now()->toDateTimeString()) + ->whereNotNull('next_send_date') + ->whereNull('deleted_at') + ->where('status_id', RecurringInvoice::STATUS_ACTIVE) + ->where('remaining_cycles', '!=', '0') + ->whereHas('company', function ($query) { + $query->where('is_disabled', 0); + }) + ->with('company') + ->cursor(); + + nlog(now()->format('Y-m-d').' Generating Recurring Expenses. Count = '.$recurring_expenses->count()); + + $recurring_expenses->each(function ($recurring_expense, $key) { + nlog('Current date = '.now()->format('Y-m-d').' Recurring date = '.$recurring_expense->next_send_date); + + if (! $recurring_expense->company->is_disabled) { + $this->generateExpense($recurring_expense); + } + }); + + } } } private function getRecurringExpenses() { - $recurring_expenses = RecurringExpense::where('next_send_date', '<=', now()->toDateTimeString()) - ->whereNotNull('next_send_date') - ->whereNull('deleted_at') - ->where('status_id', RecurringInvoice::STATUS_ACTIVE) - ->where('remaining_cycles', '!=', '0') - ->whereHas('company', function ($query) { - $query->where('is_disabled', 0); - }) - ->with('company') - ->cursor(); - - nlog(now()->format('Y-m-d').' Generating Recurring Expenses. Count = '.$recurring_expenses->count()); - - $recurring_expenses->each(function ($recurring_expense, $key) { - nlog('Current date = '.now()->format('Y-m-d').' Recurring date = '.$recurring_expense->next_send_date); - - if (! $recurring_expense->company->is_disabled) { - $this->generateExpense($recurring_expense); - } - }); + //extracting this back to the if/else block to test duplicate crons } private function generateExpense(RecurringExpense $recurring_expense) diff --git a/app/Jobs/Ninja/BankTransactionSync.php b/app/Jobs/Ninja/BankTransactionSync.php index 9b2d4c3d00ef..8afeb82aabd1 100644 --- a/app/Jobs/Ninja/BankTransactionSync.php +++ b/app/Jobs/Ninja/BankTransactionSync.php @@ -44,38 +44,30 @@ class BankTransactionSync implements ShouldQueue */ public function handle() { - // if (! Ninja::isHosted()) { - // return; - // } - //multiDB environment, need to - foreach (MultiDB::$dbs as $db) { + foreach (MultiDB::$dbs as $db) + { MultiDB::setDB($db); nlog("syncing transactions"); - $this->syncTransactions(); + $a = Account::with('bank_integrations')->whereNotNull('bank_integration_account_id')->cursor()->each(function ($account){ + + // $queue = Ninja::isHosted() ? 'bank' : 'default'; + + if($account->isPaid() && $account->plan == 'enterprise') + { + + $account->bank_integrations()->where('auto_sync', true)->cursor()->each(function ($bank_integration) use ($account){ + + ProcessBankTransactions::dispatchSync($account->bank_integration_account_id, $bank_integration); + + }); + + } + + }); } } - public function syncTransactions() - { - $a = Account::with('bank_integrations')->whereNotNull('bank_integration_account_id')->cursor()->each(function ($account){ - - // $queue = Ninja::isHosted() ? 'bank' : 'default'; - - if($account->isPaid() && $account->plan == 'enterprise') - { - - $account->bank_integrations()->where('auto_sync', true)->cursor()->each(function ($bank_integration) use ($account){ - - ProcessBankTransactions::dispatchSync($account->bank_integration_account_id, $bank_integration); - - }); - - } - - }); - } - } diff --git a/app/Jobs/Quote/QuoteCheckExpired.php b/app/Jobs/Quote/QuoteCheckExpired.php index 4e5c5d77b388..36126a957b3a 100644 --- a/app/Jobs/Quote/QuoteCheckExpired.php +++ b/app/Jobs/Quote/QuoteCheckExpired.php @@ -42,39 +42,61 @@ class QuoteCheckExpired implements ShouldQueue */ public function handle() { - if (! config('ninja.db.multi_db_enabled')) - return $this->checkForExpiredQuotes(); - - foreach (MultiDB::$dbs as $db) { + if (! config('ninja.db.multi_db_enabled')){ - MultiDB::setDB($db); + Quote::query() + ->where('status_id', Quote::STATUS_SENT) + ->where('is_deleted', false) + ->whereNull('deleted_at') + ->whereNotNull('due_date') + ->whereHas('client', function ($query) { + $query->where('is_deleted', 0) + ->where('deleted_at', null); + }) + ->whereHas('company', function ($query) { + $query->where('is_disabled', 0); + }) + // ->where('due_date', '<='. now()->toDateTimeString()) + ->whereBetween('due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()]) + ->cursor() + ->each(function ($quote){ + $this->queueExpiredQuoteNotification($quote); + }); - $this->checkForExpiredQuotes(); + } + else { + + foreach (MultiDB::$dbs as $db) + { + + MultiDB::setDB($db); + Quote::query() + ->where('status_id', Quote::STATUS_SENT) + ->where('is_deleted', false) + ->whereNull('deleted_at') + ->whereNotNull('due_date') + ->whereHas('client', function ($query) { + $query->where('is_deleted', 0) + ->where('deleted_at', null); + }) + ->whereHas('company', function ($query) { + $query->where('is_disabled', 0); + }) + // ->where('due_date', '<='. now()->toDateTimeString()) + ->whereBetween('due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()]) + ->cursor() + ->each(function ($quote){ + $this->queueExpiredQuoteNotification($quote); + }); + } } } private function checkForExpiredQuotes() { - Quote::query() - ->where('status_id', Quote::STATUS_SENT) - ->where('is_deleted', false) - ->whereNull('deleted_at') - ->whereNotNull('due_date') - ->whereHas('client', function ($query) { - $query->where('is_deleted', 0) - ->where('deleted_at', null); - }) - ->whereHas('company', function ($query) { - $query->where('is_disabled', 0); - }) - // ->where('due_date', '<='. now()->toDateTimeString()) - ->whereBetween('due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()]) - ->cursor() - ->each(function ($quote){ - $this->queueExpiredQuoteNotification($quote); - }); + } private function queueExpiredQuoteNotification(Quote $quote) diff --git a/app/Jobs/Util/ReminderJob.php b/app/Jobs/Util/ReminderJob.php index f5c64f38fcb9..eb609e2287b2 100644 --- a/app/Jobs/Util/ReminderJob.php +++ b/app/Jobs/Util/ReminderJob.php @@ -44,26 +44,19 @@ class ReminderJob implements ShouldQueue * * @return void */ - public function handle() + public function handle() :void { if (! config('ninja.db.multi_db_enabled')) { $this->processReminders(); } else { //multiDB environment, need to - /* + foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); nlog("set db {$db}"); $this->processReminders(); } - */ - //24-11-2022 fix for potential memory leak during a long running process, the second reminder may run twice - foreach (config('ninja.dbs') as $db) { - MultiDB::setDB($db); - nlog("set db {$db}"); - $this->processReminders(); - } - + } }