Laravel JSON API的可重复工作 [英] Repeatable job for Laravel json api

查看:114
本文介绍了Laravel JSON API的可重复工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MySQL上编写Laravel json API.想象一下,用户在我的数据库中创建了记录.我要做的是在创建此记录后24小时执行某种操作.我可以使用:AfterInsertJob::dispatch()->delay(now()->addHours(24));来实现.但是我需要一次又一次地执行此操作,直到记录存在.那我该怎么办呢?我应该在工作内部派遣工作吗?我的意思是我应该在AfterInsertJob内部分派AfterInsertJob吗?我可以使用Schedule,但是如何从AfterInsertJob停止该计划?

I am writing Laravel json API on MySQL. Imagine user creates record inside my database. What I want to do is perform some kind of operation 24 hours after this record is created. I can achieve this using : AfterInsertJob::dispatch()->delay(now()->addHours(24));. But I need to perform this operation again and again until record exists. So how can I do that? Should I dispatch job inside job? I mean Should I dispatch AfterInsertJob inside AfterInsertJob? I could use Schedule but then how to stop that schedule from AfterInsertJob?

推荐答案

为什么对任务使用作业.只需运行计划的命令即可检查您有什么要求.

Why using a job to to the Task. Just run a scheduled command which checks whatever requirement you have.

在用户创建的记录上添加标志.在您编写的命令中检查此标志并执行所需的任务.如果用户执行他需要做的任何事情,请设置该标志,您的命令将忽略此条目.如果没有,它将一直进行到设置为止.

Add a flag on the record the user creates. Check this flag in the command you write and perform the task required. If the user does whatever he needs to to, set the flag and your command will ignore this entry. If not it will be done until it is set.

如果在创建后恰好需要24小时,则可以检查创建日期(以及该日期的倍数)和标记. 但是还有其他方法.

If it is required exactly 24 hours after creation you can check creation date (and multiple of that) and the flag. But there are surly other ways too.

更新:

阅读您的评论后,我有另一个想法可以更好地满足您的需求.您可以使用两件事.

After reading your comment, I have another idea which can suit your needs better. You can use two things.

  1. 延迟
  2. 重试次数/最大尝试次数

延迟 是一种称为为什么要调度"的方法

delay is a method called why dispatching

最大尝试次数 是"tries"

max attempts is a proptery of the job class by the name of ´tries`

您的作业将检查它必须检查的所有内容,如果用户未满足要求,您将让该作业失败.如果是这样,它将以24小时的延迟被推回到队列中.重复此过程,直到达到最大尝试次数"为止.

Your job will check whatever it has to check, and if the requirements are NOT met by the user you will let the job fail. If that is so it will be pushed back onto the queue with the delay of 24 hours. This will repeat until the number of "max attempts" has been reached.

这篇关于Laravel JSON API的可重复工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆