gpt4 book ai didi

ruby-on-rails - Resque:对时间要求严格的作业,每个用户按顺序执行

转载 作者:行者123 更新时间:2023-12-04 06:23:46 26 4
gpt4 key购买 nike

我的应用程序创建了必须由每个用户顺序处理的resque作业,并且应尽快处理它们(最大延迟为1秒)。

例如:为用户1创建作业1和作业2,为用户2创建作业3。 Resque可以并行处理job1和job3,但是job1和job2应该顺序处理。

对于解决方案,我有不同的想法:

  • 我可以使用不同的队列(例如queue_1 ... queue_10),并为每个队列启动一个工作程序(例如rake resque:work QUEUE=queue_1)。在运行时(例如,登录时,每天等)将用户分配给队列/工作程序
  • 我可以使用动态的“用户队列”(例如queue _#{user.id})并尝试扩展要求,一次只能有1个 worker 处理队列(如Resque: one worker per queue中所要求的)
  • 我可以将作业放在非重新排序队列中,并使用带有“resque-lock(https://github.com/defunkt/resque-lock)”的“每用户元作业”来处理这些作业。

  • 在实践中您对其中一种场景有任何经验吗?还是还有其他值得思考的想法?我将不胜感激,谢谢!

    最佳答案

    感谢@Isotope的回答,我终于找到了一个似乎可行的解决方案(使用resque-retry和redis锁:

    class MyJob
    extend Resque::Plugins::Retry

    # directly enqueue job when lock occurred
    @retry_delay = 0
    # we don't need the limit because sometimes the lock should be cleared
    @retry_limit = 10000
    # just catch lock timeouts
    @retry_exceptions = [Redis::Lock::LockTimeout]

    def self.perform(user_id, ...)
    # Lock the job for given user.
    # If there is already another job for the user in progress,
    # Redis::Lock::LockTimeout is raised and the job is requeued.
    Redis::Lock.new("my_job.user##{user_id}",
    :expiration => 1,
    # We don't want to wait for the lock, just requeue the job as fast as possible
    :timeout => 0.1
    ).lock do
    # do your stuff here ...
    end
    end
    end

    我在这里使用 https://github.com/nateware/redis-objects的Redis::Lock(它封装了 http://redis.io/commands/setex的模式)。

    关于ruby-on-rails - Resque:对时间要求严格的作业,每个用户按顺序执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054248/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com