0

I have always used events and listeners to add tasks to the queue. Now I'm trying to use Jobs. I do it like this: my job.

class eventJob implements ShouldQueue
{
     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
     public $message;
     /**
      * Create a new job instance.
      *
      * @return void
      */
     public function __construct($message)
     {
         $this->message = $message;
     }

     /**
      * Execute the job.
      *
      * @return void
      */
     public function handle()
     {
         Log::alert($this->message);
     }
}

My .env file: QUEUE_CONNECTION=database In my controller, I dispatch the event like this: eventJob::dispatch('my message'); A new record appears in the jobs table and to execute it I run php artisan queue:work The record is removed from the jobs table, but nothing appears in the file logs

I tried in the handle method and the constructor to do throw new \Exception("Error Processing the job", 1); But nothing is written in the filed_jobs table, from which I made the assumption that the handle method and the constructor do not execute. I also tried running my job like this: $job = new eventJob('my test message'); dispatch($job);

But it does not change anything

  • Does the jobs table record data include the message you are trying to log? – msmahon Dec 09 '22 at 18:51
  • Nope. It looks like this: ```{"uuid":"513f9aeb-e7a3-432c-bd4e-f618af829bc4","displayName":"App\\Jobs\\eventJob","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\Jobs\\eventJob","command":"O:17:\"App\\Jobs\\eventJob\":0:{}"}}``` – user19590807 Dec 09 '22 at 18:54
  • That suggests to me that `$message` is not getting set in your constructor. What is the content of `$message` when assigning it to `$this->message`? – msmahon Dec 09 '22 at 19:11
  • I can't handle the ```$message``` in contructor. I tried ```Log::alert($message)``` and ```dd($message)``` but nothing happens – user19590807 Dec 09 '22 at 19:13
  • It looks correct to me. Can you dump the entire job class after constructing it? Also, try removing the `SerializesModels` trait since you aren't passing in any eloquent models. – msmahon Dec 09 '22 at 19:19
  • I remove ```SerializesModels``` and do this in my controller ```$job = eventJob::dispatch('my message'); dd($job);``` It returns ```#job: App\Jobs\eventJob^ {#1009 +job: null +connection: null +queue: null +chainConnection: null +chainQueue: null +chainCatchCallbacks: null +delay: null +afterCommit: null +middleware: [] +chained: [] } #afterResponse: false }``` – user19590807 Dec 09 '22 at 19:24
  • I mean new up the job but don't dispatch it. Just dump the job to see if the class is being created with the message data. `$job = new eventJob('my message'); dd($job);` See if that looks right. – msmahon Dec 09 '22 at 19:33
  • If I understand correctly, you are asking to do like: ```$job = new eventJob('message'); dd($job);``` it returns the job class: ```App\Jobs\eventJob^ {#1010 // app/Console/Commands/Cron/CheckPaymentsStatus.php:41 +job: null +connection: null +queue: null +chainConnection: null +chainQueue: null +chainCatchCallbacks: null +delay: null +afterCommit: null +middleware: [] +chained: [] } ``` – user19590807 Dec 09 '22 at 19:49
  • Sorry, I do not know why it is not working. – msmahon Dec 09 '22 at 20:22

1 Answers1

0

I don't know why but when I changed config/queue.php file from 'default' => env('QUEUE_CONNECTION', 'sync') to 'default' => env('QUEUE_CONNECTION', 'database') everything started working as it should