Skip to content

Instantly share code, notes, and snippets.

@Warafux
Created November 2, 2021 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Warafux/44b656e6b3829f32960dca84a4860ab6 to your computer and use it in GitHub Desktop.
Save Warafux/44b656e6b3829f32960dca84a4860ab6 to your computer and use it in GitHub Desktop.
Call Laravel Command as JOB
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
class CallCommandJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $command = "";
protected $args = null;
public function __construct($command, $args = [])
{
$this->command = $command;
$this->args = $args;
}
public function handle()
{
Log::channel("jobs")->info("A job started: CallCommand ".$this->command);
try{
Artisan::call($this->command, $this->args);
}catch(\Exception $ex){
Log::channel("jobs")->error("A job failed: CallCommand ".$this->command. " EX:".$ex);
}
Log::channel("jobs")->info("A job finished: CallCommand ".$this->command);
}
}
<?php
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
// ...
// ...
// ... add 'jobs' to channels
'channels' => [
'jobs' => [
'driver' => 'single',
'path' => storage_path('logs/jobs.log'),
'level' => 'debug',
],
],
// ...
// ...
// ...
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment