GonsunZan
GonsunZan

Reputation: 41

How can I use clockwork to input data every hour?

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 clockwork, but I have no idea how to use it with my project.

I wrote clock.rb and put it in my project file and ran rails s but nothing happened.

Here is 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

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

They say that I need to use $ clockwork clock.rb, but when I run rails s, there's no way to use that...

Thanks a lot.

Upvotes: 2

Views: 3465

Answers (4)

Sartaj Singh Sisodiya
Sartaj Singh Sisodiya

Reputation: 1143

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

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

Command to Run the clockword

bundle exec clockwork lib/clock.rb

Upvotes: 1

monteirobrena
monteirobrena

Reputation: 2620

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

Upvotes: 0

ASX
ASX

Reputation: 1725

As stated in the official docs 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'
      #   ...
      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')
    

    end

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

    $ gem install foreman

Create a file named 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. start your server

    $ foreman start

Upvotes: 4

Srikanth Jeeva
Srikanth Jeeva

Reputation: 3011

You can make use of 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 

Upvotes: -1

Related Questions