Scheduling a Recurring Job

Jobs are components that execute on a schedule instead of in response to user action. In the case of Twitalytics, the schedule is recurring, but other jobs could be a one-time event. With TorqueBox, jobs like this run asynchronously in the background, but they still execute within the same JVM process as the rest of the application.

Twitalytics has a recurring job that removes old status updates from the database. It’s located in the lib/jobs/delete_old_statuses.rb file, and it looks like this:

 class​ DeleteOldStatuses
 def​ run
  ids = Status.where(​"created_at < ?"​, 30.days.ago)
 
 if​ ids.size > 0
  Status.destroy(ids)
  puts ​"​​#{​ids.size​}​​ statuses have ...

Get Deploying with JRuby 9k now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.