phplaravellaravel-queuelaravel-jobs

Laravel Job not seeing passed variables


Laravel 7.2.2

I am creating a job with Laravel inside an existing job. Firing the first job works fine, once it's fires the second job it crashed giving the following error:

Too few arguments to function App\Jobs\CreateAlarm::__construct(), 0 passed in C:\xxx\Dispatchable.php on line 17 and exactly 3 expected in C:\xx\Jobs\CreateAlarm.php:29

It's saying that I didn't pass any arguments while there are 3 expected. I'm using the following code to fire this job:

    class ProcessMeasurement implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $sid;

    protected $val1;

    protected $val2;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($sid, $val1, $val2)
    {
        $this->sid = $sid;
        $this->val1 = $val1;
        $this->val2 = $val2;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $measurement = new Measurement;
        $measurement->sensor_id = $this->sid;
        $measurement->value_1 = $this->val1;
        $measurement->value_2 = $this->val2;
        $measurement->save();
        // Check for triggers
        $triggers = SensorTrigger::where('sensor_id', $this->sid)->get();
        foreach ($triggers as $trigger) {
            // check if alarm must be triggered
            if ($trigger->operator == '>') {
                // use > operator
                if ($trigger->affected_value == 1) {
                    // check value 1
                    if ($measurement->value_1 > $trigger->operand) {
                        // trigger alarm
                        $job = (new CreateAlarm($this->sid, $trigger->id, Measurement::max('id')));
                        $job->dispatch();
                    }
                }
                if ($trigger->affected_value == 2) {
                    // check value 2
                    if ($measurement->value_2 > $trigger->operand) {
                        // trigger alarm
                        $job = (new CreateAlarm($this->sid, $trigger->id, Measurement::max('id')));
                        $job->dispatch();
                    }
                }
            }
            ...
            }
        }
    }
}

I'm passing 3 variables in the job CreateAlarm: $this->sid, $trigger->id and Measuerment::max('id')

This is what the CreateAlarm looks like:

class CreateAlarm implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $sid;

    protected $trigger_id;

    protected $measurement_id;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($sid, $trigger_id, $measurement_id)
    {
        $this->sid = $sid;
        $this->trigger_id = $trigger_id;
        $this->measurement_id = $measurement_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Store alarm in database
        $alarm = new Alarm;
        $alarm->trigger_id = $this->trigger_id;
        $alarm->trigger_type = 'automatic';
        $alarm->measurement_id = $this->measurement_id;
        $alarm->organisation_id = Sensor::where('id', $this->sid)->organisation_id;
        $alarm->sensor_id = $this->sid;

        // check who to call
        $sensorContacts = SensorContact::where('sensor_id', $this->sid)->where('phone_priority', 0)->get();
        $alarm_id = Alarm::max('id');
        foreach ($sensorContacts as $sensorContact) {
            if ($sensorContact->phone_active == 1) {
                // Create call job
                $callJob = (new ProcessAlarmCall($sensorContact->contact_id, $alarm_id));
                $callJob->dispatch();
            }
            if ($sensorContact->email_active == 1) {
                // Create mail job
                $mailJob = (new ProcessAlarMEmail($sensorContact->contact_id, $alarm_id));
                $mailJob->dispatch();
            }
            if ($sensorContact->whatsapp_active == 1) {
                // Create whatsapp job
                $whatsappJob = (new ProcessAlarmWhatsapp($sensorContact->contact_id, $alarm_id));
                $whatsappJob->dispatch();
            }
        }
    }
}

I added every variable as protected $var and then constructed it in the __construct.

I can't find what I did wrong and why the job fails


Solution

  • dispatch using different way to use from Laravel v5.5

    Try use something like this :

    CreateAlarm::dispatch($this->sid, $trigger->id, Measurement::max('id'));