0

我有一个 Ruby on Rails 应用程序,我需要在其中维护一个 id 队列。我尝试使用全局数组作为队列,但是如果我的应用程序的多个实例运行,则 Web 应用程序中的全局变量不再是全局的。那么如何维护应用程序范围的队列?

这是我的 ApplicationController 的外观:

class ApplicationController < ActionController::Base
  protect_from_forgery

  helper_method :update_queue, :get_next_free_agent
  $agent_queue = []
  def update_queue(agent)
    if agent.status == "AVAILABLE"
        if agent_queue.find_index(agent.reg_id) == nil
            $agent_queue.push(agent.reg_id)
        end
    else
        $agent_queue.delete(agent.reg_id)
    end
  end

  def get_next_free_agent
    return agent_queue.shift
  end
end

这也不起作用,在阅读了关于全局变量如何成为坏主意之后,我也不想再使用它了。请提供替代解决方案。

谢谢。

4

1 回答 1

1

从远处看,您的问题似乎可以通过像 Resque 这样的消息队列系统来解决。

如果您不想走这条路,请将数组存储在键值存储中,该存储提供原子弹出并推送数组(如redis),这样您就不必担心只有一个进程访问数组

于 2013-03-29T13:46:00.610 回答