Created
November 2, 2021 08:53
-
-
Save Warafux/44b656e6b3829f32960dca84a4860ab6 to your computer and use it in GitHub Desktop.
Call Laravel Command as JOB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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