mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 07:44:38 -04:00
Clean up how we execute scheduled commands
This commit is contained in:
parent
1125487b45
commit
bb4687f18d
@ -105,11 +105,11 @@ class Kernel extends ConsoleKernel
|
||||
//not used @deprecate
|
||||
// $schedule->job(new SendFailedEmails)->daily()->withoutOverlapping();
|
||||
|
||||
$schedule->command('ninja:check-data --database=db-ninja-01')->daily('02:00')->withoutOverlapping();
|
||||
$schedule->command('ninja:check-data --database=db-ninja-01')->dailyAt('02:10')->withoutOverlapping()->name('check-data-db-1-job')->onOneServer();
|
||||
|
||||
$schedule->command('ninja:check-data --database=db-ninja-02')->dailyAt('02:05')->withoutOverlapping();
|
||||
$schedule->command('ninja:check-data --database=db-ninja-02')->dailyAt('02:20')->withoutOverlapping()->name('check-data-db-2-job')->onOneServer();
|
||||
|
||||
$schedule->command('ninja:s3-cleanup')->dailyAt('23:15')->withoutOverlapping();
|
||||
$schedule->command('ninja:s3-cleanup')->dailyAt('23:15')->withoutOverlapping()->name('s3-cleanup-job')->onOneServer();
|
||||
|
||||
}
|
||||
|
||||
|
@ -41,19 +41,7 @@ class SubscriptionCron
|
||||
nlog('Subscription Cron');
|
||||
|
||||
if (! config('ninja.db.multi_db_enabled')) {
|
||||
$this->loopSubscriptions();
|
||||
} else {
|
||||
//multiDB environment, need to
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
MultiDB::setDB($db);
|
||||
|
||||
$this->loopSubscriptions();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loopSubscriptions()
|
||||
{
|
||||
$invoices = Invoice::where('is_deleted', 0)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||
->where('balance', '>', 0)
|
||||
@ -76,9 +64,39 @@ class SubscriptionCron
|
||||
//This will send the notification daily.
|
||||
//We'll need to handle this by performing some action on the invoice to either archive it or delete it?
|
||||
});
|
||||
|
||||
|
||||
} else {
|
||||
//multiDB environment, need to
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
MultiDB::setDB($db);
|
||||
|
||||
$invoices = Invoice::where('is_deleted', 0)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||
->where('balance', '>', 0)
|
||||
->whereDate('due_date', '<=', now()->addDay()->startOfDay())
|
||||
->whereNull('deleted_at')
|
||||
->whereNotNull('subscription_id')
|
||||
->cursor();
|
||||
|
||||
$invoices->each(function ($invoice) {
|
||||
$subscription = $invoice->subscription;
|
||||
|
||||
$body = [
|
||||
'context' => 'plan_expired',
|
||||
'client' => $invoice->client->hashed_id,
|
||||
'invoice' => $invoice->hashed_id,
|
||||
'subscription' => $subscription->hashed_id,
|
||||
];
|
||||
|
||||
$this->sendLoad($subscription, $body);
|
||||
//This will send the notification daily.
|
||||
//We'll need to handle this by performing some action on the invoice to either archive it or delete it?
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function handleWebhook($invoice, $subscription)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -42,20 +42,32 @@ class CompanySizeCheck implements ShouldQueue
|
||||
public function handle()
|
||||
{
|
||||
if (! config('ninja.db.multi_db_enabled')) {
|
||||
$this->check();
|
||||
|
||||
Company::where('is_large', false)->withCount(['invoices', 'clients', 'products'])->cursor()->each(function ($company) {
|
||||
if ($company->invoices_count > 500 || $company->products_count > 500 || $company->clients_count > 500) {
|
||||
nlog("Marking company {$company->id} as large");
|
||||
|
||||
$company->account->companies()->update(['is_large' => true]);
|
||||
}
|
||||
});
|
||||
|
||||
nlog("updating all client credit balances");
|
||||
|
||||
Client::where('updated_at', '>', now()->subDay())
|
||||
->cursor()
|
||||
->each(function ($client){
|
||||
|
||||
$client->credit_balance = $client->service()->getCreditBalance();
|
||||
$client->save();
|
||||
|
||||
});
|
||||
|
||||
} else {
|
||||
//multiDB environment, need to
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
MultiDB::setDB($db);
|
||||
|
||||
$this->check();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function check()
|
||||
{
|
||||
nlog("Checking all company sizes");
|
||||
nlog("Company size check db {$db}");
|
||||
|
||||
Company::where('is_large', false)->withCount(['invoices', 'clients', 'products'])->cursor()->each(function ($company) {
|
||||
if ($company->invoices_count > 500 || $company->products_count > 500 || $company->clients_count > 500) {
|
||||
@ -77,4 +89,7 @@ class CompanySizeCheck implements ShouldQueue
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ class QueueSize implements ShouldQueue
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
public function handle() :void
|
||||
{
|
||||
LightLogs::create(new QueueSizeAnalytic(Queue::size()))
|
||||
->send();
|
||||
|
@ -46,25 +46,41 @@ class ReminderJob implements ShouldQueue
|
||||
*/
|
||||
public function handle() :void
|
||||
{
|
||||
if (! config('ninja.db.multi_db_enabled')) {
|
||||
$this->processReminders();
|
||||
if (! config('ninja.db.multi_db_enabled'))
|
||||
{
|
||||
set_time_limit(0);
|
||||
|
||||
nlog("Sending invoice reminders on ".now()->format('Y-m-d h:i:s'));
|
||||
|
||||
Invoice::query()
|
||||
->where('is_deleted', 0)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||
->whereNull('deleted_at')
|
||||
->where('balance', '>', 0)
|
||||
->where('next_send_date', '<=', now()->toDateTimeString())
|
||||
->whereHas('client', function ($query) {
|
||||
$query->where('is_deleted', 0)
|
||||
->where('deleted_at', null);
|
||||
})
|
||||
->whereHas('company', function ($query) {
|
||||
$query->where('is_disabled', 0);
|
||||
})
|
||||
->with('invitations')->cursor()->each(function ($invoice) {
|
||||
|
||||
$this->sendReminderForInvoice($invoice);
|
||||
|
||||
});
|
||||
|
||||
|
||||
} else {
|
||||
//multiDB environment, need to
|
||||
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
MultiDB::setDB($db);
|
||||
nlog("set db {$db}");
|
||||
$this->processReminders();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function processReminders()
|
||||
foreach (MultiDB::$dbs as $db)
|
||||
{
|
||||
nlog('Sending invoice reminders '.now()->format('Y-m-d h:i:s'));
|
||||
|
||||
set_time_limit(0);
|
||||
MultiDB::setDB($db);
|
||||
|
||||
nlog("Sending invoice reminders on db {$db} ".now()->format('Y-m-d h:i:s'));
|
||||
|
||||
Invoice::query()
|
||||
->where('is_deleted', 0)
|
||||
@ -81,6 +97,18 @@ class ReminderJob implements ShouldQueue
|
||||
})
|
||||
->with('invitations')->cursor()->each(function ($invoice) {
|
||||
// if ($invoice->refresh() && $invoice->isPayable()) {
|
||||
|
||||
$this->sendReminderForInvoice($invoice);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function sendReminderForInvoice($invoice) {
|
||||
|
||||
if ($invoice->isPayable()) {
|
||||
|
||||
//Attempts to prevent duplicates from sending
|
||||
@ -108,7 +136,7 @@ class ReminderJob implements ShouldQueue
|
||||
(Ninja::isSelfHost() || $invoice->company->account->isPaidHostedClient())) {
|
||||
|
||||
$invoice->invitations->each(function ($invitation) use ($invoice, $reminder_template) {
|
||||
EmailEntity::dispatch($invitation, $invitation->company, $reminder_template);
|
||||
EmailEntity::dispatchSync($invitation, $invitation->company, $reminder_template);
|
||||
nlog("Firing reminder email for invoice {$invoice->number} - {$reminder_template}");
|
||||
});
|
||||
|
||||
@ -122,7 +150,6 @@ class ReminderJob implements ShouldQueue
|
||||
$invoice->save();
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,19 +34,7 @@ class UpdateExchangeRates implements ShouldQueue
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (config('ninja.db.multi_db_enabled')) {
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
MultiDB::setDB($db);
|
||||
$this->updateCurrencies();
|
||||
}
|
||||
} else {
|
||||
$this->updateCurrencies();
|
||||
}
|
||||
}
|
||||
|
||||
private function updateCurrencies()
|
||||
public function handle() :void
|
||||
{
|
||||
info('updating currencies');
|
||||
|
||||
@ -56,6 +44,10 @@ class UpdateExchangeRates implements ShouldQueue
|
||||
|
||||
$cc_endpoint = sprintf('https://openexchangerates.org/api/latest.json?app_id=%s', config('ninja.currency_converter_api_key'));
|
||||
|
||||
if (config('ninja.db.multi_db_enabled')) {
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
MultiDB::setDB($db);
|
||||
|
||||
$client = new Client();
|
||||
$response = $client->get($cc_endpoint);
|
||||
|
||||
@ -71,5 +63,28 @@ class UpdateExchangeRates implements ShouldQueue
|
||||
$currencies = Currency::orderBy('name')->get();
|
||||
|
||||
Cache::forever('currencies', $currencies);
|
||||
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
$client = new Client();
|
||||
$response = $client->get($cc_endpoint);
|
||||
|
||||
$currency_api = json_decode($response->getBody());
|
||||
|
||||
/* Update all currencies */
|
||||
Currency::all()->each(function ($currency) use ($currency_api) {
|
||||
$currency->exchange_rate = $currency_api->rates->{$currency->code};
|
||||
$currency->save();
|
||||
});
|
||||
|
||||
/* Rebuild the cache */
|
||||
$currencies = Currency::orderBy('name')->get();
|
||||
|
||||
Cache::forever('currencies', $currencies);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -785,7 +785,7 @@ class SubscriptionService
|
||||
*/
|
||||
public function triggerWebhook($context)
|
||||
{
|
||||
nlog("trigger webook");
|
||||
nlog("trigger webhook");
|
||||
|
||||
if (empty($this->subscription->webhook_configuration['post_purchase_url']) || is_null($this->subscription->webhook_configuration['post_purchase_url']) || strlen($this->subscription->webhook_configuration['post_purchase_url']) < 1) {
|
||||
return ["message" => "Success", "status_code" => 200];
|
||||
|
Loading…
x
Reference in New Issue
Block a user