skyrunner 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/bin/skyrunner +10 -4
- data/lib/skyrunner/job.rb +43 -15
- data/lib/skyrunner/version.rb +1 -1
- data/lib/skyrunner.rb +13 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51e28867dae3ad56df63869dbf4b894350f0a848
|
4
|
+
data.tar.gz: 4a3483978b3bdec3504ec4ff5fdaf75a98e3da48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b97035c04bed836c34d2f6d102f74906e3eaa3b6b3a7e89667f28df9c8ba76a7433db4b3d7e57bcb75dce8e1c25519f4708586b904c6bb63d02e3c054242a4a
|
7
|
+
data.tar.gz: 5394bd636e6bcb4ea5b720b3265098aedcbe01a29e301085a7b1262ea8a29298c14c1d736c5663e18061e1198915684dbc07cf5d49712557293cc063737b09a0
|
data/README.md
CHANGED
@@ -39,7 +39,7 @@ To gracefully shut down a consumer, send it SIGINT (or hit Ctrl-C if you have a
|
|
39
39
|
|
40
40
|
See `jobs/example_job.rb` for an example job. To run a job, just call `execute!` on the job, passing any named job arguments you want. The job class should implement the method `run`. This method will get passed the job arguments. For each task you want consumers to run, `run` should yield an array of two elements, the first being the name of the method on the job class to run for the task, and the second a Hash of method arguments. Note that the consumer is (by default) multi-threaded, so please be sure your task methods are thread-safe.
|
41
41
|
|
42
|
-
You can specify `
|
42
|
+
You can specify `on_completed` and `on_failed` method(s) to call when the tasks are all completed, or if any of them fail, respectively. These methods will also be passed the original job arguments. Importantly, the completion method is guaranteed to be called once and only once, when the final task has been completed. If your job runs multiple tasks, SkyRunner will coordinate the job via DynamoDB and the last consumer will run the completion method. If your job runs a single task, DynamoDB is not needed for coordination and the consumer which consumes your task will call the completion or failure method.
|
43
43
|
|
44
44
|
If any of your tasks fail in a job, consumers will stop consuming tasks for that job and deplete any queued tasks on SQS. SkyRunner does not currently retry failed tasks, for now you'll need to implement your own retry logic in your task method instead.
|
45
45
|
|
data/bin/skyrunner
CHANGED
@@ -36,11 +36,11 @@ opts = Trollop::options do
|
|
36
36
|
opt :num_threads, "Number of consumer threads.", default: 10
|
37
37
|
end
|
38
38
|
|
39
|
-
SkyRunner
|
40
|
-
SkyRunner
|
41
|
-
SkyRunner
|
39
|
+
SkyRunner::dynamo_db_table_name = opts[:dynamo_db_table_name]
|
40
|
+
SkyRunner::sqs_queue_name = opts[:sqs_queue_name]
|
41
|
+
SkyRunner::consumer_threads = opts[:num_threads].to_i
|
42
42
|
|
43
|
-
COMMANDS = ["init", "purge", "consume", "test"]
|
43
|
+
COMMANDS = ["init", "purge", "consume", "test", "test_local"]
|
44
44
|
|
45
45
|
Trollop::die "Must specify command" unless COMMANDS.include?(ARGV[0])
|
46
46
|
|
@@ -53,6 +53,12 @@ when "purge"
|
|
53
53
|
SkyRunner.init!(purge: true)
|
54
54
|
when "consume"
|
55
55
|
SkyRunner.consume!
|
56
|
+
when "test_local"
|
57
|
+
$: << "."
|
58
|
+
require "#{File.dirname(__FILE__)}/../jobs/example_job"
|
59
|
+
|
60
|
+
SkyRunner.run_locally = true
|
61
|
+
ExampleJobModule::ExampleJob.new.execute!(number_of_tasks: 100)
|
56
62
|
when "test"
|
57
63
|
$: << "."
|
58
64
|
require "#{File.dirname(__FILE__)}/../jobs/example_job"
|
data/lib/skyrunner/job.rb
CHANGED
@@ -33,6 +33,8 @@ module SkyRunner::Job
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def execute!(job_args = {})
|
36
|
+
return execute_local!(job_args) if SkyRunner::run_locally
|
37
|
+
|
36
38
|
job_id = SecureRandom.hex
|
37
39
|
self.skyrunner_job_id = job_id
|
38
40
|
|
@@ -120,8 +122,46 @@ module SkyRunner::Job
|
|
120
122
|
end
|
121
123
|
end
|
122
124
|
|
125
|
+
def fire_post_event_method(event_type, job_args)
|
126
|
+
(self.class.job_event_methods[event_type] || []).each do |method|
|
127
|
+
if self.method(method).arity == 0 && self.method(method).parameters.size == 0
|
128
|
+
self.send(method)
|
129
|
+
else
|
130
|
+
self.send(method, job_args.symbolize_keys)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
123
135
|
private
|
124
136
|
|
137
|
+
def execute_local!(job_args = {})
|
138
|
+
job_id = SecureRandom.hex
|
139
|
+
self.skyrunner_job_id = job_id
|
140
|
+
task_arg_list = []
|
141
|
+
|
142
|
+
self.run(job_args) do |*task_args|
|
143
|
+
task_arg_list << task_args
|
144
|
+
end
|
145
|
+
|
146
|
+
task_arg_list.shuffle!
|
147
|
+
|
148
|
+
task_arg_list.each_with_index do |task_args, task_index|
|
149
|
+
task = self.class.new
|
150
|
+
task.skyrunner_job_id = job_id
|
151
|
+
|
152
|
+
begin
|
153
|
+
task.send(task_args[0].to_sym, task_args[1].symbolize_keys)
|
154
|
+
rescue Exception => e
|
155
|
+
task.fire_post_event_method(:failed, job_args)
|
156
|
+
break
|
157
|
+
end
|
158
|
+
|
159
|
+
if task_index == task_arg_list.size - 1
|
160
|
+
task.fire_post_event_method(:completed, job_args)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
125
165
|
def dynamo_db_record
|
126
166
|
SkyRunner.dynamo_db_table.items[self.skyrunner_job_id, self.skyrunner_job_id]
|
127
167
|
end
|
@@ -138,13 +178,7 @@ module SkyRunner::Job
|
|
138
178
|
end
|
139
179
|
end
|
140
180
|
|
141
|
-
|
142
|
-
if self.method(method).arity == 0 && self.method(method).parameters.size == 0
|
143
|
-
self.send(method)
|
144
|
-
else
|
145
|
-
self.send(method, job_args.symbolize_keys)
|
146
|
-
end
|
147
|
-
end
|
181
|
+
self.fire_post_event_method(:failed, job_args)
|
148
182
|
|
149
183
|
unless is_solo?
|
150
184
|
delete_task_records! rescue nil
|
@@ -178,13 +212,7 @@ module SkyRunner::Job
|
|
178
212
|
end
|
179
213
|
end
|
180
214
|
|
181
|
-
|
182
|
-
if self.method(method).arity == 0 && self.method(method).parameters.size == 0
|
183
|
-
self.send(method)
|
184
|
-
else
|
185
|
-
self.send(method, job_args.symbolize_keys)
|
186
|
-
end
|
187
|
-
end
|
215
|
+
self.fire_post_event_method(:completed, job_args)
|
188
216
|
|
189
217
|
unless is_solo?
|
190
218
|
delete_task_records! rescue nil
|
@@ -205,7 +233,7 @@ module SkyRunner::Job
|
|
205
233
|
delete_items_queued = false
|
206
234
|
threads = []
|
207
235
|
|
208
|
-
1.upto([1, (SkyRunner
|
236
|
+
1.upto([1, (SkyRunner::consumer_threads / 4.0).floor].max) do
|
209
237
|
threads << Thread.new do
|
210
238
|
|
211
239
|
db_table = SkyRunner.dynamo_db_table
|
data/lib/skyrunner/version.rb
CHANGED
data/lib/skyrunner.rb
CHANGED
@@ -20,7 +20,7 @@ module SkyRunner
|
|
20
20
|
table = self.dynamo_db_table
|
21
21
|
|
22
22
|
if !table.exists? || params[:purge]
|
23
|
-
table_name = SkyRunner
|
23
|
+
table_name = SkyRunner::dynamo_db_table_name
|
24
24
|
|
25
25
|
if table.exists? && params[:purge]
|
26
26
|
SkyRunner.log :warn, "Purging DynamoDB table #{table_name}."
|
@@ -32,8 +32,8 @@ module SkyRunner
|
|
32
32
|
SkyRunner.log :info, "Creating DynamoDB table #{table_name}."
|
33
33
|
|
34
34
|
table = dynamo_db.tables.create(table_name,
|
35
|
-
SkyRunner
|
36
|
-
SkyRunner
|
35
|
+
SkyRunner::dynamo_db_read_capacity,
|
36
|
+
SkyRunner::dynamo_db_write_capacity,
|
37
37
|
hash_key: { id: :string },
|
38
38
|
range_key: { task_id: :string })
|
39
39
|
|
@@ -43,7 +43,7 @@ module SkyRunner
|
|
43
43
|
queue = self.sqs_queue
|
44
44
|
|
45
45
|
if !queue || params[:purge]
|
46
|
-
queue_name = SkyRunner
|
46
|
+
queue_name = SkyRunner::sqs_queue_name
|
47
47
|
|
48
48
|
if queue && params[:purge]
|
49
49
|
SkyRunner.log :warn, "Purging SQS queue #{queue_name}. Waiting 65 seconds to re-create."
|
@@ -55,8 +55,8 @@ module SkyRunner
|
|
55
55
|
SkyRunner.log :info, "Creating SQS queue #{queue_name}."
|
56
56
|
|
57
57
|
queue = sqs.queues.create(queue_name,
|
58
|
-
visibility_timeout: SkyRunner
|
59
|
-
message_retention_period: SkyRunner
|
58
|
+
visibility_timeout: SkyRunner::sqs_visibility_timeout,
|
59
|
+
message_retention_period: SkyRunner::sqs_message_retention_period)
|
60
60
|
end
|
61
61
|
|
62
62
|
true
|
@@ -131,7 +131,7 @@ module SkyRunner
|
|
131
131
|
|
132
132
|
break if SkyRunner::stop_consuming?
|
133
133
|
|
134
|
-
sleep 1 while local_queue.size >= SkyRunner
|
134
|
+
sleep 1 while local_queue.size >= SkyRunner::consumer_threads
|
135
135
|
|
136
136
|
received_messages = []
|
137
137
|
|
@@ -197,7 +197,7 @@ module SkyRunner
|
|
197
197
|
table = Thread.current.thread_variable_get(:skyrunner_dyn_table)
|
198
198
|
return table if table
|
199
199
|
|
200
|
-
dynamo_db.tables[SkyRunner
|
200
|
+
dynamo_db.tables[SkyRunner::dynamo_db_table_name].tap do |table|
|
201
201
|
table.load_schema if table && table.exists?
|
202
202
|
Thread.current.thread_variable_set(:skyrunner_dyn_table, table)
|
203
203
|
end
|
@@ -205,14 +205,14 @@ module SkyRunner
|
|
205
205
|
|
206
206
|
def self.sqs_queue
|
207
207
|
begin
|
208
|
-
sqs.queues.named(SkyRunner
|
208
|
+
sqs.queues.named(SkyRunner::sqs_queue_name)
|
209
209
|
rescue AWS::SQS::Errors::NonExistentQueue => e
|
210
210
|
return nil
|
211
211
|
end
|
212
212
|
end
|
213
213
|
|
214
214
|
def self.log(type, message)
|
215
|
-
SkyRunner
|
215
|
+
SkyRunner::logger.send(type, "[SkyRunner] #{message}")
|
216
216
|
end
|
217
217
|
|
218
218
|
mattr_accessor :dynamo_db_table_name
|
@@ -239,6 +239,9 @@ module SkyRunner
|
|
239
239
|
mattr_accessor :consumer_threads
|
240
240
|
@@consumer_threads = 10
|
241
241
|
|
242
|
+
mattr_accessor :run_locally
|
243
|
+
@@run_locally = false
|
244
|
+
|
242
245
|
mattr_accessor :stop_consuming_flag
|
243
246
|
|
244
247
|
@@stop_consuming_mutex = Mutex.new
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: skyrunner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Fodor
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|