0

Laravel 7.2.2

我正在现有工作中使用 Laravel 创建工作。触发第一个作业可以正常工作,一旦触发第二个作业,它就会崩溃并出现以下错误:

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

意思是我没有通过任何参数,而预期有 3 个参数。我正在使用以下代码来解雇这项工作:

    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();
                    }
                }
            }
            ...
            }
        }
    }
}

我在作业 CreateAlarm: 中传递了 3 个变量$this->sid$trigger->id并且Measuerment::max('id')

这是 CreateAlarm 的样子:

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();
            }
        }
    }
}

我添加了每个变量protected $var,然后在 __construct 中构造它。

我找不到我做错了什么以及为什么工作失败

4

1 回答 1

1

dispatch使用与 Laravel v5.5 不同的使用方式

尝试使用这样的东西:

CreateAlarm::dispatch($this->sid, $trigger->id, Measurement::max('id'));
于 2020-07-08T13:16:07.610 回答