shoryuken-later 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzYxOWE4MDhiZmVkMjc5YjBjNTE4ZjJiY2VmOTY1OGI1MDhkYWU3NA==
5
+ data.tar.gz: !binary |-
6
+ ODMzY2ZkNGExNzlmNjYwZWI3NjcxMWI4MWE3YWQ0NDE4MmZkZDBiYw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ Yjg1OGYxNzRiOWUxNDg5NTVkZGQxNGRmNzM5Njc5YzQzNTcxZTUxM2U3MTRj
10
+ NDU1YmRiYzQxYTgzMDQ2MWNlNDcyZGU1MDMyOGMzZTFiMTg2MjA0ZTQzNmIz
11
+ MzlkZGZmYmM2N2EyMTI1MzI1MGYwNTg1ZjUwOTk0ZTRhNThkOGY=
12
+ data.tar.gz: !binary |-
13
+ MjJjY2Q5MWViNTBlMDFjNjc4Y2RiNWYwZDE0MTYwNThiZTMyMzI2ZWMyNjdi
14
+ MjhkMzA0ZjAzNGMxMWE0OTU5NzAzNzE1NGY2NTdkMDUyOTVjYjAwNDdmY2Y3
15
+ ZDRlNTlmNjI2ZGQ5MDJlOWIwZjIxMTJlMWI5ZWUxNjZiYzVmZTc=
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.rbc
2
+ .rspec
3
+ /pkg
4
+ /log
5
+ /tmp
6
+ /spec/tmp
7
+ **.orig
8
+ /.buildpath
9
+ /.project
10
+ /.settings
11
+ shoryuken.yml
12
+
13
+ ## Environment normalisation:
14
+ /.bundle
15
+ /vendor/bundle
16
+
17
+ # these should all be checked in to normalise the environment:
18
+ # Gemfile.lock, .ruby-version, .ruby-gemset
19
+
20
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
21
+ .rvmrc
22
+ /Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shoryuken-later.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,166 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
166
+
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # shoryuken-later
2
+
3
+ A scheduling plugin for [Shoryuken](https://github.com/phstc/shoryuken) that uses [Dynamo DB](https://aws.amazon.com/dynamodb/)
4
+ to schedule messages arbitrarily far into the future.
5
+
6
+ ## Features
7
+
8
+ ### Integration with Shoryuken::Worker
9
+
10
+ A new method named `perform_later` is added to `Shoryuken::Worker` allowing messages to be delayed arbitrarily far into the future. If the delay is 15 minutes or less, then the message is enqueued into the specified SQS `:queue` as usual. Otherwise, the message is inserted into the specified DynamoDB `:schedule_table`.
11
+
12
+ ```ruby
13
+ require 'shoryuken-later'
14
+
15
+ class MyWorker
16
+ include Shoryuken::Worker
17
+
18
+ shoryuken_options queue: 'default', schedule_table: 'default_schedule'
19
+ end
20
+
21
+ # Schedules a message to be processed 30 minutes from now.
22
+ MyWorker.perform_later(Time.now + 30 * 60, 'Foobar')
23
+ ```
24
+
25
+ ### One or more schedule tables
26
+
27
+ Supports polling one or more DynamoDB tables for messages.
28
+
29
+ ```yaml
30
+ later:
31
+ tables:
32
+ - default_schedule
33
+ - other_schedule
34
+ ```
35
+
36
+ ### Namespaced configuration
37
+
38
+ You can use the same configuration file for both `Shoryuken` and `Shoryuken::Later`, because the new configuration options are namespaced.
39
+
40
+ ```yaml
41
+ # This key is used by both Shoryuken and Shoryuken::Later
42
+ aws:
43
+ access_key_id: ... # or <%= ENV['AWS_ACCESS_KEY_ID'] %>
44
+ secret_access_key: ... # or <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
45
+ region: us-east-1 # or <%= ENV['AWS_REGION'] %>
46
+
47
+ # This key is only used by Shoryuken::Later
48
+ later:
49
+ delay: 5 * 60 # How frequently to poll the schedule table, in seconds.
50
+ tables:
51
+ - table1
52
+
53
+ # These keys are used by both Shoryuken and Shoryuken::Later
54
+ logfile: some/path/to/file.log
55
+ pidfile: some/path/to/file.pid
56
+
57
+ # These keys are only used by Shoryuken
58
+ concurrency: 3
59
+ delay: 0
60
+ queues:
61
+ - [queue1, 1]
62
+ - [queue2, 2]
63
+ ```
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ $stdout.sync = true
4
+
5
+ begin
6
+ require 'rspec/core/rake_task'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task :default => :spec
11
+ rescue LoadError
12
+ # no rspec available
13
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/shoryuken/later/cli'
4
+
5
+ begin
6
+ Shoryuken::Later::CLI.instance.run(ARGV)
7
+ rescue => e
8
+ raise e if $DEBUG
9
+ STDERR.puts e.message
10
+ STDERR.puts e.backtrace.join("\n")
11
+ exit 1
12
+ end
@@ -0,0 +1,73 @@
1
+ # Practically all of this has been "borrowed" from Shoryuken.
2
+
3
+ # @see ActiveJob::QueueAdapter::ShoryukenAdapter
4
+
5
+ # ActiveJob docs: http://edgeguides.rubyonrails.org/active_job_basics.html
6
+ # Example adapters ref: https://github.com/rails/rails/tree/master/activejob/lib/active_job/queue_adapters
7
+
8
+ require 'shoryuken-later'
9
+
10
+ module ActiveJob
11
+ module QueueAdapters
12
+ # == Shoryuken::Later adapter for Active Job
13
+ #
14
+ # Shoryuken ("sho-ryu-ken") is a super-efficient AWS SQS thread based message processor.
15
+ # Shoryuken::Later allows messages to be delayed arbitrarily far into the future.
16
+ #
17
+ # Read more about Shoryuken {here}[https://github.com/phstc/shoryuken].
18
+ # Read more about Shoryuken::Later {here}[https://github.com/joekhoobyar/shoryuken-later].
19
+ #
20
+ # To use Shoryuken::Later set the queue_adapter config to +:shoryuken_later+.
21
+ #
22
+ # Rails.application.config.active_job.queue_adapter = :shoryuken_later
23
+ class ShoryukenLaterAdapter
24
+ class << self
25
+ def enqueue(job) #:nodoc:
26
+ register_worker!(job)
27
+
28
+ Shoryuken::Client.send_message(job.queue_name, job.serialize, message_attributes: message_attributes)
29
+ end
30
+
31
+ def enqueue_at(job, timestamp) #:nodoc:
32
+ register_worker!(job)
33
+
34
+ delay = (timestamp - Time.current.to_f).round
35
+ if delay > 15.minutes
36
+ Shoryuken::Later::Client.put_item(Shoryuken::Later.default_table, perform_at: delay.to_i,
37
+ shoryuken_queue: job.queue_name, shoryuken_class: JobWrapper.to_s,
38
+ shoryuken_args: JSON.dump(body: job.serialize, options: {}))
39
+ else
40
+ Shoryuken::Client.send_message(job.queue_name, job.serialize, delay_seconds: delay,
41
+ message_attributes: message_attributes)
42
+ end
43
+ end
44
+
45
+
46
+ private
47
+
48
+ def register_worker!(job)
49
+ Shoryuken.register_worker(job.queue_name, JobWrapper)
50
+ end
51
+
52
+ def message_attributes
53
+ @message_attributes ||= {
54
+ 'shoryuken_class' => {
55
+ string_value: JobWrapper.to_s,
56
+ data_type: 'String'
57
+ }
58
+ }
59
+ end
60
+ end
61
+
62
+ class JobWrapper #:nodoc:
63
+ include Shoryuken::Worker
64
+
65
+ shoryuken_options body_parser: :json, auto_delete: true
66
+
67
+ def perform(sqs_msg, hash)
68
+ Base.execute hash
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,281 @@
1
+ # Most of this has been "borrowed" from Shoryuken.
2
+
3
+ # @see Shoryuken::CLI
4
+ $stdout.sync = true
5
+
6
+ require 'singleton'
7
+ require 'optparse'
8
+ require 'erb'
9
+ require 'shoryuken/later'
10
+
11
+ module Shoryuken
12
+ module Later
13
+ class CLI
14
+ include Shoryuken::Util
15
+ include Singleton
16
+
17
+ attr_accessor :launcher
18
+
19
+ def run(args)
20
+ self_read, self_write = IO.pipe
21
+
22
+ %w[INT TERM USR1 USR2 TTIN].each do |sig|
23
+ trap sig do
24
+ self_write.puts(sig)
25
+ end
26
+ end
27
+
28
+ setup_options(args) do |cli_options|
29
+ # this needs to happen before configuration is parsed, since it may depend on Rails env
30
+ load_rails if cli_options[:rails]
31
+ end
32
+ initialize_logger
33
+ require_workers
34
+ validate!
35
+ daemonize
36
+ write_pid
37
+ load_celluloid
38
+
39
+ require 'shoryuken/later/launcher'
40
+ @launcher = Shoryuken::Later::Launcher.new
41
+
42
+ begin
43
+ launcher.run
44
+
45
+ while readable_io = IO.select([self_read])
46
+ signal = readable_io.first[0].gets.strip
47
+ handle_signal(signal)
48
+ end
49
+ rescue Interrupt
50
+ launcher.stop(shutdown: true)
51
+ exit 0
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def load_celluloid
58
+ raise "Celluloid cannot be required until here, or it will break Shoryuken::Later's daemonization" if defined?(::Celluloid) && Shoryuken::Later.options[:daemon]
59
+
60
+ # Celluloid can't be loaded until after we've daemonized
61
+ # because it spins up threads and creates locks which get
62
+ # into a very bad state if forked.
63
+ require 'celluloid/autostart'
64
+ Celluloid.logger = (Shoryuken::Later.options[:verbose] ? Shoryuken::Later.logger : nil)
65
+
66
+ require 'shoryuken/later/manager'
67
+ end
68
+
69
+ def load_rails
70
+ # Adapted from: https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/cli.rb
71
+
72
+ require 'rails'
73
+ if ::Rails::VERSION::MAJOR < 4
74
+ require File.expand_path("config/environment.rb")
75
+ ::Rails.application.eager_load!
76
+ else
77
+ # Painful contortions, see 1791 for discussion
78
+ require File.expand_path("config/application.rb")
79
+ ::Rails::Application.initializer "shoryuken-later.eager_load" do
80
+ ::Rails.application.config.eager_load = true
81
+ end
82
+ require File.expand_path("config/environment.rb")
83
+ end
84
+
85
+ logger.info "Rails environment loaded"
86
+ end
87
+
88
+ def daemonize
89
+ return unless Shoryuken::Later.options[:daemon]
90
+
91
+ raise ArgumentError, "You really should set a logfile if you're going to daemonize" unless Shoryuken::Later.options[:logfile]
92
+
93
+ files_to_reopen = []
94
+ ObjectSpace.each_object(File) do |file|
95
+ files_to_reopen << file unless file.closed?
96
+ end
97
+
98
+ Process.daemon(true, true)
99
+
100
+ files_to_reopen.each do |file|
101
+ begin
102
+ file.reopen file.path, "a+"
103
+ file.sync = true
104
+ rescue ::Exception
105
+ end
106
+ end
107
+
108
+ [$stdout, $stderr].each do |io|
109
+ File.open(Shoryuken::Later.options[:logfile], 'ab') do |f|
110
+ io.reopen(f)
111
+ end
112
+ io.sync = true
113
+ end
114
+ $stdin.reopen('/dev/null')
115
+
116
+ initialize_logger
117
+ end
118
+
119
+ def write_pid
120
+ if path = Shoryuken::Later.options[:pidfile]
121
+ File.open(path, 'w') do |f|
122
+ f.puts Process.pid
123
+ end
124
+ end
125
+ end
126
+
127
+ def parse_options(argv)
128
+ opts = {}
129
+
130
+ @parser = OptionParser.new do |o|
131
+ o.on '-d', '--daemon', 'Daemonize process' do |arg|
132
+ opts[:daemon] = arg
133
+ end
134
+
135
+ o.on '-t', '--table TABLE...', 'Table to process' do |arg|
136
+ Shoryuken::Later.tables << args
137
+ end
138
+
139
+ o.on '-r', '--require [PATH|DIR]', 'Location of the worker' do |arg|
140
+ opts[:require] = arg
141
+ end
142
+
143
+ o.on '-C', '--config PATH', 'Path to YAML config file' do |arg|
144
+ opts[:config_file] = arg
145
+ end
146
+
147
+ o.on '-R', '--rails', 'Load Rails' do |arg|
148
+ opts[:rails] = arg
149
+ end
150
+
151
+ o.on '-L', '--logfile PATH', 'Path to writable logfile' do |arg|
152
+ opts[:logfile] = arg
153
+ end
154
+
155
+ o.on '-P', '--pidfile PATH', 'Path to pidfile' do |arg|
156
+ opts[:pidfile] = arg
157
+ end
158
+
159
+ o.on '-v', '--verbose', 'Print more verbose output' do |arg|
160
+ opts[:verbose] = arg
161
+ end
162
+
163
+ o.on '-V', '--version', 'Print version and exit' do |arg|
164
+ puts "Shoryuken::Later #{Shoryuken::Later::VERSION}"
165
+ exit 0
166
+ end
167
+ end
168
+
169
+ @parser.banner = 'shoryuken-later [options]'
170
+ @parser.on_tail '-h', '--help', 'Show help' do
171
+ logger.info @parser
172
+ exit 1
173
+ end
174
+ @parser.parse!(argv)
175
+ opts
176
+ end
177
+
178
+ def handle_signal(sig)
179
+ logger.info "Got #{sig} signal"
180
+
181
+ case sig
182
+ when 'USR1'
183
+ logger.info "Received USR1, will soft shutdown down"
184
+
185
+ launcher.stop
186
+
187
+ exit 0
188
+ when 'TTIN'
189
+ Thread.list.each do |thread|
190
+ logger.info "Thread TID-#{thread.object_id.to_s(36)} #{thread['label']}"
191
+ if thread.backtrace
192
+ logger.info thread.backtrace.join("\n")
193
+ else
194
+ logger.info "<no backtrace available>"
195
+ end
196
+ end
197
+
198
+ idle = launcher.manager.instance_variable_get(:@idle).size
199
+ busy = launcher.manager.instance_variable_get(:@busy).size
200
+ tables = launcher.manager.instance_variable_get(:@tables)
201
+
202
+ logger.info "Idle: #{idle}, Busy: #{busy}, Polled Tables: #{tables.join(', ')}"
203
+ else
204
+ logger.info "Received #{sig}, will shutdown down"
205
+
206
+ raise Interrupt
207
+ end
208
+ end
209
+
210
+ def setup_options(args)
211
+ options = parse_options(args)
212
+
213
+ # yield parsed options in case we need to do more setup before configuration is parsed
214
+ yield(options) if block_given?
215
+
216
+ config = options[:config_file] ? parse_config(options[:config_file]).deep_symbolize_keys : {}
217
+
218
+ Shoryuken::Later.options.merge!(config)
219
+
220
+ Shoryuken::Later.options.merge!(options)
221
+
222
+ # Tables from command line options take precedence...
223
+ unless Shoryuken::Later.tables.any?
224
+ tables = Shoryuken::Later.options[:later][:tables]
225
+
226
+ # Use the default table if none were specified in the config file.
227
+ tables << Shoryuken::Later.default_table if tables.empty?
228
+
229
+ Shoryuken::Later.tables.replace(tables)
230
+ end
231
+ end
232
+
233
+ def parse_config(config_file)
234
+ if File.exist?(config_file)
235
+ YAML.load(ERB.new(IO.read(config_file)).result)
236
+ else
237
+ raise ArgumentError, "Config file #{config_file} does not exist"
238
+ end
239
+ end
240
+
241
+ def initialize_logger
242
+ Shoryuken::Logging.initialize_logger(Shoryuken::Later.options[:logfile]) if Shoryuken::Later.options[:logfile]
243
+
244
+ Shoryuken::Later.logger.level = Logger::DEBUG if Shoryuken::Later.options[:verbose]
245
+ end
246
+
247
+ def validate!
248
+ raise ArgumentError, 'No tables given to poll' if Shoryuken::Later.tables.empty?
249
+
250
+ if Shoryuken::Later.options[:aws][:access_key_id].nil? && Shoryuken::Later.options[:aws][:secret_access_key].nil?
251
+ if ENV['AWS_ACCESS_KEY_ID'].nil? && ENV['AWS_SECRET_ACCESS_KEY'].nil?
252
+ raise ArgumentError, 'No AWS credentials supplied'
253
+ end
254
+ end
255
+
256
+ initialize_aws
257
+
258
+ Shoryuken::Later.tables.uniq.each do |table|
259
+ # validate all tables and AWS credentials consequently
260
+ begin
261
+ unless Shoryuken::Later::Client.tables(table).exists?
262
+ raise ArgumentError, "Table '#{table}' does not exist"
263
+ end
264
+ rescue => e
265
+ raise
266
+ end
267
+ end
268
+ end
269
+
270
+ def initialize_aws
271
+ # aws-sdk tries to load the credentials from the ENV variables: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
272
+ # when not explicit supplied
273
+ AWS.config Shoryuken::Later.options[:aws] if Shoryuken::Later.options[:aws]
274
+ end
275
+
276
+ def require_workers
277
+ require Shoryuken::Later.options[:require] if Shoryuken::Later.options[:require]
278
+ end
279
+ end
280
+ end
281
+ end
@@ -0,0 +1,26 @@
1
+ require 'securerandom'
2
+
3
+ module Shoryuken
4
+ module Later
5
+ class Client
6
+ @@tables = {}
7
+
8
+ class << self
9
+ def tables(table)
10
+ @@tables[table.to_s] ||= ddb.tables[table].tap do |tbl|
11
+ tbl.hash_key = [:id, :string]
12
+ end
13
+ end
14
+
15
+ def put_item(table, attributes, options={unless_exists: 'id'})
16
+ attributes['id'] ||= SecureRandom.uuid
17
+ tables(table).items.create(attributes, options)
18
+ end
19
+
20
+ def ddb
21
+ @ddb ||= AWS::DynamoDB.new
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end