1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Ninja/TaskScheduler.php

75 lines
1.7 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
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Ninja;
use App\Jobs\Report\SendToAdmin;
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()
{
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();
}
catch(\Exception $e){
nlog($e->getMessage());
}
}
2023-01-13 23:46:17 +01:00
2022-05-19 00:33:29 +02:00
}