Reputation: 136
I need to read some data from external API every 10 seconds and save them to my database. I have tried doing it with cron job - gem whenever, but it doesn't allow time intervals smaller than 1 minute. What is the best way in Rails 4+ to run some background job every few seconds?
Upvotes: 1
Views: 965
Reputation: 1636
Just use resque
with resque-scheduler
to run those background jobs.
For example, given this job:
class ReadDataFromApiJob
def self.perform
data = read_from_api
save(data)
end
def read_from_api
# ...
end
def save(data)
# save to db
end
end
You can create a scheduled job to run it every 10 seconds in a config/resque_schedule.yml
file:
---
ReadDataFromApiJob:
every:
- 10s
class: ReadDataFromApiJob
queue: my_queue
description: This job will read and save data from the api every 10 seconds
Upvotes: 3