繁体   English   中英

如何每小时使用发条输入数据?

[英]How can I use clockwork to input data every hour?

我的环境是带有Ruby 2.0和Rails 4的Macbook OSX 10.7 Lion。

我是RoR的新手。 我使用脚手架建立了一个带有一个列数据库的网站。
这只是一个关于天气的小网站。
我想每小时输入一次数据库。
我找到了宝石发条 ,但我不知道如何在我的项目中使用它。

我写了clock.rb并把它放在我的项目文件中并运行rails s但没有任何反应。

这是myproject / clock.rb

MyProject的/ clock.rb

require 'clockwork'
module Clockwork

handler do |job|
puts "Running #{job}"

every(1 hours, ''){
Mydata.create(:degree => input_data)
}
end

我应该怎么做或者我应该把文件放在哪里?

他们说我需要使用$ clockwork clock.rb ,但是当我运行rails s ,没有办法使用它...

非常感谢。

正如官方文档中所述,您需要做一些事情。

  1. 设置时钟文件,我在app/clock.rb设置它

    需要'发条'模块发条

     handler do |job| case job when 'weather.input_degree' Mydata.create degree: input_data # when 'some_other_task' # ... else puts "Couldn't find your job!" end end every(1.hour, 'weather.input_degree') # the string indicates an arbitrary job name # every(20.seconds, 'weather.some_other_task') 

    结束

  2. 启动过程。 关键是使用像Foreman这样的东西

    $ gem安装工头

在应用程序的根目录中创建一个名为Procfile的文件:

web: bundle exec rails -s
clock: bundle exec clockwork app/clock.rb
# any_other_service_you_want: bash script here
  1. 启动你的服务器

    $工头开始

添加到Gemfile

gem 'clockwork'

在gemfile中添加上面列出的gem之后,使用bundle install将这个gem添加到项目中。

在/ lib目录下创建一个文件clock.rb

clock.rb

require File.expand_path('../../config/boot', __FILE__)

require File.expand_path('../../config/environment', __FILE__)

require 'clockwork'

include Clockwork

module Clockwork

## Here Student is a domain class, having a method insertRecord to

## insert a record in DB

every(20.seconds, 'job ­ Inserting Record in DB') { Student.insertRecord }

end

命令运行时钟字

bundle exec clockwork lib/clock.rb

你可以利用Cron 编写一个rake任务并从Cron调用rake任务以重复进程。

示例(将其添加到cron以每小时运行一次任务):

0 * * * * cd /home/projectdir;rake do:task 

此代码需要保留在处理程序块之外:

every(1.hour, ''){ Mydata.create(:degree => input_data) }

你可以这样执行时钟:

bundle exec clockwork clock.rb

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM