gestione gare pubbliche
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

148 righe
3.8 KiB

<?php
namespace App\Console\Commands\Utilities;
use DateTime;
use Exception;
use ReflectionClass;
use InvalidArgumentException;
use Illuminate\Console\Command;
class JobDispatcherCommand extends Command
{
/**
* Job info
*
* @var mixed
*/
private $job;
/**
* Queue info
*
* @var mixed
*/
private $queue;
/**
* Connection Info
*
* @var mixed
*/
private $connection;
/**
* Delay info
*
* @var mixed
*/
private $delay;
/**
* Params info
*
* @var mixed
*/
private $params;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'job:dispatch
{job : Job\'s Name}
{--P|params=*? : Job\'s Parameters}
{--Q|queue=preservation : Dispatch queue (default, low, high, ...)}
{--C|connection=database : Dispatch queue on a specific connection (sync, database)}
{--D|date=now : Dispatch date (Y-m-d H:i:s)}
{--S|sync}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Dispatch a job into a queue.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
try {
$this->job = $this->argument("job");
$this->job = str_replace('/', '\\', $this->job);
if(! class_exists($this->job)) {
$this->job = str_ireplace(["App", "Jobs"], "", $this->job);
$this->job = trim($this->job, "\\");
$this->job = "App\\Jobs\\{$this->job}";
if(! class_exists($this->job)) {
throw new InvalidArgumentException("{$this->job} class does not exist.");
}
}
$this->params = null;
$class = new ReflectionClass($this->job);
$constructor = $class->getConstructor();
if(! empty($constructor) && ! empty($constructor->getParameters())) {
$this->params = $this->option("params") ?? null;
if(! empty($this->params) && method_exists($this->job, "formatParameters")) {
$this->params = $this->job::formatParameters(... $this->params);
}
}
if($this->option('sync')) {
$this->job::dispatchSync(... $this->params);
return 0;
}
$this->queue = $this->option("queue");
$this->connection = $this->option("connection");
if(! in_array($this->connection, ["sync", "database"])) { $this->connection = "database"; }
$this->delay = 0;
$date = $this->option("date");
if($date != "now") {
$format = "Y-m-d H:i:s";
$d = DateTime::createFromFormat($format, $date);
if($d && $d->format($format) === $date) {
if (strtotime($date) > strtotime('now')) {
$this->delay = now()->addSeconds(strtotime($date) - strtotime('now'));
}
}
}
$this->job::dispatch(... $this->params)
->onConnection($this->connection)
->onQueue($this->queue)
->delay($this->delay);
$this->line("Job has been scheduled.");
return 0;
} catch (Exception $e) {
$this->line($e->getMessage() . " on line " . $e->getLine());
}
}
}