简体   繁体   中英

laravel 5.1 queue job delay doesn't work

i trying to set a reminder via laravel 5.1 queue service, the job executes and works fine, the only problem it's that it not delaying the job, i'm not sure if i'm doing it the right way, can someone tell me what am i doing wrong please? my SetReminder job class looks like that

namespace LM2\Jobs;

use LM2\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use LM2\Models\Lead;
use LM2\Models\User;

class SetReminder extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;

public $user;
public $lead;

public function __construct(User $user, Lead $lead)
{
    $this->user = $user;
    $this->lead = $lead;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    $user = new User(['client_id' => 1, 'name'=>'asdasd', 'password' => '123' , 'email' => microtime().'@gmail.com']);
    $user->save();
 }
}

and the controller function calling that job is:

 public function setReminder($lead_id, Request $request)
{
    $due_time = $request->input('due_time');
    $message = $request->input('message');
    $user = \Auth::user();
    $lead = Lead::where('_id',$lead_id)->first();
    $lead->reminders = ['message' => $message , 'due_time' => $due_time, 'user_id' => $user->id, 'active' => 1];
    $lead->save();
    $job = (new SetReminder(
            \Auth::user(),
            Lead::find($lead_id)
        ))->delay(10);
    $this->dispatch($job);
    return $lead;
}

before calling the function i ran php artisan queue:listen at the terminal. thanks a lot everyone :)

ps the new user inserted in the handle function it's just for testing it.

You probablly have in the env file QUEUE_DRIVER=sync . It wont work with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM