Jackson Henley
Jackson Henley

Reputation: 1531

Which way to use Delayed_Job for scheduling?

I have a list auction items in a sql db that end (come due) at a specific time and I'm looking for the proper approach toward using (Delayed::Job.enqueue) to trigger a method when a given auction finishes.

1) Use a worker for every auction which is loaded into memory when the auction starts

2) Use one worker that is loaded when the web app starts which queries the db periodically to see if any auctions have ended.

Are there any better approaches than the 2 listed above?

Upvotes: 1

Views: 204

Answers (1)

Cyberfox
Cyberfox

Reputation: 1145

You could use run_at and separately queue each item to wake up at their end time when it's added to the database. Then you'd only need one worker, which wouldn't get any work to do until something's ended. (Or, more accurately, you would want N workers, where N is the number of simultaneous closings you expect, or want to handle.)

auction.delay(:run_at => auction.ending_at).post_end_work

If you can't queue the auction when it's added to the database (if it's added by something that doesn't have access to your DelayedJob for instance) then you'll want to use a single worker who sweeps the database for ending_at values within the last X amount of time, where X is the run-frequency of your worker. (And an index on ending_at.)

I'd strongly recommend against using separate workers for each auction.

Upvotes: 2

Related Questions