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;
|
|
|
|
|
|
|
|
use App\Jobs\Report\SendToAdmin;
|
2022-05-21 20:53:22 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2022-05-19 00:33:29 +02:00
|
|
|
use App\Models\Scheduler;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach (MultiDB::$dbs as $db) {
|
2022-05-25 00:06:42 +02:00
|
|
|
MultiDB::setDB($db);
|
2022-05-27 05:10:32 +02:00
|
|
|
|
2022-05-30 20:52:12 +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()
|
2022-06-21 11:57:17 +02:00
|
|
|
->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();
|
|
|
|
}
|
|
|
|
catch(\Exception $e){
|
|
|
|
nlog($e->getMessage());
|
|
|
|
|
|
|
|
}
|
2022-05-21 20:53:22 +02:00
|
|
|
}
|
2023-01-13 23:46:17 +01:00
|
|
|
|
|
|
|
|
2022-05-19 00:33:29 +02:00
|
|
|
}
|