使用 Gearman 推迟作业队列

发布于 2024-10-02 15:08:56 字数 381 浏览 5 评论 0原文

我想将一些耗时的事情提取到队列中。为此,我发现 Gearman 是最常用的,但不知道它是否适合我。 我们要做的任务之一是排队发送电子邮件,并希望提供能够取消1分钟发送邮件的功能。因此,它不应该立即执行该作业,而应该在现在 + 1 分钟后执行。这样我就可以在此之前取消该作业,并且它永远不会被发送。

有办法做到这一点吗?

它将在 debian 上运行。并且应该可以从 php 使用。到目前为止我发现的唯一一件事是 安排一个Gearman 中的工作在特定日期和时间,但运行的内容并未广泛传播:(

I want to extract some of the time consuming things into a queue. For this I found Gearman to be the most used but don't know if it is the right thing for me.
One of the tasks we want to do is queue sending emails and want to provide the feature to be able to cancel to send the mail for 1 minute. So it should not work on the job right away but execute it at now + 1 minute. That way I can cancel the job before that and it never gets sent.

Is there a way to do this?

It will run on debian. And should be usable from php. The only thing I found so far was Schedule a job in Gearman for a specific date and time but that runs on something not widely spread :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2024-10-09 15:08:56

您的问题分为两个部分:(1) 将来的安排和 (2) 能够取消该作业直到那时。

对于 (1) at 应该可以按照该问题中指定的方式正常工作,该人甚至发布了他的包装器代码。你尝试过吗?

如果您不想使用它,请考虑以下场景:

  • 在数据库中插入要发送的电子邮件的电子邮件记录,其中包括“timeSent”列,您将在未来设置 1 分钟。
  • 有一个 gearman 工作人员(我将解释为什么是单身)查看数据库中尚未发送的电子邮件(例如某些 status 列 = 0)以及 timeSent 的位置已经过去了,并发送那些。

因此,对于 (2),如果您想在发送之前取消电子邮件,只需将其 status 列更新为其他内容即可。

您的 gearman 工作人员必须是一名工作人员,因为如果您有多个工作人员,他们可能会获取并尝试发送相同的电子邮件记录。如果您需要多个,请确保首先获取电子邮件记录的记录在任何耗时的操作(例如实际发送电子邮件)之前立即锁定它(例如,通过将 status 列更新为其他内容)。

There are two parts to your question: (1) scheduling in the future and (2) being able to cancel the job until that time.

For (1) at should work just fine as specified in that question and the guy even posted his wrapper code. Have you tried it?

If you don't want to use that, consider this scenario:

  • insert an email record for the email to-be-sent in a database, including a "timeSent" column which you will set 1 minute in the future.
  • have a single gearman worker (I'll explain why single) look at the database for emails that have not been sent (eg some status column = 0) and where timeSent has already passed, and send those.

So, for (2), if you want to cancel an email before it's sent just update its status column to something else.

Your gearman worker has to be a single one because if you have multiple they might fetch and try to send the same email record. If you need multiple make sure the one that gets the email record first locks it immediately before any time consuming operations like actually emailing it (say, by updating that status column to something else).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文