Anar Bayramov
Anar Bayramov

Reputation: 11594

Laravel 5.3 Queue Job is not working

I am trying to dispatch my send email action using Laravel database queue however this process still continues in my browser instead of working behind.

this is my controller

protected function importUserExcel(UploadedFile $file, Request $request){

    $user_role = Role::where('name','=','user')->first();


    \Excel::load($file, function($reader) use ($user_role) {
        $excel =  $reader->select()->get();
        foreach($excel[0] as $line){
            $user = User::firstOrnew([
                'email' => $line['email']]);
            $user->email = $line['email'];
            $user->name = $line['name'];
            $user->password= bcrypt(srand(15));

            $user->town = $line['town'];
            $user->dealer_code = $line['dealer_code'];
            $user->type = $line['type'];
          //  $user->save();
            $user->sendUserEmail();
            //$user->attachRole($user_role);


        }
    });
}

this is my model function

public function sendUserEmail()
{   
    $delay = Carbon::now()->addMinutes(15);
    \Log::info("Request Begins");
    $user = new SendEmails($this);
    $user->delay($delay);
    dispatch($user);
    \Log::info("Request Ends");
}

and this is my job

class SendEmails implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct(User $user)
{
    $this->handle($user);

}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle(User $user)
{
    $broker = $user->broker;
    $brokerInstance = \Password::broker($broker);
    view()->share('locale', app()->getLocale());
    $response = $brokerInstance->sendResetLink([ 'email' => $user->email ], function (Message $message) {
        $message->subject(trans('emails.welcome_subject'));
    });
}

}

however result seems coming eventually not delaying or queueing anything. Meanwhile my browser also process instead of putting process to behind.

enter image description here

Upvotes: 3

Views: 2181

Answers (2)

kilo636
kilo636

Reputation: 31

You can try again in the following way (I assume that you did instructions in Laravel docs but someday it's not working):

  1. drop table jobs in your database.
  2. run command php artisan migrate in console
  3. run command php artisan queue:work in console
  4. retry your app

Upvotes: 1

sisve
sisve

Reputation: 19791

Your job's constructor should not call the handle() method; it should just set properties needed for the handle method. It's up to your queue worker to call the handle method.

Your call to app()->getLocale() may be incorrect if you're setting the locale per-request; a job is executed from another process and without middlewares or an associated http request.

class SendEmails implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels;
    protected $user;

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

    public function handle() {
        $user = $this->user;
        $broker = $user->broker;
        $brokerInstance = \Password::broker($broker);

        view()->share('locale', app()->getLocale());
        $response = $brokerInstance->sendResetLink([ 'email' => $user->email ], function (Message $message) {
            $message->subject(trans('emails.welcome_subject'));
        });
    }
}

Upvotes: 2

Related Questions