I created a job in Laravel 8.x
<?php
namespace App\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\Quiz\Entities\Quiz;
class SetQuizStatus implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $quiz;
/**
* Create a new job instance.
*
* #return void
*/
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Quiz::where('publish_datetime', '>=', Carbon::now())->update([
'status' => EnumHelper::get('quiz.status.published'),
]);
}
}
Then in app\Console\Kernel.php I scheduled this job in the schedule function:
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:clean')->daily()->at('04:00');
$schedule->command('backup:run')->daily()->at('05:00');
$schedule->job(new SetQuizStatus)->everyMinute();
}
I want to change this quiz status if the publish_datetime has been reached. I run php artisan schedule:run and it's listed in php artisan schedule:list but nothing happens.
.env QUEUE_CONNECTION is set to database.
What am I missing?
I believe handle() wasn't being called when I instantiated SetQuizStatus. This fixed it and it now works. My .env file QUEUE_CONNECTION can be set to sync as well.
public function __construct()
{
$this->handle();
}
Do you run horizon? If you sent event to background you need horizon
Related
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();
}
}
Hello i have a laravel queue for save operations to do later
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 SumoCoders\Teamleader;
use Illuminate\Notifications\Messages\SlackMessage;
use Monolog\Handler\Slack;
use Illuminate\Support\Facades\Mail;
/**
* Class ProcessNotifications
*
* #package App\Jobs
* Worker for send the notifications slack / mail / teamleader in asincr way
*/
class ProcessNotifications implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $arrayDataNotification ;
protected $typeNotification;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($arrayDataNotification , $typeNotification)
{
$this->typeNotification = $typeNotification;
$this->arrayDataNotification = $arrayDataNotification;
\Log::debug('createNotif',$arrayDataNotification);
}
/**
* Execute the job.
*
* #return void
*/
public function handle($arrayDataNotification , $typeNotification)
{
//first get type of notification
\Log::debug('debug handle ',$this);
\Log::info('into handle '.$this->typeNotification);
if($this->typeNotification=='mail'){
//mail internal
}
if ($this->typeNotification=='slack'){
//notifications slack
}
if($this->typeNotification=='teamleader'){
//teamleader connection
}
}
}
For send a a new job to the queue i am using dispatch method :
$this->dispatch(new ProcessNotifications(['data1', 'data2', 'data3'], 'slack'));
I have all in ddbb in the job table , then params are ok
i setted my crontab by run schedule:run each 5 minutes, is launched ok , but on method schedule , when the method handle is called , the params are lost , and i have this in the function scheduler:
protected function schedule(Schedule $schedule)
{
Log::debug('running scheduler '.date("d-m-Y H:i:s"));
$schedule->job(ProcessNotifications::dispatch());
}
Then , the params in this point is lost, same if i run in console php artisan queue work i have :
Too few arguments to function App\Jobs`\ProcessNotifications::__construct()`
in my ddbb i have all params, but i dont know how recover or if this is the good way to call the queue ?
Ok i found, parameters under function handle not need parameters, this is serialized / unserialized automatically for the queue
Hi I have a laravel job as follows:
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 Timeline\Timeline;
class testQueue 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(Timeline $timeline, $tag)
{
$tagData = $timeline->getTagFeed($tag)->getRankedItems();
if ($tagData) {
echo "boom";
}
}
}
I'm running it via a route as follows:
Route::get('/queue', function () {
$timeline= new Timeline();
$timeline->login("test", "testpwd");
Queue::later(5, new testQueue($timeline, "Testtag"));
});
Then on the commandline I ran:
php artisan queue:listen database
However, that one job is running 255 times instead of 1 times and exiting successfully.
What am I doing wrong?
The documentation states:
Binary data, such as raw image contents, should be passed through the
base64_encode function before being passed to a queued job. Otherwise,
the job may not properly serialize to JSON when being placed on the
queue.
So you shouldn't use public function handle(Timeline $timeline, $tag) (or public function handle(Instagram $currentInstagram, $tag) in your conversation, as the Timeline or something is binary data.
Delete the job after execution.
Currently im creating a job to be queued but for some reason the failed() method is not being triggered when the code executed in the handle() method of the job throws an exception. Is this some kind of bug in Laravel or am I doing something wrong? the Queue::failing() method in my AppServiceProvider IS being called
<?php
namespace App\Jobs;
use App\Models\Email;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class MyCommand implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $lead;
/**
* Create a new job instance.
*/
public function __construct($lead)
{
$this->lead = $lead;
}
/**
* Execute the job.
*/
public function handle()
{
throw new \Exception("error!");
}
/**
* The job failed to process.
*
* #param \Exception $exception
*/
public function failed(\Exception $exception)
{
\Log::critical('wooot');
}
}
In the above job. The failed() method is never being called. We use supervisor to keep the queue running
Thank
It looks like the failed() method was being called but because of the $lead property not being public. I got an exception when I wanted to retrieve the contents of the property.
I implemented a laravel queue with the database driver, in an Ubuntu 14.04 server. I execute this code
php /path to app/artisan queue:listen --tries=3 --env=local
It says tries=3. But when I see the jobs table I see jobs with 22 attempts, how is this possible? it should try 3 times and then add it to failed_jobs table.
Also, what does reserved_at means in the jobs table?.
Thank you
Here is the job that, by the way, it works perfectly
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Notifiers\Im_Notification;
use App\Notifiers\Notification;
use App\Notifiers\Mail_Notification;
use App\Reservation;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class NotifyPlaceReservationStatus extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
protected $notification;
protected $reservation;
protected $info_changed;
public function __construct(Notification $notification,Reservation $reservation)
{
$this->reservation = $reservation;
$this->notification = $notification;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$this->notification->notifyPlaceReservationStatus($this->reservation);
}
public function failed()
{
error_log('Job failed');
}
}
When you provide the number of tries in the CLI, Laravel worker is going to apply the tries limit only for those jobs currently in the queue (or in your unexecuted job table if you prefer). In order to make every execution of a job have a try limit, you should put a public $tries attribute in your Job class as stated in the Laravel documentation on Dispatching Jobs (for Laravel 5.4)
public $tries = 3;
protected $notification;
protected $reservation;
protected $info_changed;