Laravel Scheduler运行队列

时间:2018-07-24 15:27:58

标签: php laravel cron queue jobs

您好,我有一个laravel队列,可以稍后进行保存操作

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 SumoCoders\Teamleader;
use Illuminate\Notifications\Messages\SlackMessage;
use Monolog\Handler\Slack;
use Illuminate\Support\Facades\Mail;

/**
 * Class ProcessNotifications
 *
 * @package App\Jobs
 * Worker for send the notifications slack / mail / teamleader in asincr way
 */

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

    protected  $arrayDataNotification ;
    protected  $typeNotification;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($arrayDataNotification , $typeNotification)
    {
        $this->typeNotification = $typeNotification;
        $this->arrayDataNotification = $arrayDataNotification;
        \Log::debug('createNotif',$arrayDataNotification);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle($arrayDataNotification , $typeNotification)
    {
        //first get type of notification
        \Log::debug('debug handle ',$this);
        \Log::info('into handle '.$this->typeNotification);

        if($this->typeNotification=='mail'){
        //mail internal

        }

        if ($this->typeNotification=='slack'){
        //notifications slack
        }

        if($this->typeNotification=='teamleader'){
        //teamleader connection
        }

    }
}

使用调度方法将新作业发送到队列中

$ this-> dispatch(new ProcessNotifications(['data1','data2','data3'],'slack')); 我把所有的东西放在工作表的ddbb中,然后参数就可以了

我通过运行schedule设置了我的crontab:每5分钟运行一次,启动正常,但是在方法schedule上,当调用方法句柄时,参数丢失,并且我在函数调度程序中拥有此函数:   受保护的功能时间表(时间表$ schedule)     {

    Log::debug('running scheduler '.date("d-m-Y H:i:s"));
$schedule->job(ProcessNotifications::dispatch());

}

然后,在这一点上的参数丢失了,如果我在控制台php artisan队列工作中运行,则同样如此:

Too few arguments to function App\Jobs`\ProcessNotifications::__construct()`

在我的ddbb中,我具有所有参数,但是我不知道该如何恢复,或者这是否是调用队列的好方法?

1 个答案:

答案 0 :(得分:0)

好吧,我发现,函数句柄下的参数不需要参数,这会自动对队列进行序列化/未序列化