也许这是我的任务,也许是我的调度程序,但由于某种原因,我的计划任务正在运行两次或将两个版本保存到数据库。这是我的代码:
report.rb:
def record_page_stats!
oauth = Koala::Facebook::OAuth.new
@api = Koala::Facebook::API.new oauth.get_app_access_token
@fb_page = @api.get_object(self.fb_page_id)
self.page_stats.new(like_count: @fb_page["likes"], talking_about_count: @fb_page["talking_about_count"])
self.save
end
和scheduler.rb:
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new
scheduler.every '10m' do
Report.where(tracking_page: true).each do |report|
report.record_page_stats!
end
end
我错过了一些非常明显的东西吗?这是保存的页面统计数据库的输出:
2013-12-20 18:43:06 UTC = 8304 likes and 229 talking about count
2013-12-20 18:43:08 UTC = 8304 likes and 229 talking about count
2013-12-20 18:53:06 UTC = 8304 likes and 229 talking about count
2013-12-20 18:53:08 UTC = 8304 likes and 229 talking about count
2013-12-20 19:03:06 UTC = 8304 likes and 229 talking about count
2013-12-20 19:03:08 UTC = 8304 likes and 229 talking about count
2013-12-20 19:13:06 UTC = 8304 likes and 229 talking about count
2013-12-20 19:13:08 UTC = 8304 likes and 229 talking about count
2013-12-20 19:23:07 UTC = 8304 likes and 229 talking about count
2013-12-20 19:23:33 UTC = 8304 likes and 229 talking about count
2013-12-20 19:33:06 UTC = 8304 likes and 229 talking about count
2013-12-20 19:33:32 UTC = 8304 likes and 229 talking about count
正如您在时间戳中看到的那样,它每10分钟运行两次
编辑:
我只是再次查看数据......并且调度程序已经更改了几次:
编辑2: 锁定后的新输出:
2013-12-20 22:14:22 UTC = 176155 likes and 5757 talking about count
2013-12-20 22:14:24 UTC = 176155 likes and 6994 talking about count
我的文件现在看起来像这样:
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new(:lockfile => ".rufus-scheduler.lock")
scheduler.every '10m' do
Report.where(tracking_page: true).each do |report|
report.record_page_stats!
end
log "started Scheduler #{scheduler.object_id}"
end
然而在heroku日志中有一个错误:
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 job:
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 Rufus::Scheduler::EveryJob "10m" {}
2013-12-20T22:44:42.625951+00:00 app[worker.1]: { 70149016354440 rufus-scheduler intercepted an error:
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 error:
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 70149016354440
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 ArgumentError
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 wrong number of arguments (1 for 0)
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 /app/vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/globals.rb:16:in `log'
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:224:in `call'
2013-12-20T22:44:42.626163+00:00 app[worker.1]: 70149016354440 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:224:in `do_trigger'
2013-12-20T22:44:42.626163+00:00 app[worker.1]: 70149016354440 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:269:in `block (3 levels) in start_work_thread'
2013-12-20T22:44:42.626163+00:00 app[worker.1]: 70149016354440 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:272:in `call'
2013-12-20T22:44:42.626163+00:00 app[worker.1]: 70149016354440 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:272:in `block (2 levels) in start_work_thread'
2013-12-20T22:44:42.626163+00:00 app[worker.1]: 70149016354440 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:258:in `loop'
2013-12-20T22:44:42.626163+00:00 app[worker.1]: 70149016354440 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:258:in `block in start_work_thread'
2013-12-20T22:44:42.626163+00:00 app[worker.1]: } 70149016354440 .
2013-12-20T22:44:42.625951+00:00 app[worker.1]: 70149016354440 /app/config/initializers/scheduler.rb:9:in `block in <top (required)>'
2013-12-20T22:44:44.472460+00:00 app[web.1]: { 69970029016200 rufus-scheduler intercepted an error:
2013-12-20T22:44:44.472460+00:00 app[web.1]: 69970029016200 job:
2013-12-20T22:44:44.472460+00:00 app[web.1]: 69970029016200 Rufus::Scheduler::EveryJob "10m" {}
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 ArgumentError
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 wrong number of arguments (1 for 0)
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 /app/vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/globals.rb:16:in `log'
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:224:in `call'
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:224:in `do_trigger'
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:269:in `block (3 levels) in start_work_thread'
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:272:in `call'
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:272:in `block (2 levels) in start_work_thread'
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:258:in `loop'
2013-12-20T22:44:44.473000+00:00 app[web.1]: 69970029016200 /app/vendor/bundle/ruby/2.0.0/gems/rufus-scheduler-3.0.3/lib/rufus/scheduler/jobs.rb:258:in `block in start_work_thread'
2013-12-20T22:44:44.472460+00:00 app[web.1]: 69970029016200 error:
2013-12-20T22:44:44.472460+00:00 app[web.1]: 69970029016200 69970029016200
2013-12-20T22:44:44.472615+00:00 app[web.1]: 69970029016200 /app/config/initializers/scheduler.rb:9:in `block in <top (required)>'
2013-12-20T22:44:44.473000+00:00 app[web.1]: } 69970029016200 .
答案 0 :(得分:0)
因此,根据您在问题上的标记,您将使用Ruby on Rails。
我的印象是你有两个调度程序运行(一个在第一个调度程序后2秒初始化,如日志输出所示)。
您可以轻松放置类似
的内容log "started Scheduler #{scheduler.object_id}"
在调度程序初始化之后,查看正在进行的操作。
你不会在(Webrick,Passenger,Thin,Unicorn,......等)之上提及你运行Rails的内容。其中一些运行Rails过程的多个副本。
您可以在rufus-scheduler中使用锁定系统来阻止第二个(或第三个......)调度程序启动。
对于简单的锁定机制:https://github.com/jmettraux/rufus-scheduler/#lockfile--mylockfiletxt
如果您需要更多(可能在不同的主机上运行多个Rails):https://github.com/jmettraux/rufus-scheduler/#advanced-lock-schemes
您在编辑中看到的变化可能与服务器分配新的Rails副本以应对需求有关。
答案 1 :(得分:0)
所以你在Heroku上。
这可能会有所帮助:https://devcenter.heroku.com/articles/clock-processes-ruby
如果你多搜索一下或搜索heroku文档,除了Rails过程之外,你还可以找到其他地方解释如何运行特殊进程,例如调度程序和后台工作程序。