【问题标题】:update MongoDB for every 30sec with remote JSON data?使用远程 JSON 数据每 30 秒更新一次 MongoDB?
【发布时间】:2013-04-27 18:13:26
【问题描述】:

我正在将远程 JSON 数据解析到 MongoDB,实际上我正在解析动态 JSON 数据,但我想每 30 秒用动态数据更新 MongoDB。

像这样解析 JSON 数据

require 'open-uri'

require 'json'

result = JSON.parse(open("url_of_json_service").read)

如何每 30 秒更新一次 MongoDB?

【问题讨论】:

    标签: ruby-on-rails json mongodb


    【解决方案1】:

    使用 rufus-schedular gem,它工作正常。

    在 Gemfile 中

    gem 'rufus-scheduler', :require => "rufus/scheduler"
    

    在 config/initializers/reminder_sheduler.rb 中

    scheduler = Rufus::Scheduler.start_new
    
    scheduler.cron("0 5 * * *") do
      Model.send_reminder_email
    end
    

    【讨论】:

      【解决方案2】:

      Cron 是重复作业的常用解决方案,您可以从 cron-job 启动脚本/运行程序 Rails 脚本或 rake 任务。

      我应该提到,对于 Cron,最精细的粒度是 1 分钟。

      另一种解决方案是创建一个后台作业,它以Daemon 运行,并且基本上运行一个连续循环:加载远程 JSON,休眠 30 秒,加载远程 JSON,休眠,...

      查看这些 Railscast:

      http://railscasts.com/episodes/164-cron-in-ruby-revised

      http://railscasts.com/episodes/164-cron-in-ruby

      http://railscasts.com/episodes/129-custom-daemon

      Cron:

      http://en.wikipedia.org/wiki/Cron

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-07
        • 2017-11-08
        • 2012-06-13
        • 2011-11-06
        • 2014-05-03
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        相关资源
        最近更新 更多