1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Jobs/Ninja/TaskScheduler.php

89 lines
2.1 KiB
PHP
Raw Normal View History

2022-05-19 00:33:29 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-05-19 00:33:29 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Ninja;
2023-05-04 01:42:42 +02:00
use App\Libraries\MultiDB;
2023-10-26 04:57:44 +02:00
use App\Models\Scheduler;
2022-05-19 00:33:29 +02:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2023-10-26 04:57:44 +02:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Auth;
2022-05-19 00:33:29 +02:00
2023-01-13 02:43:38 +01:00
//@rebuild it
2022-05-19 00:33:29 +02:00
class TaskScheduler implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2023-01-29 07:14:36 +01:00
public $deleteWhenMissingModels = true;
2022-05-19 00:33:29 +02:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2023-05-04 01:42:42 +02:00
Auth::logout();
2023-01-22 21:41:27 +01:00
if (! config('ninja.db.multi_db_enabled')) {
Scheduler::with('company')
->where('is_paused', false)
->where('is_deleted', false)
->whereNotNull('next_run')
->where('next_run', '<=', now())
->cursor()
->each(function ($scheduler) {
$this->doJob($scheduler);
});
return;
}
foreach (MultiDB::$dbs as $db) {
2022-05-25 00:06:42 +02:00
MultiDB::setDB($db);
2022-05-27 05:10:32 +02:00
Scheduler::with('company')
2023-01-13 10:23:03 +01:00
->where('is_paused', false)
2022-05-27 05:10:32 +02:00
->where('is_deleted', false)
2023-01-13 10:23:03 +01:00
->whereNotNull('next_run')
->where('next_run', '<=', now())
2022-05-27 05:10:32 +02:00
->cursor()
->each(function ($scheduler) {
2022-05-27 05:10:32 +02:00
$this->doJob($scheduler);
});
2022-05-19 00:33:29 +02:00
}
}
private function doJob(Scheduler $scheduler)
{
2023-01-13 12:24:23 +01:00
nlog("Doing job {$scheduler->name}");
2023-01-13 23:46:17 +01:00
try {
$scheduler->service()->runTask();
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
2023-01-13 23:46:17 +01:00
nlog($e->getMessage());
}
}
2022-05-19 00:33:29 +02:00
}