laravel how to use serviceprovider in job - php

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)
{
}

Related

Laravel 8 looped job chaining order

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
}
}

Handle Method not being called in Laravel Redis Queue

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

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);
}
}

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