resque-status-future 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +67 -0
- data/LICENSE +201 -0
- data/README.md +114 -0
- data/Rakefile +13 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/resque/plugins/status/future.rb +127 -0
- data/lib/resque-status-future.rb +1 -0
- data/resque-status-future.gemspec +25 -0
- metadata +139 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
resque-status-future (0.4.0)
|
5
|
+
resque-status (~> 0.5)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.1.0)
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
method_source (0.8.2)
|
13
|
+
mono_logger (1.1.0)
|
14
|
+
multi_json (1.11.2)
|
15
|
+
pry (0.10.3)
|
16
|
+
coderay (~> 1.1.0)
|
17
|
+
method_source (~> 0.8.1)
|
18
|
+
slop (~> 3.4)
|
19
|
+
rack (1.6.4)
|
20
|
+
rack-protection (1.5.3)
|
21
|
+
rack
|
22
|
+
rake (10.4.2)
|
23
|
+
redis (3.2.2)
|
24
|
+
redis-namespace (1.5.2)
|
25
|
+
redis (~> 3.0, >= 3.0.4)
|
26
|
+
resque (1.25.2)
|
27
|
+
mono_logger (~> 1.0)
|
28
|
+
multi_json (~> 1.0)
|
29
|
+
redis-namespace (~> 1.3)
|
30
|
+
sinatra (>= 0.9.2)
|
31
|
+
vegas (~> 0.1.2)
|
32
|
+
resque-status (0.5.0)
|
33
|
+
resque (~> 1.19)
|
34
|
+
rspec (3.4.0)
|
35
|
+
rspec-core (~> 3.4.0)
|
36
|
+
rspec-expectations (~> 3.4.0)
|
37
|
+
rspec-mocks (~> 3.4.0)
|
38
|
+
rspec-core (3.4.1)
|
39
|
+
rspec-support (~> 3.4.0)
|
40
|
+
rspec-expectations (3.4.0)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.4.0)
|
43
|
+
rspec-mocks (3.4.0)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.4.0)
|
46
|
+
rspec-support (3.4.1)
|
47
|
+
sinatra (1.4.6)
|
48
|
+
rack (~> 1.4)
|
49
|
+
rack-protection (~> 1.4)
|
50
|
+
tilt (>= 1.3, < 3)
|
51
|
+
slop (3.6.0)
|
52
|
+
tilt (2.0.2)
|
53
|
+
vegas (0.1.11)
|
54
|
+
rack (>= 1.0.0)
|
55
|
+
|
56
|
+
PLATFORMS
|
57
|
+
ruby
|
58
|
+
|
59
|
+
DEPENDENCIES
|
60
|
+
bundler (~> 1.10)
|
61
|
+
pry
|
62
|
+
rake (~> 10.0)
|
63
|
+
resque-status-future!
|
64
|
+
rspec
|
65
|
+
|
66
|
+
BUNDLED WITH
|
67
|
+
1.11.2
|
data/LICENSE
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 2016 WHG (International) Limited
|
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,114 @@
|
|
1
|
+
# resque-status-future
|
2
|
+
|
3
|
+
This gem adds [futures](https://en.wikipedia.org/wiki/Futures_and_promises) to
|
4
|
+
the excellent [resque-status](https://github.com/quirkey/resque-status) gem.
|
5
|
+
|
6
|
+
## Why?
|
7
|
+
|
8
|
+
resque-status is great for querying the status of Resque jobs in progress, but
|
9
|
+
very often you want to wait for them to complete and then use their return
|
10
|
+
values for something.
|
11
|
+
|
12
|
+
For example, maybe you have a service that builds Docker containers on demand
|
13
|
+
and then starts them up. For this you'd want to wait for the containers to be
|
14
|
+
built before starting them, and maybe you need some data from the first job
|
15
|
+
before you know exactly how to schedule the second job.
|
16
|
+
|
17
|
+
Currently, once you've built your workflow up, you need to call the `wait`
|
18
|
+
method to process all the the jobs and get the final result.
|
19
|
+
|
20
|
+
## Chaining example
|
21
|
+
|
22
|
+
Imagine we have two resque-status jobs: one that creates Docker containers
|
23
|
+
and one that starts them.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'resque-status'
|
27
|
+
|
28
|
+
class CreateContainer
|
29
|
+
include Resque::Plugins::Status
|
30
|
+
@queue = :create
|
31
|
+
def perform
|
32
|
+
container = Docker.create_container(image: options['image_name'])
|
33
|
+
completed('container_name' => container.name)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class StartContainer
|
38
|
+
include Resque::Plugins::Status
|
39
|
+
@queue = :start
|
40
|
+
def perform
|
41
|
+
status = Docker.start_container(name: options['container_name'])
|
42
|
+
completed('status' => status)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
You can't call `StartContainer` until `CreateContainer` is completed.
|
48
|
+
Moreover, you need the container name generated during `CreateContainer` in
|
49
|
+
order to enqueue the `StartContainer` job.
|
50
|
+
|
51
|
+
This can be done with a future. A method for creating futures is added to
|
52
|
+
every resque-status class.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
require 'resque-status-future'
|
56
|
+
|
57
|
+
future = CreateContainer.future('image_name' => 'redis').then do |st|
|
58
|
+
StartContainer.future('container_name' => st['container_name'])
|
59
|
+
end
|
60
|
+
|
61
|
+
status = future.wait(timeout: 10)
|
62
|
+
```
|
63
|
+
|
64
|
+
The callback given to `#then` can be any Ruby code and it receives the
|
65
|
+
`Resque::Plugins::Status::Hash` from the completed job before it is executed.
|
66
|
+
|
67
|
+
If the callback itself returns a future, the job will be chained and the
|
68
|
+
call to `#wait` will wait for every sequential job to complete, with the return
|
69
|
+
status of the final job being returned as the return value of `#wait` or passed
|
70
|
+
to the next item in the chain.
|
71
|
+
|
72
|
+
If the callback returns something other than a future, it will break the chain
|
73
|
+
and its value is returned by `#wait`.
|
74
|
+
|
75
|
+
## timeout and interval
|
76
|
+
|
77
|
+
`#wait` takes optional parameters:
|
78
|
+
|
79
|
+
* `timeout`: How long to wait before raising a `TimeoutError` in seconds (default: 60).
|
80
|
+
* `interval`: How often to query Redis for job status (default: 0.2).
|
81
|
+
|
82
|
+
## status
|
83
|
+
|
84
|
+
At any time you can call `future.status` to get back the current status of the
|
85
|
+
job, even if it's not completed yet. If the last job in the chain is not yet
|
86
|
+
running, this will return `nil`.
|
87
|
+
|
88
|
+
## Waiting for multiple futures
|
89
|
+
|
90
|
+
If you have kicked off multiple jobs at once, you can wait for them all with
|
91
|
+
the module-level `.wait`:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
f1 = CreateContainer.future('image_name' => 'redis')
|
95
|
+
f2 = CreateContainer.future('image_name' => 'ruby')
|
96
|
+
s1, s2 = Resque::Plugins::Status::Future.wait(f1, f2, timeout: 10)
|
97
|
+
```
|
98
|
+
|
99
|
+
The statuses are returned in the same order as the futures were passed in,
|
100
|
+
not the order in which they completed.
|
101
|
+
|
102
|
+
## Running the specs
|
103
|
+
|
104
|
+
The specs need a Redis container and some Resque workers running before
|
105
|
+
they will pass. The easiest way to get these running is using Docker:
|
106
|
+
|
107
|
+
```sh
|
108
|
+
docker run -p 6379:6379 -d redis
|
109
|
+
COUNT=3 QUEUE=* rake resque:workers
|
110
|
+
```
|
111
|
+
|
112
|
+
## Authors
|
113
|
+
|
114
|
+
Rich Daley, 2015
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require_relative "./spec/support/examples.rb"
|
4
|
+
require "resque/tasks"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
task 'resque:setup' do
|
11
|
+
Resque.logger = Logger.new(STDOUT)
|
12
|
+
Resque.logger.level = Logger::INFO
|
13
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "resque-status-future"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'resque-status'
|
2
|
+
require 'timeout' # for the exception
|
3
|
+
|
4
|
+
class Resque::Plugins::Status::Future
|
5
|
+
def initialize(id=nil)
|
6
|
+
@id = id
|
7
|
+
@parent = nil
|
8
|
+
@callback = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_accessor :id, :parent, :callback
|
12
|
+
|
13
|
+
# Get the current status of this job as a Resque::Plugins::Status::Hash.
|
14
|
+
# Returns nil if the job hasn't started yet or this is a non-job future.
|
15
|
+
def status
|
16
|
+
id ? Resque::Plugins::Status::Hash.get(id) : nil
|
17
|
+
end
|
18
|
+
|
19
|
+
# Create a future for the given callback. If you #wait on the new future,
|
20
|
+
# it will wait for the parent future to complete, then pass its status
|
21
|
+
# to the provided block.
|
22
|
+
#
|
23
|
+
# If the provided block returns a future, that future will be waited for
|
24
|
+
# after the callback completes.
|
25
|
+
def then(&block)
|
26
|
+
f = Resque::Plugins::Status::Future.new
|
27
|
+
f.parent = self
|
28
|
+
f.callback = block
|
29
|
+
f
|
30
|
+
end
|
31
|
+
|
32
|
+
# Wait for the operation to complete and return its result as
|
33
|
+
# a Resque::Plugins::Status::Hash. Raises TimeoutError if it
|
34
|
+
# reaches the timeout without completing.
|
35
|
+
#
|
36
|
+
# If this is a future that has been chained with #then, wait for
|
37
|
+
# the chain and then execute the callback with the return value of the
|
38
|
+
# last element in the chain as a parameter. If the callback returns a
|
39
|
+
# future, wait for it too.
|
40
|
+
#
|
41
|
+
def wait(options={})
|
42
|
+
self.class.wait(self, options).first
|
43
|
+
end
|
44
|
+
|
45
|
+
# Wait for multiple futures at the same time. The status are returned as
|
46
|
+
# an array, in the same order as the futures were passed in. Options are
|
47
|
+
# the same as for #wait.
|
48
|
+
def self.wait(*futures)
|
49
|
+
# Pop options off the end if they're provided
|
50
|
+
options = futures.last.kind_of?(Hash) ? futures.pop : {}
|
51
|
+
|
52
|
+
interval = options[:interval] || 0.2
|
53
|
+
timeout = options[:timeout] || 60
|
54
|
+
returns = {}
|
55
|
+
start_time = Time.now
|
56
|
+
unfinished = futures
|
57
|
+
loop do
|
58
|
+
unfinished.each do |f|
|
59
|
+
finished, retval = f.send(:check_if_finished)
|
60
|
+
if finished
|
61
|
+
returns[f] = retval
|
62
|
+
end
|
63
|
+
end
|
64
|
+
unfinished = futures.reject {|f| returns.has_key? f}
|
65
|
+
if unfinished.empty?
|
66
|
+
return futures.map {|f| returns[f]}
|
67
|
+
end
|
68
|
+
if (Time.now - start_time) > timeout
|
69
|
+
raise Timeout::Error
|
70
|
+
end
|
71
|
+
sleep interval
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
protected
|
76
|
+
|
77
|
+
# Check if this particular future has completed and get the return
|
78
|
+
# value if it has. Returns two values (finished, value) in case value
|
79
|
+
# is false
|
80
|
+
def check_if_finished
|
81
|
+
if parent
|
82
|
+
return parent_check
|
83
|
+
else
|
84
|
+
st = status
|
85
|
+
if st.completed?
|
86
|
+
return [true, st]
|
87
|
+
else
|
88
|
+
return [false, nil]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Check if the parent has completed. If it has, execute the callback and
|
94
|
+
# return its return value, removing it as the parent
|
95
|
+
def parent_check
|
96
|
+
# If the parent has a parent, check that
|
97
|
+
if parent.parent
|
98
|
+
_, st = parent.parent_check
|
99
|
+
else
|
100
|
+
st = parent.status
|
101
|
+
end
|
102
|
+
if st and st.completed?
|
103
|
+
# Execute the callback
|
104
|
+
retval = callback.(st)
|
105
|
+
self.parent = nil
|
106
|
+
self.callback = nil
|
107
|
+
# If the retval is a future, set our id and continue
|
108
|
+
if retval.kind_of? Resque::Plugins::Status::Future
|
109
|
+
self.id = retval.id
|
110
|
+
# If it's something else, we've reached the bottom of the chain -
|
111
|
+
# return it
|
112
|
+
else
|
113
|
+
return [true, retval]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
return [false, nil]
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
# Monkeypatch RPS to include a future method that is like create() but returns
|
122
|
+
# a future
|
123
|
+
module Resque::Plugins::Status::ClassMethods
|
124
|
+
def future(*args)
|
125
|
+
Resque::Plugins::Status::Future.new(create *args)
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'resque/plugins/status/future'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "resque-status-future"
|
7
|
+
spec.version = "0.4.0"
|
8
|
+
spec.authors = ["Rich Daley", "David Collom"]
|
9
|
+
spec.email = ["david.collom@williamhill.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Adds support for futures to resque-status}
|
12
|
+
spec.homepage = "https://github.com/fishpercolator/resque-status-future"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
15
|
+
spec.bindir = "exe"
|
16
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency 'resque-status', '~> 0.5'
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-status-future
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rich Daley
|
9
|
+
- David Collom
|
10
|
+
autorequire:
|
11
|
+
bindir: exe
|
12
|
+
cert_chain: []
|
13
|
+
date: 2016-04-05 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: resque-status
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0.5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0.5'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: bundler
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '1.10'
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.10'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '10.0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: pry
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
type: :development
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
description:
|
96
|
+
email:
|
97
|
+
- david.collom@williamhill.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- .rspec
|
104
|
+
- .travis.yml
|
105
|
+
- Gemfile
|
106
|
+
- Gemfile.lock
|
107
|
+
- LICENSE
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
112
|
+
- lib/resque-status-future.rb
|
113
|
+
- lib/resque/plugins/status/future.rb
|
114
|
+
- resque-status-future.gemspec
|
115
|
+
homepage: https://github.com/fishpercolator/resque-status-future
|
116
|
+
licenses: []
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 1.8.23
|
136
|
+
signing_key:
|
137
|
+
specification_version: 3
|
138
|
+
summary: Adds support for futures to resque-status
|
139
|
+
test_files: []
|