Laravel 8 looped job chaining order - php

I have two jobs that need to run one after the other. These two jobs are chained, and the entire chain is run twice.
$models = array(1,2);
foreach($models as $model) {
Bus::chain([
new getRecommendations($model),
new processRecommendations($model),
])->dispatch();
}
By my understanding, this is how the queue should run:
getRecommendations(1)
processRecommendations(1)
getRecommendations(2)
processRecommendations(2)
Instead, the queue is running as below:
getRecommendations(1)
getRecommendations(2)
processRecommendations(1)
processRecommendations(2)
How do I get the jobs to run the way I need them? I don't want processRecommendations to wait until all instances of getRecommendations are done before they run.
These are the job classes:
class getRecommendations implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $model;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($model)
{
$this->model = $model;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
// do stuff
}
}
class processRecommendations implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $model;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($model)
{
$this->model = $model;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
// do stuff
}
}

Related

Getting Job Id in a Laravel queue with Database driver

I have a laravel job which I create from a controller and some times i want to delete those jobs as the timings will be rescheduled
It is a notification job which sends notification one hour before a class and if the class is rescheduled i need to delete the jobs and insert new ones
my controller code is as below
$job = (new OnlineClassRemainderJob($remainder_data))->delay($value);
$id = dispatch($job);
array_push($job_ids, $id);
and the Job class is as shown below
<?php
namespace App\Jobs;
use App\Mail\OnlineClassRemainderMail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mail;
class OnlineClassRemainderJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $details;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Mail::to($this->details['email'])
->send(new OnlineClassRemainderMail($this->details));
}
}
Kindly show light how I can get the Job id so that i can save it in a database and in case of a reschedule i can remove all job ids related to that class and then dispatch new schedules
I can layout the scenario here
I am scheduling classes for example
I have a class on every day from 20th Jan to 25th Jan at 10 AM and I am scheduling a remainder mail sending job which will fire 1 hour before the class
But at certains scenario the Classes will be rescheduled and for that purpose i need to reschedule the remainders or delete and redispatch the jobs
you need to retrun job id from job with this function $this->job->getJobId();
class OnlineClassRemainderJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Mail::to($this->details['email'])
->send(new OnlineClassRemainderMail($this->details));
retrun $this->job->getJobId();
}
}
then
$job = (new OnlineClassRemainderJob($remainder_data))->delay($value);
$id = dispatch($job);
array_push($job_ids, $id);
ref link https://laravel.com/api/8.x/Illuminate/Contracts/Queue/Job.html#method_getJobId
To take the id of a job before executing it, e.g. to dequeue it before executing it.
$log = UpdateLog::create();
UpdateProduct::dispatch($log)->delay(now()->addMinutes(10));
$regex = 'UpdateLog.*?id\\\\";i:'.$log->id.';';
//delete or get job before start
DB::table('jobs')->where('payload','REGEXP',$regex)->delete();
my job example
//...
class UpdateProduct implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* #var UpdateLog
*/
protected $log;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(UpdateLog $log)
{
$this->log = $log;
}
//...
}

Laravel 5.4 Type error: Too few arguments to function. 0 passed and exactly 1 expected in

I am using Laravel 5.4 and I want to implement queue in sending emails. I have a function of register as
public function register(CustomerRequest $request)
{
\Log::info("Request Cycle with Queues Begins");
$parameter = 'This is parameter';
dispatch(new SendWelcomeEmail($parameter));
\Log::info("Request Cycle with Queues Ends");
}
and I have created a job inside App\Jobs\SendWelcomeEmail. Which looks like below:
class SendWelcomeEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
#
}
/**
* Execute the job.
*
* #return void
*/
public function handle($parameter)
{
Log::info($parameter);
}
}
Why am I getting error like
Type error: Too few arguments to function App\Jobs\SendWelcomeEmail::handle(), 0 passed and exactly 1 expected in
$parameter not for handle() method, but for its constructor
class SendWelcomeEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $parameter;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($parameter)
{
$this->parameter = $parameter;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Log::info($this->parameter);
}
}

laravel how to use serviceprovider in job

I have a service provider app/Providers/MailchimpServiceProvider.php.
I have added it to providers in config/app.php
Now I would like to use it in a Job:
class SendMail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Mailchimp $mailchimp)
{
dd($mailchimp);
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
}
}
I think the DI only works in the controllers. How would I use the mailchimp singleton within the job?
You can inject your dependencies in the handle method:
public function handle(Mailchimp $mailchimp)
{
}

Running Task scheduler on Queues Laravel

//Mail Verification File
class EmailVerification extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
protected $user;
public function __construct($user)
{
$this->user = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('frontend.email.activation')->with([
'email_token' => $this->user->email_token,
]);
}
}
//SendVerificationEmail
class SendVerificationEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
protected $user;
public function __construct($user)
{
$this->user = $user;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = new EmailVerification($this->user);
Mail::to($this->user->email)->send($email);
}
}
i am currently working on this simple laravel projects. I am try to make use of queues to implement the user email activation function. and it works fine when i use php artisan queue:work to run it. i now tried to create an task scheduler to do this every five minutes
// The code Here
protected function schedule(Schedule $schedule)
{
$schedule->job(new SendVerificationEmail($user))->everyFiveMinutes();
}
But its returning undefined variable. Is there a mistake in the above, or is there a better way to make Queues run automatically?
Please read: Laravel 5.5 Documentation - Queues#dispatching-jobs
You can't schedule this kind of job, you should dispatch it.

Laravel [5.3|5.4] testing dispatch of job within a job

I'm struggling to understand how I could test a dispatch of a job within another job. I'll provide a code sample.
This is my main job class, we can call it father
final class FatherJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
\Log::info("Hello World, I'm the father.");
dispatch(new ChildJob());
}
}
Then we have the child job
final class ChildJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
\Log::info("I'm the child");
}
}
The test has been set as following
final class JobTest extends TestCase
{
/** #test */
public function it_has_been_dispatched()
{
$this->expectsJobs(ChildJob::class);
dispatch(New FatherJob ());
}
}
This test fails, of course that's the whole point of the question, but why?
I've done some digging and I presume that the problem relies on the call withoutJobs() inside expectsJobs(), it seems that withoutJobs() distrupt the current queue thus it doesn't allow to call the rest of the jobs but maybe I am totally off track.
If this logic is intended, how can I create a test suite that allows me to check if the job within a job has been called?
Thank you in advance.
expectsJobs mock all jobs engine. You can't use dispacth().
$this->expectsJobs(ChildJob::class);
$job = new FatherJob();
$job->handle();

Categories