Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Queued Job, Execute it before its execution time

Tags:

php

mysql

laravel

I created a Laravel job to be executed on a specific date and time (e.g., tomorrow). I want to add a manual button that overrides the time and executes that queued job before its set execution time. Clicking the button creates an ajax call and send a job ID to the server. This then leads to the job being executed today instead of tomorrow.

We can manually retry failed jobs using the following command: php artisan queue:retry JOBIDHERE

I am not sure what to use for executing a queued job.

I can get the job ID, but I do not know if it is possible to execute the Laravel Job before its set execution time.

I searched on Google but did not found anyone with such problem and solution.

I am using Laravel Ver 5.8. Using Mysql 5.7


Update:

Following is the payload for the queued Job.

I tried to use Json Decode and decoded it, but I am not sure if can update the command for that queue so that I can update the date and time for the queue and save it back to the queued job record.

{"displayName":"App\\Jobs\\Payway\\UpdateCustomerInvestment","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"delay":null,"timeout":null,"timeoutAt":null,"data":{"commandName":"App\\Jobs\\Payway\\UpdateCustomerInvestment","command":"O:40:\"App\\Jobs\\Payway\\UpdateCustomerInvestment\":17:{s:57:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000transactionType\";s:7:\"payment\";s:57:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000principalAmount\";d:9999;s:56:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000customerNumber\";s:4:\"BR-2\";s:50:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000currency\";s:3:\"aud\";s:58:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000singleUseTokenID\";N;s:55:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000payway_helper\";O:29:\"App\\Http\\Helpers\\PaywayHelper\":0:{}s:54:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000impodenceKey\";s:36:\"afedfc34-d08e-4831-a4aa-29de930d6b98\";s:49:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000headers\";a:0:{}s:60:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000localInvestmentObj\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:33:\"App\\Models\\Investment\\Investments\";s:2:\"id\";i:374;s:9:\"relations\";a:2:{i:0;s:8:\"investor\";i:1;s:13:\"investor.user\";}s:10:\"connection\";s:5:\"mysql\";}s:54:\"\u0000App\\Jobs\\Payway\\UpdateCustomerInvestment\u0000paywayTotals\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:38:\"App\\Models\\Banking\\Payway\\PaywayTotals\";s:2:\"id\";i:1;s:9:\"relations\";a:0:{}s:10:\"connection\";s:5:\"mysql\";}s:6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";s:6:\"payway\";s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";O:13:\"Carbon\\Carbon\":3:{s:4:\"date\";s:26:\"2019-11-12 23:35:22.752222\";s:13:\"timezone_type\";i:3;s:8:\"timezone\";s:16:\"Australia\/Sydney\";}s:7:\"chained\";a:0:{}}"}}

Update 2:

When I deserialized the payload Command, I got the following information.

enter image description here

So I am trying to update that delay date, hopefully it will work.

But from the answer of "Julian Stark", I may have to update the available_at as well.

My theory is when the queue runs, it will look for jobs based on available_at. However, when the job is executing and it it has a delay, it might not execute at that specific time. This theory is yet to be tested.

I will update both of these dateTimes and check if everything works smoothly.

like image 557
Sizzling Code Avatar asked Nov 08 '19 18:11

Sizzling Code


People also ask

How do I know if my Laravel queue is working?

$this->mailer->queue($view, $data, function ($message) use ($toEmail, $toName, $subject) { $message ->to($toEmail, $toName) ->subject($subject); }); This will successfully run, but if the queue is not 'listening', the job gets pushed on to the job table, forever. I am looking for something like \Queue::isListening();

Is Laravel queue asynchronous?

php - Laravel Jobs are not asynchronous - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

How does queue work in Laravel?

Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time.


1 Answers

In a similar use case, I just updated the jobs available_at time, so the queue:listen command executes the job as soon as possible.

DB::table('jobs')->where('id', $jobId)->update(['available_at' => time()]);

I don't know if this is the correct way to do it, but it worked for me

like image 140
julianstark999 Avatar answered Sep 22 '22 06:09

julianstark999