tomykaira/clockwork — pull request #88 by robisenberg (2014-03-10 14:17:16+00:00)
Title: Implement syncing of database tasks

# Motivation

See https://github.com/tomykaira/clockwork/issues/25. Several people seem to want this functionality, and I needed it for a project I'm working on, so I went ahead and implemented. I took your advice and subclassed the Manager.

# Summary

Syntax in clock.rb is:

sync_database_tasks model: Task, every: 1.minute do |instance_job_name|
# do work
end

This creates a "sync_database_tasks_for_model_#{model}" that will run
with the "every:" frequency specified - in this case 1.minute. This task
deletes all previous database tasks stored (relating to this particular
sync_database_task call) and recreates them from scratch to avoid managing
which ones have been modifed/updated/removed.

Note that, to avoid the same database task incorrectly being run twice,
we prevent the database tasks being executed in the same cycle that the
database sync occurs.

You can have multiple sync_database_tasks calls in the same clock.rb
file as the events are scoped to the particular call that set up that
sync.

# How to Integrate

Let me know whether you're happy to integrate this work into the project, and if so, your thoughts on how to correctly integrate it.

At the moment, in lib/clockwork.rb I've just changed #manager to create a ManagerWithDatabaseTasks instead of the normal Manager class. I'm not sure whether you want people always use this new manager (which inherits from the normal one), or you want them to somehow configure this if they need the support for database synced tasks. I've (hopefully) made ManagerWithDatabaseTasks completely backwards-compatible, it just adds the extra functionality needed.

I decided to name the new manager ManagerWithDatabaseTasks rather than DBBackedManager like you suggested because I wanted to suggest that not *all* tasks are database tasks - it provides the feature *in addition* to what was already provided before. Happy to take your guidance on this though. :)