11

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.

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

3 Comments

Updated my question with new findings. Please check those out. Thankyou for mentioning about the available_at.
I tested my example in laravel 5.8 without modifying the payload delay and the job executes, so your theory mentioned in Update 2 is wrong
I'd change time() to DB::raw('NOW()') just to be sure.
5
+100

To achieve this, all you have to do is first decode your payload which will be

{
   "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:{}}"
   }
}

When you have decoded your payload, you'll see the "command" is serialized, so next you've to unserialize "command".

[delay] => Array
        (
            [date] => 2019-11-12 23:35:22.752222
            [timezone_type] => 3
            [timezone] => Australia/Sydney
        )

After that, you can then update the date under the delay node and either using Carbon or PHP date, update the date as per your need. After doing that, you just need to serialize the "command" node again and then json_encode the whole payload and save it.

You'll also need to follow @Julian Stark's answer as well, that is, modify the available_at of that job.

I hope this helps.

1 Comment

I followed the same process already and updated both dates. Already Working flawlessly.
1

updating the time of queued job will be the bad practice instead of that you can flush the scheduled job and run dispatch now function.

1 Comment

That can also work. Thou i am not sure why the other method is a bad practice? Both methods seems fine to me, Updating the date directly or deleting existing and creating job again. But i wanted to look how we can update the date/time of existing job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.