Skip to content

Commit

Permalink
Move lock clearing into a callback so it can be skipped when not needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
betamatt committed Jun 13, 2011
1 parent 55ad0e1 commit a1f0838
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/delayed/lifecycle.rb
Expand Up @@ -8,5 +8,14 @@ def self.set_callback name, *args, &block
Delayed::Worker.set_callback name, *args, &block
end
end

def self.skip_callback name, *args, &block
case name
when :enqueue
Delayed::Job.skip_callback name, *args, &block
else
Delayed::Worker.skip_callback name, *args, &block
end
end
end
end
12 changes: 9 additions & 3 deletions lib/delayed/worker.rb
Expand Up @@ -72,7 +72,7 @@ def self.after_fork
end

define_callbacks :execute, :loop, :perform, :error, :failure

def initialize(options={})
@quiet = options.has_key?(:quiet) ? options[:quiet] : true
self.class.min_priority = options[:min_priority] if options.has_key?(:min_priority)
Expand All @@ -96,6 +96,8 @@ def name=(val)
@name = val
end

set_callback :execute, :around, :clear_locks

def start
say "Starting job worker"

Expand Down Expand Up @@ -125,8 +127,6 @@ def start
break if $exit
end
end
ensure
Delayed::Job.clear_locks!(name)
end

# Do num jobs and return stats on success/failure.
Expand Down Expand Up @@ -211,6 +211,12 @@ def reserve_and_run_one_job
run_callbacks(:perform){ result = run(job) } if job
result
end

def clear_locks
yield
ensure
Delayed::Job.clear_locks!(name)
end
end

end

0 comments on commit a1f0838

Please sign in to comment.