Gem 问: 有没有一个能间隔一秒执行一次的 gem 呢?

towonzhou · 2013年12月02日 · 最后由 Ddl1st 回复于 2013年12月03日 · 3278 次阅读

用 goliath 的 plugin 运行的一个周期任务,周期是 1 秒

module Plugin
  class Query
    include RunQuery
    def initialize(address, port, config, status, logger)
    end

    def run
      EM.add_periodic_timer(1) do
        _run
      end
    end
  end
end

class QueryOrder < Goliath::API
  plugin ::Plugin::Query

  def response(env)
    [200, {}, "query_orders runing"]
  end
end

在_run 中会引用另外一个 gem: A gem A 中会有异步 http 请求

EM.run do
  http = EM::HttpRequest.new(url, connect_options).post(req_options)
  http.callback {......}
end

这样 gem A 就与 goliath 的周期任务冲突了,产生了 EM 的嵌套 有类似的 gem 吗?能够心跳运行,又跟 gem A 中的 EM 不冲突 求解

报错是

[:HTTP_ERROR, "connection closed by server"]

#1 楼 @Galeno 我需要一个完整的 server,还得相应其他的 http 请求呢

用 rake + whenever(crontab) 肯定会暴掉,因为每次 rake 启动的时间就不只一秒了。 建议用 whenever 每秒发个 curl request 给 server

#3 楼 @jarorwar #4 楼 @small_fish__

@lulalala 说的很对,用 curl 多了很多 http 开销,这个浪费就有点大了

#6 楼 @Ddl1st nice 这是这个

弱弱的问一下用 timers 跟 while true ....... sleep 1 end 有什么不同么?

#8 楼 @towonzhou 你看下源码把,就一百多行,没啥区别。只不过满足了基本上的需求

需要 登录 后方可回复, 如果你还没有账号请 注册新账号