关于幂等性和函数的基本 Sidekiq 问题 [英] Basic Sidekiq Questions about Idempotency and functions

查看:47
本文介绍了关于幂等性和函数的基本 Sidekiq 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Sidekiq 在后台执行一些繁重的处理.我在网上查了一下,但找不到以下问题的答案.我正在使用:

I'm using Sidekiq to perform some heavy processing in the background. I looked online but couldn't find the answers to the following questions. I am using:

Class.delay.use_method(listing_id) 

然后,在课堂上,我有一个

And then, inside the class, I have a

self.use_method(listing_id)
    listing = Listing.find_by_id listing_id
    UserMailer.send_mail(listing)
    Class.call_example_function()

两个问题:

  1. 如何使该函数对 UserMailer sendmail 具有幂等性?换句话说,如果延迟方法运行两次,我如何确保它只发送一次邮件?将它包装在这样的工作中吗?

  1. How do I make this function idempotent for the UserMailer sendmail? In other words, in case the delayed method runs twice, how do I make sure that it only sends the mail once? Would wrapping it in something like this work?

mail_sent = false如果 !mail_sentUserMailer.send_mail(listing)邮件发送 = 真结束

mail_sent = false if !mail_sent UserMailer.send_mail(listing) mail_sent = true end

我猜不是,因为再次尝试该函数,然后第二次运行时将 mail_sent 设置为 false.那么如何才能让 UserMailer 只运行一次.

I'm guessing not since the function is tried again and then mail_sent is set to false for the second run through. So how do I make it so that UserMailer is only run once.

  1. 延迟异步方法中调用的函数是否也是异步的?换句话说, Class.call_example_function() 是否异步执行(不是响应/请求周期的一部分?)如果不是,我应该使用 Class.delay.call_example_function()

总的来说,只是熟悉 Sidekiq,所以任何想法都将不胜感激.

Overall, just getting familiar with Sidekiq so any thoughts would be appreciated.

谢谢

推荐答案

关于幂等性,可以使用 https://github.com/mhenrixon/sidekiq-unique-jobs gem:

Regarding idempotency, you can use https://github.com/mhenrixon/sidekiq-unique-jobs gem:

所需要做的就是专门设置 sidekiq 选项对于独特的 true 如下所示:

All that is required is that you specifically set the sidekiq option for unique to true like below:

sidekiq_options unique: true

对于未来安排的工作,可以设置多长时间这项工作应该是独一无二的.作业将是唯一的配置的秒数或直到作业完成.

For jobs scheduled in the future it is possible to set for how long the job should be unique. The job will be unique for the number of seconds configured or until the job has been completed.

*如果您希望独特的作业在成功处理后仍然存在,那么只需将 unique_unlock_order 设置为除了 :before_yield 或 :after_yield (unique_unlock_order =:从不)

*If you want the unique job to stick around even after it has been successfully processed then just set the unique_unlock_order to anything except :before_yield or :after_yield (unique_unlock_order = :never)

我不确定我是否理解问题的第二部分 - 当您延迟一个方法调用时,整个方法调用被推迟到 sidekiq 进程.如果通过响应/请求周期"表示您正在运行 Web 服务器,并且您从那里调用 delay,那么 use_method 中的所有调用都是从sidekiq 过程,因此在该循环之外.它们相对于彼此同步调用...

I'm not sure I understand the second part of the question - when you delay a method call, the whole method call is deferred to the sidekiq process. If by 'response / request cycle' you mean that you are running a web server, and you call delay from there, so all the calls within the use_method are called from the sidekiq process, and hence outside of that cycle. They are called synchronously relative to each other though...

这篇关于关于幂等性和函数的基本 Sidekiq 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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