¿Cómo puedo usar Clockwork para ingresar datos cada hora?

My environment is Macbook OSX 10.7 Lion with Ruby 2.0 and Rails 4.

I am new to RoR. I built a website with one column database using a scaffold.
It's just a little website about weather.
I want to input degrees to database every hour.
And I found the gem aparato de relojería, but I have no idea how to use it with my project.

Yo escribí clock.rb and put it in my project file and ran rails s Pero nada pasó.

Here is myproject/clock.rb


require 'clockwork'
module Clockwork

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

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

What should I do with it or where should I put the file?

They say that I need to use $ clockwork clock.rb, pero cuando corro rails s, there's no way to use that...

Muchas gracias.

preguntado el 27 de noviembre de 13 a las 05:11

I don't know why they gave you a -1, but I'll try to answer to your question, even if it's really late :) -

4 Respuestas

Como se indica en el documentos oficiales you need a couple of things.

  1. setup the clock file, I've set it in app/clock.rb

    require 'clockwork' module Clockwork

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


  2. The starting process. The key is to use something like Capataz

    $ gem install foreman

Crea un archivo llamado Procfile in root of your app:

web: bundle exec rails -s
clock: bundle exec clockwork app/clock.rb
# any_other_service_you_want: bash script here
  1. inicia tu servidor

    $ capataz de inicio

respondido 25 mar '14, 18:03

Add to Gemfile ­

gem 'clockwork'

after adding upper listed gem in gemfile, use bundle install to add this gem into project.

Example ­

Create a file clock.rb under /lib directory


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 }


Command to Run the clockword

bundle exec clockwork lib/clock.rb

Respondido el 20 de junio de 20 a las 10:06

This code needs stay outside of handler block:

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

You can execute the clock like that:

bundle exec clockwork clock.rb

Respondido 17 Abr '14, 14:04

Puedes hacer uso de cron. Write a rake task and call the rake task from the Cron for repeating processes.

Example (Add this to cron to run the task every hour):

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

respondido 27 nov., 13:06

That's really the suggested way to do it modern application development. More on: adam.heroku.com/past/2010/4/13/rethinking_cron - Adit Saxena

la pregunta es sobre clockwork gem. your answer is not really answers the question. because Cron is not useful in distributed systems. - Asad Ali

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.