2
2
271822015-07-30 23:18:45
Ruby on Rails
27182, 2015-07-30 23:18:45

How to execute Ruby method to check DB data every second?

Users save information to the database and choose an expiration date in seconds (maybe 10 seconds, maybe 1800). After this time from the moment of creation, it is necessary to run a certain trigger. Cron tasks can be run at least once per minute.
Solution used

loop do
 Foo.check
 sleep 1
end

1. What are the best ways to do this?
2. How to optimize such a mechanism, given that there is a lot of data, and the check can take longer than 1 second?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dima, 2015-07-31
@MAXOPKA

You can use the Sidekiq gem - an analogue of a cron for Rails.
When the user adds an entry - add the task to Sidekiq, with a delay that the user has specified.
Read more here - Sidekiq

V
Viktor Vsk, 2015-07-31
@viktorvsk

Task queue (delayed job, sidekiq, resque) + scheduler ( https://github.com/tomykaira/clockwork )

M
maximw, 2015-07-30
@maximw

Run in cron, process multiple entries in each run. The main thing is not to forget about locks, so that it works well if suddenly two processes are running at once - both in terms of speed and in terms of data integrity. I usually use DB level locks (MySQL GET_LOCK)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question