Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a background job every few seconds

Say I have an application that needs to pull data from an API, but that there is a limit to how often I can send a query (i.e., caps at X requests / minute). In order to ensure I don't hit this limit, I want to add requests to a queue, and have a background job that will pull X requests and execute it every minute. I'm not sure what's the best method for this in Rails, however. From what I gather, DelayedJob is the better library for my needs, but I don't see any support for only running X jobs a minute. Does anyone know if there is a preferred way of implementing functionality like this?

like image 319
user1769426 Avatar asked Dec 25 '22 12:12

user1769426


2 Answers

I'm a little late but I would like to warn against using the whenever gem in your situation: Since you're using Ruby on Rails, using the whenever gem will be loading the environment each time it gets called in cron.

Give rufus-scheduler a try.

Place the code below, for example, in config/initializers/cron_stuff.rb

require 'rufus/scheduler'

scheduler = Rufus::Scheduler.start_new

scheduler.every '20m' do
  puts 'hello'
end
like image 72
Abdo Avatar answered Feb 08 '23 06:02

Abdo


First, I would recommend using Sidekiq for processing background jobs. It's well supported and very simple to use. If you do use Sidekiq, then there is another gem, called Sidetiq, that will allow you to run recurring jobs.

like image 25
tomdepplito Avatar answered Feb 08 '23 06:02

tomdepplito