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
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
2022-05-19 00:33:29 +02:00
|
|
|
|
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) {
|
2024-02-04 05:06:24 +01:00
|
|
|
|
|
|
|
nlog("Doing job {$scheduler->name}");
|
|
|
|
|
|
|
|
try {
|
|
|
|
$scheduler->service()->runTask();
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2023-01-22 21:41:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2024-02-04 05:06:24 +01:00
|
|
|
|
|
|
|
nlog("Doing job {$scheduler->name}");
|
|
|
|
|
|
|
|
try {
|
|
|
|
/** @var \App\Models\Scheduler $scheduler */
|
|
|
|
$scheduler->service()->runTask();
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
2022-05-19 00:33:29 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-02-04 05:06:24 +01:00
|
|
|
});
|
2023-01-13 23:46:17 +01:00
|
|
|
}
|
2022-05-21 20:53:22 +02:00
|
|
|
}
|
2024-02-04 05:06:24 +01:00
|
|
|
|
2022-05-19 00:33:29 +02:00
|
|
|
}
|