resque_unit 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -51,6 +51,11 @@ unless defined?(Resque::Plugin)
51
51
  def after_enqueue_hooks(job)
52
52
  job.methods.grep(/^after_enqueue/).sort
53
53
  end
54
+
55
+ # Given an object, returns a list `before_enqueue` hook names.
56
+ def before_enqueue_hooks(job)
57
+ job.methods.grep(/^before_enqueue/).sort
58
+ end
54
59
  end
55
60
  end
56
61
 
@@ -62,4 +67,4 @@ unless defined?(Resque::Job::DontPerform)
62
67
  DontPerform = Class.new(StandardError)
63
68
  end
64
69
  end
65
- end
70
+ end
@@ -71,28 +71,25 @@ module Resque
71
71
 
72
72
  # Executes all jobs in all queues in an undefined order.
73
73
  def run!
74
- old_queue = @queue.dup
75
- self.reset!
76
-
77
-
78
- old_queue.each do |queue_name, queue|
79
- queue.each do |job_payload|
80
- job_payload = decode(job_payload)
81
- @hooks_enabled ? perform_with_hooks(job_payload) : perform_without_hooks(job_payload)
82
- end
74
+ payloads = []
75
+ @queue.each do |queue_name, queue|
76
+ payloads.concat queue.slice!(0, queue.size)
83
77
  end
78
+ exec_payloads payloads.shuffle
84
79
  end
85
80
 
86
- # Executes all jobs in the given queue in an undefined order.
87
- def run_for!(queue_name)
88
- jobs_payloads = all(queue_name)
89
-
90
- self.reset!(queue_name)
81
+ def run_for!(queue_name, limit=false)
82
+ queue = @queue[queue_name]
83
+ exec_payloads queue.slice!(0, ( limit ? limit : queue.size) ).shuffle
84
+ end
91
85
 
92
- jobs_payloads.each do |job_payload|
86
+ def exec_payloads(raw_payloads)
87
+ raw_payloads.each do |raw_payload|
88
+ job_payload = decode(raw_payload)
93
89
  @hooks_enabled ? perform_with_hooks(job_payload) : perform_without_hooks(job_payload)
94
90
  end
95
91
  end
92
+ private :exec_payloads
96
93
 
97
94
  # 1. Execute all jobs in all queues in an undefined order,
98
95
  # 2. Check if new jobs were announced, and execute them.
@@ -112,7 +109,11 @@ module Resque
112
109
 
113
110
  # :nodoc:
114
111
  def enqueue(klass, *args)
115
- queue_name = queue_for(klass)
112
+ enqueue_to( queue_for(klass), klass, *args)
113
+ end
114
+
115
+ # :nodoc:
116
+ def enqueue_to( queue_name, klass, *args )
116
117
  # Behaves like Resque, raise if no queue was specifed
117
118
  raise NoQueueError.new("Jobs must be placed onto a queue.") unless queue_name
118
119
  enqueue_unit(queue_name, {"class" => klass.name, "args" => args })
@@ -122,6 +123,7 @@ module Resque
122
123
  def queue_for(klass)
123
124
  klass.instance_variable_get(:@queue) || (klass.respond_to?(:queue) && klass.queue)
124
125
  end
126
+ alias :queue_from_class :queue_for
125
127
 
126
128
  # :nodoc:
127
129
  def empty_queues?
@@ -132,6 +134,12 @@ module Resque
132
134
 
133
135
  def enqueue_unit(queue_name, hash)
134
136
  klass = constantize(hash["class"])
137
+ if @hooks_enabled
138
+ before_hooks = Plugin.before_enqueue_hooks(klass).map do |hook|
139
+ klass.send(hook, *hash["args"])
140
+ end
141
+ return nil if before_hooks.any? { |result| result == false }
142
+ end
135
143
  queue(queue_name) << encode(hash)
136
144
  if @hooks_enabled
137
145
  Plugin.after_enqueue_hooks(klass).each do |hook|
@@ -17,6 +17,40 @@ class ResqueUnitTest < Test::Unit::TestCase
17
17
  end
18
18
  end
19
19
 
20
+ context "A task that explicitly is queued to a different queue" do
21
+ setup { Resque.enqueue_to(:a_non_class_determined_queue, MediumPriorityJob) }
22
+ should "not queue to the class-determined queue" do
23
+ assert_equal 0, Resque.queue(MediumPriorityJob.queue).length
24
+ end
25
+ should "queue to the explicly-stated queue" do
26
+ assert_equal 1, Resque.queue(:a_non_class_determined_queue).length
27
+ end
28
+ end
29
+
30
+ context "A task that spawns multiple jobs on a single queue" do
31
+ setup do
32
+ 3.times {Resque.enqueue(HighPriorityJob)}
33
+ end
34
+
35
+ should "allow partial runs with explicit limit" do
36
+ assert_equal 3, Resque.queue(:high).length, 'failed setup'
37
+ Resque.run_for!( :high, 1 )
38
+ assert_equal 2, Resque.queue(:high).length, 'failed to run just single job'
39
+ end
40
+
41
+ should "allow full run with too-large explicit limit" do
42
+ assert_equal 3, Resque.queue(:high).length, 'failed setup'
43
+ Resque.run_for!( :high, 50 )
44
+ assert_equal 0, Resque.queue(:high).length, 'failed to run all jobs'
45
+ end
46
+
47
+ should "allow full run with implicit limit" do
48
+ assert_equal 3, Resque.queue(:high).length, 'failed setup'
49
+ Resque.run_for!( :high )
50
+ assert_equal 0, Resque.queue(:high).length, 'failed to run all jobs'
51
+ end
52
+ end
53
+
20
54
  context "A task that schedules a resque job" do
21
55
  setup do
22
56
  @returned = Resque.enqueue(LowPriorityJob)
@@ -82,6 +116,11 @@ class ResqueUnitTest < Test::Unit::TestCase
82
116
  assert(JobWithHooks.markers[:after_enqueue], 'no after_queue marker set')
83
117
  end
84
118
 
119
+ should "have run the before_enqueue hook" do
120
+ assert(JobWithHooks.markers[:before_enqueue], 'no before_queue marker set')
121
+ assert_queued(JobWithHooks)
122
+ end
123
+
85
124
  should "run the before and after hooks during a run" do
86
125
  Resque.run!
87
126
  assert(JobWithHooks.markers[:before], 'no before marker set')
@@ -125,6 +164,18 @@ class ResqueUnitTest < Test::Unit::TestCase
125
164
  end
126
165
  end
127
166
 
167
+ context "when before_enqueue returns false" do
168
+ setup do
169
+ JobWithHooksBeforeBlocks.clear_markers
170
+ end
171
+
172
+ should "not queue" do
173
+ Resque.enqueue JobWithHooksBeforeBlocks
174
+ assert_not_queued JobWithHooksBeforeBlocks
175
+ end
176
+
177
+ end
178
+
128
179
  context "but without around" do
129
180
  setup do
130
181
  JobWithHooksWithoutAround.clear_markers
@@ -53,6 +53,10 @@ module HooksMethods
53
53
  def after_enqueue_mark(*args)
54
54
  markers[:after_enqueue] = true
55
55
  end
56
+
57
+ def before_enqueue_mark(*args)
58
+ markers[:before_enqueue] = true
59
+ end
56
60
 
57
61
  def after_perform_mark(*args)
58
62
  markers[:after] = true
@@ -108,6 +112,13 @@ class JobWithHooks
108
112
  end
109
113
  end
110
114
 
115
+ class JobWithHooksBeforeBlocks < JobWithHooks
116
+ @queue = :with_hooks
117
+ def self.before_enqueue_block_enqueueing
118
+ return false
119
+ end
120
+ end
121
+
111
122
  class JobWithHooksWithoutBefore
112
123
  extend HooksMethods
113
124
 
metadata CHANGED
@@ -1,60 +1,56 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: resque_unit
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
4
5
  prerelease:
5
- version: 0.4.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Justin Weiss
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-06-28 00:00:00 -07:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: json
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70242270906860 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
23
21
  version: 1.4.6
24
22
  type: :runtime
25
23
  prerelease: false
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
24
+ version_requirements: *70242270906860
25
+ - !ruby/object:Gem::Dependency
28
26
  name: bundler
29
- requirement: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &70242270906100 !ruby/object:Gem::Requirement
30
28
  none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
35
33
  type: :development
36
34
  prerelease: false
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
35
+ version_requirements: *70242270906100
36
+ - !ruby/object:Gem::Dependency
39
37
  name: shoulda
40
- requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirement: &70242270904960 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
46
44
  type: :development
47
45
  prerelease: false
48
- version_requirements: *id003
46
+ version_requirements: *70242270904960
49
47
  description:
50
48
  email: justin@uberweiss.org
51
49
  executables: []
52
-
53
50
  extensions: []
54
-
55
- extra_rdoc_files:
51
+ extra_rdoc_files:
56
52
  - README.md
57
- files:
53
+ files:
58
54
  - lib/resque_unit/assertions.rb
59
55
  - lib/resque_unit/errors.rb
60
56
  - lib/resque_unit/helpers.rb
@@ -70,41 +66,37 @@ files:
70
66
  - test/sample_jobs.rb
71
67
  - test/test_helper.rb
72
68
  - README.md
73
- has_rdoc: true
74
69
  homepage: http://github.com/justinweiss/resque_unit
75
70
  licenses: []
76
-
77
71
  post_install_message:
78
72
  rdoc_options: []
79
-
80
- require_paths:
73
+ require_paths:
81
74
  - lib
82
- required_ruby_version: !ruby/object:Gem::Requirement
75
+ required_ruby_version: !ruby/object:Gem::Requirement
83
76
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- hash: -1898540308835741897
88
- segments:
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ segments:
89
82
  - 0
90
- version: "0"
91
- required_rubygems_version: !ruby/object:Gem::Requirement
83
+ hash: -2544515951238107368
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
85
  none: false
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- hash: -1898540308835741897
97
- segments:
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ segments:
98
91
  - 0
99
- version: "0"
92
+ hash: -2544515951238107368
100
93
  requirements: []
101
-
102
94
  rubyforge_project:
103
- rubygems_version: 1.5.0
95
+ rubygems_version: 1.8.3
104
96
  signing_key:
105
97
  specification_version: 3
106
98
  summary: Test::Unit support for resque job queueing
107
- test_files:
99
+ test_files:
108
100
  - test/resque_test.rb
109
101
  - test/resque_unit_scheduler_test.rb
110
102
  - test/resque_unit_test.rb