resqutils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a63b6baf7b6534578500e949ccb71c6db6494794
4
+ data.tar.gz: 1a59736bd38007a380c5935e089125cbec529840
5
+ SHA512:
6
+ metadata.gz: 16ca3aff7ae9da98e8a61295742e344fbbc76c139eeb309a13e0fd3a66c47644a7f6a4ec2396c242a10d460a13e6de91efbbe503f464ba82990174f1d8daa941
7
+ data.tar.gz: 627e1e5b9a6f9e60854a41ee261270c625392dfc446b5dfe20ee68510b37a85df47deb1c82a15fb6ac57b2c6da8e6b269a088943e8acb82843e264eb304c07aa
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ pkg
2
+ spec/reports
3
+ .vimrc
4
+ *.sw?
5
+ .idea/
6
+ config/database.yml
7
+ db
8
+ .tddium*
9
+ .DS_Store
10
+ .jhw-cache
11
+ **.orig
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ resqutils
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://www.rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ resqutils (0.0.1)
5
+ resque
6
+
7
+ GEM
8
+ remote: https://www.rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ mono_logger (1.1.0)
12
+ multi_json (1.10.1)
13
+ rack (1.5.2)
14
+ rack-protection (1.5.3)
15
+ rack
16
+ rake (10.3.2)
17
+ redis (3.1.0)
18
+ redis-namespace (1.5.1)
19
+ redis (~> 3.0, >= 3.0.4)
20
+ resque (1.25.2)
21
+ mono_logger (~> 1.0)
22
+ multi_json (~> 1.0)
23
+ redis-namespace (~> 1.3)
24
+ sinatra (>= 0.9.2)
25
+ vegas (~> 0.1.2)
26
+ rspec (3.1.0)
27
+ rspec-core (~> 3.1.0)
28
+ rspec-expectations (~> 3.1.0)
29
+ rspec-mocks (~> 3.1.0)
30
+ rspec-core (3.1.2)
31
+ rspec-support (~> 3.1.0)
32
+ rspec-expectations (3.1.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.1.0)
35
+ rspec-mocks (3.1.0)
36
+ rspec-support (~> 3.1.0)
37
+ rspec-support (3.1.0)
38
+ sinatra (1.4.5)
39
+ rack (~> 1.4)
40
+ rack-protection (~> 1.4)
41
+ tilt (~> 1.3, >= 1.3.4)
42
+ tilt (1.4.1)
43
+ vegas (0.1.11)
44
+ rack (>= 1.0.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ rake
51
+ resqutils!
52
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [2014] [David Bryant Copeland]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # resqutils - useful stuff when you have Resque in your app
2
+
3
+ This is a small library of useful modules and functions that can help dealing with Resque.
4
+
5
+ Currently:
6
+
7
+ * Spec helper `:some_queue.should have_job_queued(class: FooJob)`
8
+ * Methods to introspect queues, including the delayed queue, in your specs
9
+ * Simple `resque:work` task wrapper to better handle exceptions in the worker
10
+ * Marker interface to document jobs which should not be retried
11
+
12
+ Maybe will have more stuff1
13
+
14
+ ## To use
15
+
16
+ Add to your `Gemfile`:
17
+
18
+ ```ruby
19
+ gem 'resqutils'
20
+ ```
21
+
22
+ ## Spec Helpers
23
+
24
+ ```ruby
25
+ # in spec_helper.rb
26
+ require 'resqutils/spec'
27
+
28
+ # In one of your spec files
29
+ describe SomeProcess do
30
+ include Resqutils::Spec::ResqueHelpers
31
+
32
+ # ...
33
+ end
34
+ ```
35
+
36
+ `require`ing the `resqutils/spec` will also set up the `have_job_queued` matcher, which is likely what you'll want to use.
37
+
38
+ ### Checking that Jobs Were Queued
39
+
40
+ ```ruby
41
+ # foo_service.rb
42
+ class FooService
43
+ def doit(foo)
44
+ Resque.enqueue(:foo,FooJob,foo)
45
+ "bar"
46
+ end
47
+ end
48
+
49
+ # foo_service_spec.rb
50
+ describe FooService do
51
+ it "queues a job" do
52
+ result = FooService.new.doit("blah")
53
+
54
+ expect(result).to eq("bar")
55
+ expect(:foo).to have_job_queued(class: FooJob, args: [ "blah" ])
56
+ end
57
+ end
58
+ ```
59
+
60
+ This also works with the delayed queue as provided by resque-scheduler:
61
+
62
+ ```ruby
63
+ # foo_service.rb
64
+ class FooService
65
+ def doit(foo)
66
+ Resque.enqueue_in(5.minutes,:foo,FooJob,foo)
67
+ "bar"
68
+ end
69
+ end
70
+
71
+ # foo_service_spec.rb
72
+
73
+ describe FooService do
74
+ it "queues a job" do
75
+ result = FooService.new.doit("blah")
76
+
77
+ expect(result).to eq("bar")
78
+ # :delayed is special and triggers logic to look into the various scheduled queues
79
+ expect(:delayed).to have_job_queued(class: FooJob, args: [ "blah" ])
80
+ end
81
+ end
82
+ ```
83
+
84
+ ### Executing Jobs
85
+
86
+ In an integration test, you may wish to execute a job that's on the queue, which will both assert that it's there and perform whatever function it performs.
87
+
88
+
89
+ ```ruby
90
+ # foo_service.rb
91
+ class FooService
92
+ def doit(foo)
93
+ Resque.enqueue(:foo,FooJob,foo)
94
+ "bar"
95
+ end
96
+ end
97
+
98
+ class FooJob
99
+ def perform(some_value)
100
+ Foo.create!(value: some_value)
101
+ end
102
+ end
103
+
104
+ # the_foo_service_spec.rb
105
+ describe "the foo service" do
106
+ include Resqutils::Spec::ResqueHelpers
107
+ it "writes a Foo with the value" do
108
+ result = FooService.new.doit("blah")
109
+
110
+ process_resque_job(FooJob)
111
+
112
+ expect(Foo.last.value).to eq("blah")
113
+ end
114
+ end
115
+ ```
116
+
117
+ The `ResqueHelpers` module has many more methods, if you need finer control over your tests with respect to resque.
118
+
119
+ ### Exception Handling in your Worker
120
+
121
+ The built-in worker lets exceptions bubble up.
122
+ In a PaaS setup, or where your Redis is "over the internet", you'll get periodic connection issues from your worker.
123
+ These self-heal when your worker management system (e.g. monit) restarts the worker after it crashes.
124
+ Thus, these unhandled exceptions should just be ignored.
125
+
126
+ Since the built-in resque worker is a rake task, we provide a wrapper rake task to call it and log the exception:
127
+
128
+ ```ruby
129
+ require 'resqutils/worker_task'
130
+ ```
131
+
132
+ To run:
133
+
134
+ ```
135
+ env TERM_CHILD=1 bundle exec rake environment resqutils:work QUEUE=file_uploads --trace
136
+ ```
137
+
138
+ ### Being clear about not retrying
139
+
140
+ Although you should design your jobs to automatically retry, some jobs simply should not be retried.
141
+ Instead of omitting the retry logic or dropping in a comment, you should use a marker interface to communicate intent via code:
142
+
143
+ ```ruby
144
+ class DangerousJob
145
+ include Resqutils::DoNotAutoRetry
146
+
147
+ def perform
148
+ # ...
149
+ end
150
+ end
151
+ ```
152
+
153
+ This is a more powerful statement that a comment, and communicates intent clearly.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+ require 'rubygems/package_task'
7
+ require 'rspec/core/rake_task'
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'ExtraExtra'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.rdoc')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ Bundler::GemHelper.install_tasks
20
+
21
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ module Resqutils
2
+ # Marker module to explicitly indicate that a job should not retried. Useful when your app makes
3
+ # heavy use of retries as a means to make it very clear a certain job should not be retried.
4
+ module DoNotAutoRetry
5
+ end
6
+ end
@@ -0,0 +1,142 @@
1
+ module Resqutils
2
+ module Spec
3
+ end
4
+ end
5
+
6
+ # Mix this into your tests to get helpful methods to manipulate a live resque queue.
7
+ module Resqutils::Spec::ResqueHelpers
8
+ # Process the resque job on the queue that matches the given criteria. Useful to simulate
9
+ # a resque worker processing something inside an integration test. Also handles the delayed queue.
10
+ #
11
+ # expected_class:: Class that should be on the job queue
12
+ # queue:: if present, the queue name (as a String or Symbol). If omitted, expected_class's @queue ivar
13
+ # will be queries to get the queue name. +:delayed+ is a special queue
14
+ # that represents the delayed queue provided by +resque-scheduler+.
15
+
16
+ # block:: if provided, will be given the popped job right before it is processed.
17
+ def process_resque_job(expected_class, queue=nil, &block)
18
+ block ||= ->(payload) {}
19
+ queue ||= expected_class.instance_variable_get("@queue")
20
+ job = if queue == :delayed
21
+ delayed_jobs.select { |job|
22
+ job["class"] == expected_class.name
23
+ }.first
24
+ else
25
+ job = Resque.pop(queue)
26
+ end
27
+ raise "No jobs on #{queue}" if job.nil?
28
+ klass = job["class"].constantize
29
+ klass.should == expected_class
30
+ block.call(job)
31
+ klass.perform(*job["args"])
32
+ end
33
+
34
+ # Process several jobs that have the same class and queue.
35
+ #
36
+ # number:: number of jobs expected
37
+ # expected_class:: Class of the job you want to process
38
+ # queue:: if present, the queue name (as a String or Symbol). If omitted, expected_class's @queue ivar
39
+ # will be queries to get the queue name. +:delayed+ is a special queue
40
+ # that represents the delayed queue provided by +resque-scheduler+.
41
+
42
+ # block:: if provided, will be given each popped job right before it is processed.
43
+ def process_multiple_resque_jobs(number, expected_class, queue=nil, &block)
44
+ number.times do
45
+ process_resque_job(expected_class, queue, &block)
46
+ end
47
+ end
48
+
49
+ # Get the size of the given queue. This mostly prevents your test from having
50
+ # direct coupling to resque's (crappy) internal API.
51
+ #
52
+ # queue:: name of the queue, as a Symbol or String
53
+ def queue_size(queue)
54
+ Resque.size(queue)
55
+ end
56
+
57
+ # Clear the queue. Useful in before blocks to make sure there's nothing hanging around in your queue from
58
+ # a previous test run.
59
+ #
60
+ # queue_name:: If a Class, the ivar @queue is used to determine which queue to clear. Otherwise, assumes
61
+ # it's a string or symbol and will clear that queue. +:delayed+ is a special queue
62
+ # that represents the delayed queue provided by +resque-scheduler+.
63
+ def clear_queue(queue_name)
64
+ if queue_name.kind_of?(Class)
65
+ queue_name = queue_name.instance_variable_get("@queue")
66
+ end
67
+ if queue_name == :delayed
68
+ Resque.reset_delayed_queue
69
+ else
70
+ Resque.redis.del("queue:#{queue_name}")
71
+ Resque.redis.del("resque:queue:#{queue_name}")
72
+ end
73
+ end
74
+
75
+ # Get jobs from +queue_name+ that match the given hash. This returns
76
+ # both the jobs that matched and *all* jobs from the queue, so you can create a useful
77
+ # error message in your tests.
78
+ #
79
+ # queue_name:: A String or Symbol representing the name of the queue, where +:delayed+ is special and will
80
+ # cause the code to look in the delayed queue as provided by resque-scheduler.
81
+ # expected_job_hash:: Criteria for matching, typically a two-element hash containing the class and args
82
+ # of the job you are looking for.
83
+ #
84
+ # Returns an array of size 2, where the first element is an Array of jobs matching the given criteria, and
85
+ # the second element is an Array of all jobs.
86
+ #
87
+ # Example
88
+ #
89
+ # Consider the queue "foo" has these jobs in it:
90
+ #
91
+ # * { "class" => "WarmCaches", "args" => [ 1234 ] }
92
+ # * { "class" => "WarmCaches", "args" => [ 4567 ] }
93
+ # * { "class" => "IndexPurchases", "args" => [ 1234 ] }
94
+ #
95
+ # jobs_matching(:foo, class: WarmCaches, args: [ 1234 ]) # => matching jobs is the first job above
96
+ # jobs_matching(:foo, class: IndexPurchases, args: [ 1234 ]) # => matching jobs is the third job above
97
+ # jobs_matching(:foo, class: IndexPurchases, args: [ 9999 ]) # => matching jobs is nothing
98
+ #
99
+ def jobs_matching(queue_name,expected_job_hash)
100
+ jobs = if queue_name == :delayed
101
+ delayed_jobs
102
+ else
103
+ (0...Resque.size(queue_name)).map { |index|
104
+ [Resque.peek(queue_name,index),index]
105
+ }
106
+ end
107
+ matching_jobs = jobs.select { |(job,index)|
108
+ raise "No job at index #{index}" if job.nil?
109
+ expected_job_hash.all? do |k,v|
110
+ k = k.to_s
111
+ v = v.to_s if v.kind_of?(Class)
112
+ raise "Job #{job} at index #{index} was missing key #{k}" if job[k].nil?
113
+ if k == 'scheduler_timestamp'
114
+ ((v.to_i - 60000)..(v.to_i + 60000)).cover?(job[k])
115
+ else
116
+ job[k] == v
117
+ end
118
+ end
119
+ }.map(&:first)
120
+ [matching_jobs,jobs]
121
+ end
122
+
123
+ # Return all jobs in a delayed queue, with each job augmented with the key +scheduler_timestamp+ to represent
124
+ # what time the job was schedule to run for.
125
+ def delayed_jobs
126
+ # The double-checks here are so that we won't blow up if the config stops using redis-namespace
127
+ timestamps = (Array(Resque.redis.zrange("resque:delayed_queue_schedule",0,-1)) +
128
+ Array(Resque.redis.zrange("delayed_queue_schedule",0,-1)))
129
+ raise "Nothing on delayed schedule" if timestamps.empty?
130
+
131
+ timestamps.map { |timestamp|
132
+ [
133
+ Array(Resque.redis.lrange("resque:delayed:#{timestamp}",0,-1)) + Array(Resque.redis.lrange("delayed:#{timestamp}",0,-1)),
134
+ timestamp,
135
+ ]
136
+ }.map { |(job_strings,timestamp)|
137
+ job_strings.map { |job_string|
138
+ JSON.parse(job_string).merge('scheduler_timestamp' => timestamp.to_i)
139
+ }
140
+ }.flatten
141
+ end
142
+ end
@@ -0,0 +1,27 @@
1
+ # Defines the match +have_job_queued+ which can be asserted on a queue name
2
+ # to check that a particular job is queued.
3
+ #
4
+ # Example
5
+ #
6
+ # :purchasing.should have_job_queued(class: SubscriptionChargeJob
7
+ # args: [ 12345, "99.87" ] )
8
+ RSpec::Matchers.define :have_job_queued do |expected_job_hash|
9
+ include Resqutils::Spec::ResqueHelpers
10
+
11
+ match do |queue_name|
12
+ jobs_matching(queue_name,expected_job_hash).first.size == 1
13
+ end
14
+
15
+ failure_message_for_should do |queue_name|
16
+ matching_jobs,all_jobs = jobs_matching(queue_name,expected_job_hash)
17
+ if matching_jobs.empty?
18
+ "No jobs in #{queue_name} matched #{expected_job_hash.inspect} (Found these jobs: #{all_jobs.map(&:inspect).join(',')})"
19
+ elsif matching_jobs.size > 1
20
+ "Mutiple jobs on #{queue_name} matched - try clearing queues before each test"
21
+ end
22
+ end
23
+
24
+ failure_message_for_should_not do |queue_name|
25
+ "Found job #{expected_job_hash.inspect} in the queue"
26
+ end
27
+ end
@@ -0,0 +1,2 @@
1
+ require 'resqutils/spec/resque_helpers'
2
+ require 'resqutils/spec/resque_matchers'
@@ -0,0 +1,3 @@
1
+ module Resqutils
2
+ VERSION='0.0.1'
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'resque/tasks'
2
+
3
+ # This task wraps the built-in resque:work task to rescue and log
4
+ # errors that happen on connect. The built-in task will allow
5
+ # the exception to bubble up, which will certainly alert your
6
+ # exception handler. This is largely useless for two reasons:
7
+ #
8
+ # * Your worker monitoring tool will simply restart the worker and 99% of the time
9
+ # have no problem reconnecting, thus you don't need this alert
10
+ # * That 1% of the time when there's a legit problem, you have two ways to find out about
11
+ # it: your workering monitoring/management tool and your backed-up queues. Both of
12
+ # those are preferable to an unhandled exception that's usually not actionable.
13
+ #
14
+ # To use this, simply require this file in a place where Rake will pick it up, either in your
15
+ # Rakefile or (for Rails) in a file like +lib/tasks/resque.rake+
16
+ namespace :resqutils do
17
+ desc 'Resque worker task that logs Redis errors instead of raising them'
18
+ task :work do
19
+ begin
20
+ Rake::Task['resque:work'].invoke
21
+ rescue Redis::BaseError => ex
22
+ puts "Unhandled Redis Error from Resque Worker: #{ex.message}"
23
+ end
24
+ end
25
+ end
data/lib/resqutils.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Resqutils
2
+ end
3
+
4
+ require 'resqutils/do_not_auto_retry'
5
+ require 'resqutils/version'
data/resqutils.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'resqutils/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "resqutils"
7
+ s.version = Resqutils::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Stitch Fix Engineering']
10
+ s.email = ['eng@stitchfix.com']
11
+ s.homepage = "http://tech.stitchfix.com"
12
+ s.summary = "Utilities for using Resque in a Rails app"
13
+ s.description = "Utilities for using Resque in a Rails app"
14
+ s.rubyforge_project = "resqutils"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_dependency("resque")
21
+ s.add_development_dependency("rake")
22
+ s.add_development_dependency("rspec")
23
+ end
@@ -0,0 +1,6 @@
1
+ GEM_ROOT = File.expand_path(File.join(File.dirname(__FILE__),'..'))
2
+ Dir["#{GEM_ROOT}/spec/support/**/*.rb"].sort.each {|f| require f}
3
+
4
+ RSpec.configure do |config|
5
+ config.order = "random"
6
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resqutils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stitch Fix Engineering
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Utilities for using Resque in a Rails app
56
+ email:
57
+ - eng@stitchfix.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".ruby-gemset"
64
+ - ".ruby-version"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - lib/resqutils.rb
71
+ - lib/resqutils/do_not_auto_retry.rb
72
+ - lib/resqutils/spec.rb
73
+ - lib/resqutils/spec/resque_helpers.rb
74
+ - lib/resqutils/spec/resque_matchers.rb
75
+ - lib/resqutils/version.rb
76
+ - lib/resqutils/worker_task.rb
77
+ - resqutils.gemspec
78
+ - spec/spec_helper.rb
79
+ homepage: http://tech.stitchfix.com
80
+ licenses: []
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project: resqutils
98
+ rubygems_version: 2.2.0.rc.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Utilities for using Resque in a Rails app
102
+ test_files:
103
+ - spec/spec_helper.rb