Permalink
Browse files

Extract Mongoid backend into its own gem. See http://github.com/colle…

  • Loading branch information...
1 parent df9cf7e commit b0251840c885ee15fcbb09c576dd91fc744e318a @bkeepers bkeepers committed Sep 5, 2010
View
@@ -51,14 +51,6 @@ $ script/generate delayed_job
$ rake db:migrate
</pre>
-h3. DataMapper
-
-<pre>
-# config/initializers/delayed_job.rb
-Delayed::Worker.backend = :data_mapper
-Delayed::Worker.backend.auto_upgrade!
-</pre>
-
h2. Queuing Jobs
Call @.delay.method(params)@ on any object and it will be processed in the background.
View
@@ -21,14 +21,10 @@ Jeweler::Tasks.new do |s|
s.test_files = Dir['spec/*_spec.rb']
s.add_dependency "daemons"
+ s.add_dependency "activesupport"
s.add_development_dependency "rspec"
s.add_development_dependency "sqlite3-ruby"
s.add_development_dependency "activerecord"
- s.add_development_dependency "dm-core"
- s.add_development_dependency "dm-observer"
- s.add_development_dependency "dm-aggregates"
- s.add_development_dependency "dm-validations"
- s.add_development_dependency "do_sqlite3"
s.add_development_dependency "couchrest"
end
View
@@ -34,7 +34,6 @@ This gem is collectiveidea's fork (http://github.com/collectiveidea/delayed_job)
"lib/delayed/backend/active_record.rb",
"lib/delayed/backend/base.rb",
"lib/delayed/backend/couch_rest.rb",
- "lib/delayed/backend/data_mapper.rb",
"lib/delayed/backend/shared_spec.rb",
"lib/delayed/command.rb",
"lib/delayed/message_sending.rb",
@@ -54,13 +53,11 @@ This gem is collectiveidea's fork (http://github.com/collectiveidea/delayed_job)
"spec/autoloaded/struct.rb",
"spec/backend/active_record_job_spec.rb",
"spec/backend/couch_rest_job_spec.rb",
- "spec/backend/data_mapper_job_spec.rb",
"spec/message_sending_spec.rb",
"spec/performable_method_spec.rb",
"spec/sample_jobs.rb",
"spec/setup/active_record.rb",
"spec/setup/couch_rest.rb",
- "spec/setup/data_mapper.rb",
"spec/spec_helper.rb",
"spec/worker_spec.rb",
"spec/yaml_ext_spec.rb",
@@ -84,37 +81,25 @@ This gem is collectiveidea's fork (http://github.com/collectiveidea/delayed_job)
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<daemons>, [">= 0"])
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
s.add_development_dependency(%q<rspec>, [">= 0"])
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
s.add_development_dependency(%q<activerecord>, [">= 0"])
- s.add_development_dependency(%q<dm-core>, [">= 0"])
- s.add_development_dependency(%q<dm-observer>, [">= 0"])
- s.add_development_dependency(%q<dm-aggregates>, [">= 0"])
- s.add_development_dependency(%q<dm-validations>, [">= 0"])
- s.add_development_dependency(%q<do_sqlite3>, [">= 0"])
s.add_development_dependency(%q<couchrest>, [">= 0"])
else
s.add_dependency(%q<daemons>, [">= 0"])
+ s.add_dependency(%q<activesupport>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 0"])
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
s.add_dependency(%q<activerecord>, [">= 0"])
- s.add_dependency(%q<dm-core>, [">= 0"])
- s.add_dependency(%q<dm-observer>, [">= 0"])
- s.add_dependency(%q<dm-aggregates>, [">= 0"])
- s.add_dependency(%q<dm-validations>, [">= 0"])
- s.add_dependency(%q<do_sqlite3>, [">= 0"])
s.add_dependency(%q<couchrest>, [">= 0"])
end
else
s.add_dependency(%q<daemons>, [">= 0"])
+ s.add_dependency(%q<activesupport>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 0"])
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
s.add_dependency(%q<activerecord>, [">= 0"])
- s.add_dependency(%q<dm-core>, [">= 0"])
- s.add_dependency(%q<dm-observer>, [">= 0"])
- s.add_dependency(%q<dm-aggregates>, [">= 0"])
- s.add_dependency(%q<dm-validations>, [">= 0"])
- s.add_dependency(%q<do_sqlite3>, [">= 0"])
s.add_dependency(%q<couchrest>, [">= 0"])
end
end
@@ -1,121 +0,0 @@
-require 'dm-core'
-require 'dm-observer'
-require 'dm-aggregates'
-
-DataMapper::Resource.class_eval do
- yaml_as "tag:ruby.yaml.org,2002:DataMapper"
-
- def self.yaml_new(klass, tag, val)
- klass.find(val['id'])
- end
-
- def to_yaml_properties
- ['@id']
- end
-end
-
-module Delayed
- module Backend
- module DataMapper
- class Job
- include ::DataMapper::Resource
- include Delayed::Backend::Base
-
- storage_names[:default] = 'delayed_jobs'
-
- property :id, Serial
- property :priority, Integer, :default => 0, :index => :run_at_priority
- property :attempts, Integer, :default => 0
- property :handler, Text, :lazy => false
- property :run_at, Time, :index => :run_at_priority
- property :locked_at, Time, :index => true
- property :locked_by, String
- property :failed_at, Time
- property :last_error, Text
-
- def self.db_time_now
- Time.now
- end
-
- def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
-
- simple_conditions = { :run_at.lte => db_time_now, :limit => limit, :failed_at => nil, :order => [:priority.asc, :run_at.asc] }
-
- # respect priorities
- simple_conditions[:priority.gte] = Worker.min_priority if Worker.min_priority
- simple_conditions[:priority.lte] = Worker.max_priority if Worker.max_priority
-
- # lockable
- lockable = (
- # not locked or past the max time
- ( all(:locked_at => nil ) | all(:locked_at.lt => db_time_now - max_run_time)) |
-
- # OR locked by our worker
- all(:locked_by => worker_name))
-
- # plus some other boring junk
- (lockable).all( simple_conditions )
- end
-
- # When a worker is exiting, make sure we don't have any locked jobs.
- def self.clear_locks!(worker_name)
- all(:locked_by => worker_name).update(:locked_at => nil, :locked_by => nil)
- end
-
- # Lock this job for this worker.
- # Returns true if we have the lock, false otherwise.
- def lock_exclusively!(max_run_time, worker = worker_name)
-
- now = self.class.db_time_now
- overtime = now - max_run_time
-
- # FIXME - this is a bit gross
- # DM doesn't give us the number of rows affected by a collection update
- # so we have to circumvent some niceness in DM::Collection here
- collection = locked_by != worker ?
- (self.class.all(:id => id, :run_at.lte => now) & ( self.class.all(:locked_at => nil) | self.class.all(:locked_at.lt => overtime) ) ) :
- self.class.all(:id => id, :locked_by => worker)
-
- attributes = collection.model.new(:locked_at => now, :locked_by => worker).dirty_attributes
- affected_rows = self.repository.update(attributes, collection)
-
- if affected_rows == 1
- self.locked_at = now
- self.locked_by = worker
- return true
- else
- return false
- end
- end
-
- # these are common to the other backends, so we provide an implementation
- def self.delete_all
- Delayed::Job.auto_migrate!
- end
-
- def self.find id
- get id
- end
-
- def update_attributes(attributes)
- attributes.each do |k,v|
- self[k] = v
- end
- self.save
- end
-
-
- end
-
- class JobObserver
- include ::DataMapper::Observer
-
- observe Job
-
- before :save do
- self.run_at ||= self.class.db_time_now
- end
- end
- end
- end
-end
@@ -189,13 +189,13 @@ def create_job(opts = {})
end
it "should be able to get access to the task if it was started more then max_age ago" do
- @job.locked_at = 5.hours.ago
+ @job.locked_at = @backend.db_time_now - 5.hours
@job.save
@job.lock_exclusively! 4.hours, 'worker2'
@job.reload
@job.locked_by.should == 'worker2'
- @job.locked_at.should > 1.minute.ago
+ @job.locked_at.should > (@backend.db_time_now - 1.minute)
end
it "should not be found by another worker" do
@@ -226,7 +226,7 @@ def create_job(opts = {})
end
it "should not allow a second worker to get exclusive access if failed to be processed by worker1 and run_at time is now in future (due to backing off behaviour)" do
- @job.update_attributes(:attempts => 1, :run_at => 1.day.from_now)
+ @job.update_attributes(:attempts => 1, :run_at => @backend.db_time_now + 1.day)
@job_copy_for_worker_2.lock_exclusively!(4.hours, 'worker2').should == false
end
end
@@ -1,4 +1,5 @@
require 'active_support/basic_object'
+require 'active_support/core_ext/module/aliasing'
module Delayed
class DelayProxy < ActiveSupport::BasicObject
@@ -1,16 +0,0 @@
-require 'spec_helper'
-require 'backend/shared_backend_spec'
-require 'delayed/backend/data_mapper'
-
-describe Delayed::Backend::DataMapper::Job do
- before(:all) do
- @backend = Delayed::Backend::DataMapper::Job
- end
-
- before(:each) do
- # reset database before each example is run
- DataMapper.auto_migrate!
- end
-
- it_should_behave_like 'a backend'
-end
@@ -1,8 +0,0 @@
-require 'dm-core'
-require 'dm-validations'
-
-require 'delayed/backend/data_mapper'
-
-DataMapper.logger = Delayed::Worker.logger
-DataMapper.setup(:default, "sqlite3::memory:")
-DataMapper.auto_migrate!

0 comments on commit b025184

Please sign in to comment.