2
votes

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

2
Could you post full of your controller function?Jonny Vu
ypdated @VũTuấnAnhAnar Bayramov
Your code seem to be fine except your log. Try to push your log to SendEmails class. If you push Logger into your model, you could not see the delay time. By the way, in your controller functions should return View to send the response to browserJonny Vu
I know @VũTuấnAnh just trying to figure out queue thing then I will add view also logs are same in other side too.Anar Bayramov

2 Answers

1
votes

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'));
        });
    }
}
0
votes

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