0

I am working on a Rails application, where is a lot of articles. Every articles can be published whenever - the author of an article can select an hour when he want to publish the article and also the minute when (00, 10, 20, 30, 40, 50).

What makes me worried here - how to set up the CRON here? I publish the articles with the Delayed jobs gem. I know that I can set up the CRON to be running every 10 minutes, but let's say that there is 10 articles that should be published at 11:20 PM - can - or will the the CRON process all these 10 articles and will there articles be published by the Delayed Jobs?

Because there is just 10 minutes on processing these tasks (as the CRON will be run at every 00, 10, 20, 30, 40 and 50 minute) - is that enough time?

user984621
  • 46,344
  • 73
  • 224
  • 412

1 Answers1

0

use the run_at option of Delayed::Job, no need for cron

enqueue the job when the article is updated/created with its publish_at datetime specified

job = PublishArticleJob.new(article.id)
Delayed::Job.enqueue(job, run_at: article.publish_at)

Beware of how you are handling timezones, if I publish an article as a 'Pacific' time user I probably expect my article to publish at that time in 'Pacific' ?

If the users can update the publish_at time before it runs, you would need to delete the existing jobs or update the run_at, see How to cancel scheduled job with delayed_job in Rails? for more info on how to do that

above assumes your job looks something like

class PublishArticleJob < Struct.new(article_id)
  def perform
    article = Article.find(article_id)
    # code to do the publishing
  end
end
Community
  • 1
  • 1
house9
  • 20,359
  • 8
  • 55
  • 61