Laravel排队的作业,在其执行时间之前执行

Siz*_*ode 8 php mysql laravel

我创建了一个Laravel作业,要在特定的日期和时间(例如,明天)执行。我想添加一个手动按钮来覆盖时间,并在设置的执行时间之前执行该排队的作业。单击该按钮将创建一个ajax调用,然后将作业ID发送到服务器。然后,这导致工作在今天而不是明天执行。

我们可以使用以下命令手动重试失败的作业: php artisan queue:retry JOBIDHERE

我不确定用于执行排队作业的内容。

我可以获得作业ID,但是我不知道是否可以在其设置的执行时间之前执行Laravel作业。

我在Google上进行了搜索,但没有找到有此类问题和解决方案的人。

我正在使用Laravel Ver 5.8。使用MySQL 5.7


更新:

以下是排队作业的有效负载。

我尝试使用Json Decode并将其解码,但是我不确定是否可以更新该队列的命令,以便可以更新队列的日期和时间并将其保存回排队的作业记录。

{"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:{}}"}}
Run Code Online (Sandbox Code Playgroud)

更新2:

当我反序列化有效负载命令时,我得到以下信息。

在此处输入图片说明

因此,我正在尝试更新该延迟日期,希望它会起作用。

但是,根据“朱利安·斯塔克”的答案,我可能还必须更新available_at。

我的理论是,当队列运行时,它将根据寻找工作available_at。但是,当作业正在执行并且有延迟时,它可能不会在该特定时间执行。该理论尚待检验。

我将更新这两个dateTimes并检查是否一切正常。

Jul*_*n S 11

在类似的用例中,我只是更新了作业available_at时间,因此该queue:listen命令将尽快执行作业。

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

我不知道这是否是正确的方法,但是对我有用


Fah*_*Ali 5

为了实现这一点,您所要做的就是首先解码您的有效负载,这将是

{
   "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:{}}"
   }
}
Run Code Online (Sandbox Code Playgroud)

当您解码有效负载后,您将看到“command”已序列化,因此接下来您必须反序列化“command”

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

之后,您可以更新延迟节点下的日期,并使用 Carbon 或 PHP 日期,根据您的需要更新日期。完成此操作后,您只需再次序列化“命令”节点,然后序列化json_encode整个有效负载并保存它。

您还需要遵循@Julian Stark 的答案,即修改该available_at作业的 。

我希望这有帮助。