July 2016
Intermediate to advanced
150 pages
4h 24m
English
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 ... |
Read now
Unlock full access