Skip to content

Commit 4348544

Browse files
committed
introduce Resque::Plugin to replace the super dance
1 parent 0d1c978 commit 4348544

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

lib/resque.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'resque/stat'
1818
require 'resque/job'
1919
require 'resque/worker'
20+
require 'resque/plugin'
2021

2122
module Resque
2223
include Helpers

lib/resque/job.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@ def perform
108108
job_args = args || []
109109
job_was_performed = false
110110

111+
plugins = payload_class.instance_variable_get(:@plugins) || []
112+
plugins << payload_class
113+
111114
begin
112115
# Execute before_perform hook. Abort the job gracefully if
113116
# Resque::DontPerform is raised.
114117
begin
115-
if payload_class.respond_to?(:before_perform)
116-
payload_class.before_perform(*job_args)
117-
end
118+
plugins.each { |p| p.before_perform(*job_args) if p.respond_to?(:before_perform) }
118119
rescue DontPerform
119120
return false
120121
end

lib/resque/plugin.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Resque
2+
module Plugin
3+
4+
# `extend_object` is like `extended` except that we now override
5+
# everything that happens when this module is extended.
6+
def extend_object(obj)
7+
var = :@plugins
8+
obj.instance_variable_set(var, []) unless obj.instance_variable_defined?(var)
9+
10+
k = Class.new
11+
k.send(:include, self)
12+
obj.instance_variable_get(var) << k.new
13+
end
14+
end
15+
end

test/job_hooks_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,15 @@ def self.on_failure(exception, history)
310310
include PerformJob
311311

312312
module BeforeOne
313+
extend Resque::Plugin
313314
def before_perform(history)
314-
super rescue NoMethodError
315315
history << :before_one
316316
end
317317
end
318318

319319
module BeforeTwo
320+
extend Resque::Plugin
320321
def before_perform(history)
321-
super rescue NoMethodError
322322
history << :before_two
323323
end
324324
end

0 commit comments

Comments
 (0)