Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #37 from okbreathe/dm-compat
Browse files Browse the repository at this point in the history
Add DM compatibility
  • Loading branch information
Michael van Rooijen committed Oct 16, 2011
2 parents 3f6ba60 + 1628539 commit 0fab407
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/hirefire/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def self.included(base)
end
end

if defined?(::Delayed::Backend::DataMapper::Job)
base.send(:include, HireFire::Backend::DelayedJob::DataMapper)
end

if defined?(::Delayed::Backend::Mongoid::Job)
base.send(:include, HireFire::Backend::DelayedJob::Mongoid)
end
Expand Down
1 change: 1 addition & 0 deletions lib/hirefire/backend/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module DelayedJob
autoload :ActiveRecord, 'hirefire/backend/delayed_job/active_record'
autoload :ActiveRecord2, 'hirefire/backend/delayed_job/active_record_2'
autoload :Mongoid, 'hirefire/backend/delayed_job/mongoid'
autoload :DataMapper, 'hirefire/backend/delayed_job/data_mapper'
end
end
end
30 changes: 30 additions & 0 deletions lib/hirefire/backend/delayed_job/data_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: utf-8

module HireFire
module Backend
module DelayedJob
module DataMapper

##
# Counts the amount of queued jobs in the database,
# failed jobs are excluded from the sum
#
# @return [Fixnum] the amount of pending jobs
def jobs
::Delayed::Job.count(:failed_at => nil, :run_at.lte => Time.now.utc)
end

##
# Counts the amount of jobs that are locked by a worker
# There is no other performant way to determine the amount
# of workers there currently are
#
# @return [Fixnum] the amount of (assumably working) workers
def working
::Delayed::Job.count(:locked_by.not => nil)
end

end
end
end
end
14 changes: 14 additions & 0 deletions lib/hirefire/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ def self.included(base)
after_update 'self.class.environment.fire',
:unless => Proc.new { |job| job.failed_at.nil? }
end
elsif base.name == "Delayed::Backend::DataMapper::Job"
base.send :extend, HireFire::Environment::DelayedJob::ClassMethods

base.class_eval do
after :create do
self.class.hirefire_hire
end
after :destroy do
self.class.environment.fire
end
after :update do
self.class.environment.fire unless self.failed_at.nil?
end
end
end

Logger.message("#{ base.name } detected!")
Expand Down
9 changes: 9 additions & 0 deletions lib/hirefire/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def self.initialize!
send(:include, HireFire::Backend)
end

##
# If DelayedJob is using DataMapper, then include
# HireFire::Environment in to the DataMapper Delayed Job Backend
if defined?(::Delayed::Backend::DataMapper::Job)
::Delayed::Backend::DataMapper::Job.
send(:include, HireFire::Environment).
send(:include, HireFire::Backend)
end

##
# Load Delayed Job extensions, this will patch Delayed::Worker
# to implement the necessary hooks to invoke HireFire from
Expand Down

0 comments on commit 0fab407

Please sign in to comment.