Skip to content

Instantly share code, notes, and snippets.

@Warafux
Created November 2, 2021 08:53
Show Gist options
  • Select an option

  • Save Warafux/44b656e6b3829f32960dca84a4860ab6 to your computer and use it in GitHub Desktop.

Select an option

Save Warafux/44b656e6b3829f32960dca84a4860ab6 to your computer and use it in GitHub Desktop.

Revisions

  1. Warafux created this gist Nov 2, 2021.
    36 changes: 36 additions & 0 deletions CallCommandJob.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?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);
    }
    }
    24 changes: 24 additions & 0 deletions logging.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?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',
    ],
    ],


    // ...
    // ...
    // ...

    ]