this is my controller
SmsAdsJob::dispatch('aaa','bbb');
this is my job
protected $request;
public function __construct($request)
{
$this->request = $request;
}
public function handle()
{
$clients=$this->request;
logger($clients);
}
this is logger
local.DEBUG: aaa
Problem is in controller I dispatch two value but in job I get One value,But I should get two value in job, can you give some advise?
You Can pass your params in an array or object datatypes. For instance:
$params = ['aaa', 'bbb']
SmsAdsJob::dispatch( $params );
Or:
public function __construct($param1, $param2)
{
$this->param1 = $param1;
$this->param2 = $param2;
}
When you pass to variables you should consider that your job class must know how many params do you want to receive.
SmsAdsJob should have two properties in It's constructor. Then you can access them. I don't understand why you are using "request" in the Job
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SmsAdsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $a;
private $b;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($a,$b)
{
//
$this->a = $a;
$this->b = $b;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
echo $this->a,PHP_EOL;
echo $this->b,PHP_EOL;
}
}
Please check https://laravel.com/docs/8.x/queues
Related
When adding an item to the queue, for some reason the handle method is not being called.
The Log entry in __construct is appearing but when attempting to log in handle(), nothing appears.
The method i'm using to dispatch is ProcessImport::dispatch($path, $task->task_id);
My queue service is configured to use Redis, and redis is storing all the data accordingly.
I am using Laravel 8. What could be wrong?
<?php
namespace App\Jobs;
use App\Models\Tasks;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Http\Controllers\Products\Products;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobProcessed;
use Throwable;
class ProcessImport implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $file_path;
protected $response;
protected $task;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($path, $task_id)
{
Log::info("Importing products (construct)");
$this->task = Tasks::where('task_id', $task_id)->first();
$this->file_path = $path;
Log::info('Importing ' . $path);
}
private function getFilePath() {
return $this->file_path;
}
/**
* Handle a job failure.
*
* #param \Throwable $exception
* #return void
*/
public function failed(Throwable $exception)
{
$this->task->failed($exception->getMessage());
}
/**
* Get the cache driver for the unique job lock.
*
* #return \Illuminate\Contracts\Cache\Repository
*/
public function uniqueVia()
{
return Cache::driver('redis');
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Log::info("Importing products (handle)");
$this->task->start();
$products = new Products();
$products->importProductsFromCSV($this->getFilePath());
$this->task->success();
Log::info("End of importing products..");
}
}
You've just pushed the jobs onto the queue but haven't started a worker to process them. You can run the worker with:
php artisan queue:work
I want to pass a closure function to job dispatch method of Laravel queue. But i am getting "Serialization of 'Closure' is not allowed" exception. Can anyone please tell me how i can be able to pass closure function to the dispatch method?
Here are the controller codes:
public function syncProductByQueue($id){
$syncCallback=function() use($id){
$this->syncProduct($id);
};
ProcessSyncImport::dispatch($syncCallback);
}
And here is the job class:
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;
class ProcessSyncImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
private $syncCallBack;
public function __construct($syncCallBack)
{
$this->syncCallBack= $syncCallBack;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$syncCallBack();
}
}
I am trying to send a email in Laravel. It works finicky to say at least. got couple of classes for sending email for different conditions. Only difference would be markup of sent email and blade file which data is sent to before sending. I tried using use Mail; in controller from whom i'm sending it, with \Mail::(... and use Illuminate\Support\Facades\Mail; with Mail::(... and vice-versa. So it ain't it.
Here are the mail files:
ToHR file:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class toHR extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.toHR');
}
}
ToHRFirst file:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class toHRFirst extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.toHRFirst');
}
}
And my test file JobCreated:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class JobCreated extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.job-created');
}
}
They'll all the same, yet error occurs. What gives? Did i miss something? Any help will be appreciated, just point me in the right direction.
Code which i use to send email:
Variables used get values from queries which are rather long, but they are not an issue anyways.
Ver1:
Mail::to($user->email)->send(
new JobCreated($user,$job,$jobCount)
);
Ver2:
Mail::to($user->email)->send(
new toHRFirst($user,$job,$jobCount)
);
Ver3:
Mail::to($user->email)->send(
new toHR($user,$job,$jobCount)
);
Sending works if it's used first version. Other cannot be found and that's the error.
Try renaming your files to follow Laravel convention;
You can see that the file JobCreated has a class JobCreated. You need to respect the uppercases:
Rename the ToHR.php file into ToHr.php, then:
Rename the class class toHRinto class ToHr
Rename the ToHRFirst.php file into ToHrfirst.php, then:
Rename the class class toHRFirst into class ToHrfirst
Or if you want to keep the uppercases, just rename the classes like so:
class toHRinto class ToHR
class toHRFirstinto class ToHRFirst
Finally, don't forget to import your job class at the top of your controller file (or wherever you use the Mail method:
use App\Mail\ToHR;
and
use App\Mail\ToHRFirst;
so i have this variables in NasabahEloquent.php for my notifications.
$shohibuls = ShohibulFinance::where('barang_id','=',$submission->id)->get();
the notifications:
foreach($shohibuls as $sohib){
User::find($sohib->shohibul_id)->notify(New NasabahAkadItemToInvestor($submission,$data));
}
the notifications runs fine. but when in put it in a jobs for sending email, it return an error :
Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function notify() on null in /opt/lampp/htdocs/_pain/app/Jobs/JobForNasabahAkadItemToInvestor.php:36
this is the job dispatch on NasabahEloquent.php
foreach($shohibuls as $sohib){
$userdata = User::where('id',$sohib->shohibul_id)->get();
$AkadEndJob = (new JobForNasabahAkadItemToInvestor($userdata,$submission))->delay(Carbon::now()->addSeconds(2));
dispatch($AkadEndJob);
}
this is the handle and line 36 where the error comes from in JobForNasabahAkadItemToInvestor.php
public function handle()
{
$this->userdata->notify(New NasabahAkadItemToInvestorMail($this->submission));
}
why does my job have null on the user variables?
EDIT : the JobForNasabahAkadItemToInvestor.php file
<?php
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 App\Notifications\NasabahAkadItemToInvestorMail;
class JobForNasabahAkadItemToInvestor implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $userdata;
private $submission;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($userdata, $submission)
{
$this->$userdata = $userdata;
$this->$submission = $submission;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
//nda tau lagi dah
//FIXME:Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function notify() on null in /opt/lampp/htdocs/_pain/app/Jobs/JobForNasabahAkadItemToInvestor.php:36
$this->userdata->notify(New NasabahAkadItemToInvestorMail($this->submission));
}
}
In your __construct, you access the properties the wrong way.
$this->$userdata = $userdata;
$this->$submission = $submission;
Should be:
$this->userdata = $userdata;
$this->submission = $submission;
Notice the excess $ signs. I think that's what causes the problem.
I'm trying to create job to register users and tried to follow Jeffrey's video but looks like dispatchfrom is removed for some reason. This is what i'm trying to do now:
This is my controller:
public function PostSignUp(Request $request)
{
dispatch(new RegisterUser($request->all()));
return 'done';
}
This is my job:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class RegisterUser implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $request;
/**
* Create a new job instance.
* #param $request
* #return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = $this->request->email;
var_dump('I should register user with email:' . $email);
}
}
I also tried to put
just $request instead of $request->all()
but then i get
Serialization of 'Closure' is not allowed
And now I'm getting Trying to get property of non-object error. Is this good way to pass whole request to job ? Should i do it some other way ?
try with input()
$request->input()
When you do $request->all(), that means you are passing an array to the job and not the whole $request. Therefore, you can simply do this in your Job Handler.
public function handle()
{
$email = $this->request['email'];
var_dump('I should register user with email:' . $email);
}
Let me know if something else is required :)