Created
May 4, 2010 12:42
-
-
Save faust45/389365 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Job < CouchRestBase | |
| include Delayed::Backend::Base | |
| use_database :delayed_job | |
| property :priority | |
| property :attempts | |
| property :handler | |
| property :run_at | |
| property :locked_at | |
| property :locked_by | |
| property :failed_at | |
| property :last_error | |
| timestamps! | |
| view_by :locked_by | |
| view_by(:locked_at, :run_at, | |
| :map => "function(doc){" + | |
| " if(doc['couchrest-type'] == 'Job' && doc.run_at) {" + | |
| " var locked_at = doc.locked_at || '';" + | |
| " emit([locked_at, doc.run_at], null);}" + | |
| " }") | |
| set_callback :save, :before, :set_default_run_at | |
| def self.db_time_now; Time.now; end | |
| def self.find id; get id; end | |
| def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time) | |
| by_locked_at_and_run_at :start_key => [''], :end_key => ['', db_time_now], :limit => limit | |
| end | |
| def self.clear_locks!(worker_name) | |
| docs = by_locked_by :startkey => [worker_name], :endkey => [worker_name, {}] | |
| docs.each { |doc| doc.locked_by, doc.locked_at = nil, nil; } | |
| database.bulk_save docs | |
| end | |
| def self.delete_all | |
| database.recreate! | |
| end | |
| def lock_exclusively!(max_run_time, worker = worker_name) | |
| locked_at, locked_by = self.class.db_time_now, worker | |
| save | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment