laboristo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gems +3 -0
- data/.gitignore +37 -0
- data/LICENSE +21 -0
- data/README.md +115 -0
- data/bin/laboristo +46 -0
- data/laboristo.gemspec +18 -0
- data/lib/laboristo.rb +46 -0
- data/makefile +9 -0
- data/test/helper.rb +42 -0
- data/test/queue_test.rb +66 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3733281eacf2c77397c8e7f452273bfb33e852bc
|
4
|
+
data.tar.gz: efdd5e9f806e245917c28339ada758b331f0821f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ed914bf20b5799719c6e569f1845a9061bc78c52935065cc7bf63bd12d596f6fa11838bde6786af7e826a103e9e4702771d835fba1d2e7d05746e4cb77feeea
|
7
|
+
data.tar.gz: 7abb5b6db3f92b7c619b6cb270f155e6329f3c70925f2f524ff7dc985b0560db16727b827d877f8f3586eac220fe86c4e661887fa89a4c6d44a452a4fb28e330
|
data/.gems
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
.gitignore
|
2
|
+
*.gem
|
3
|
+
*.gs
|
4
|
+
*.rbc
|
5
|
+
/.config
|
6
|
+
/coverage/
|
7
|
+
/InstalledFiles
|
8
|
+
/pkg/
|
9
|
+
/spec/reports/
|
10
|
+
/test/tmp/
|
11
|
+
/test/version_tmp/
|
12
|
+
/tmp/
|
13
|
+
.rubocop.yml
|
14
|
+
|
15
|
+
## Specific to RubyMotion:
|
16
|
+
.dat*
|
17
|
+
.repl_history
|
18
|
+
build/
|
19
|
+
|
20
|
+
## Documentation cache and generated files:
|
21
|
+
/.yardoc/
|
22
|
+
/_yardoc/
|
23
|
+
/doc/
|
24
|
+
/rdoc/
|
25
|
+
|
26
|
+
## Environment normalisation:
|
27
|
+
/.bundle/
|
28
|
+
/lib/bundler/man/
|
29
|
+
|
30
|
+
# for a library or gem, you might want to ignore these files since the code is
|
31
|
+
# intended to run in multiple environments; otherwise, check them in:
|
32
|
+
# Gemfile.lock
|
33
|
+
# .ruby-version
|
34
|
+
# .ruby-gemset
|
35
|
+
|
36
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
37
|
+
.rvmrc
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 matiasow
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# Laboristo
|
2
|
+
Laboristo is an attempt to port [Ost](https://github.com/soveran/ost) simplicity to AWS SQS.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Simply run:
|
7
|
+
|
8
|
+
$ gem install laboristo
|
9
|
+
|
10
|
+
### Using Bundler?
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'laboristo'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
|
23
|
+
## Configuration
|
24
|
+
|
25
|
+
Before using Laboristo, you will need to set some environment variables:
|
26
|
+
|
27
|
+
AWS_REGION=<your_aws_region>
|
28
|
+
AWS_ACCOUNT_ID=<your_aws_account_id>
|
29
|
+
AWS_ACCESS_KEY_ID=<your_aws_access_key>
|
30
|
+
AWS_SECRET_ACCESS_KEY=<your_aws_secret_key>
|
31
|
+
|
32
|
+
Please, refer to [AWS documentation](%28http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html%29) to obtain the corresponding values for your account.
|
33
|
+
|
34
|
+
## Considerations
|
35
|
+
|
36
|
+
First of all, if you are not familiar with AWS SQS I hardly recommend that you begin reading the [AWS SQS Getting Started Guide](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/Welcome.html).
|
37
|
+
|
38
|
+
Here are some considerations that you need to know when using Laboristo and SQS:
|
39
|
+
|
40
|
+
- Laboristo does not create queues. You will have to set them up before sending messages to it (pretty obvious, right?). For instance, you can use [AWS Console](http://console.aws.amazon.com/) to create quehes.
|
41
|
+
- When creating queues, you can set up queue attributes such as *DelaySeconds*, *MaximumMessageSize*, *MessageRetentionPeriod*, *Policy*, *ReceiveMessageWaitTimeSeconds* and *VisibilityTimeout*. Tune this settings according to your needs to efficiently use the service, and keep usage costs at a minimum level.
|
42
|
+
- Laboristo retrieves the message, yields the process to your code, and then deletes the message from the queue. If something fails in between, the message will go back to the queue. I encourage you to consider using [Dead Letter Queues](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html) to prevent eternal processing of invalid/wrong messages.
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
To push a message to the queue:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
require 'laboristo'
|
50
|
+
|
51
|
+
Laboristo['my_queue_name'] << 'some message'
|
52
|
+
```
|
53
|
+
|
54
|
+
And get messages from a queue:
|
55
|
+
```ruby
|
56
|
+
require 'laboristo'
|
57
|
+
Laboristo['my_queue_name'].each do |msg|
|
58
|
+
# Do something with msg...
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
### Workers
|
63
|
+
|
64
|
+
Workers can be as simple as this:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
require 'my_app'
|
68
|
+
|
69
|
+
Laboristo['my_queue'].each do |message|
|
70
|
+
# Do some magic stuff with the message
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
Place worker at ```./workers/my_worker.rb``` and run this code to run worker in foreground:
|
75
|
+
|
76
|
+
```
|
77
|
+
$ laboristo my_worker
|
78
|
+
```
|
79
|
+
|
80
|
+
To run it in background you can use ```-d``` flag to daemonize the process:
|
81
|
+
|
82
|
+
```
|
83
|
+
$ laboristo my_worker -d
|
84
|
+
```
|
85
|
+
|
86
|
+
You can stop the workers by killing the process. Keep in mind that, because of how SQS works, unprocessed messages will go back to the queue.
|
87
|
+
|
88
|
+
## Development
|
89
|
+
|
90
|
+
After cloning repository install dependencies using [dep](https://github.com/cyx/dep):
|
91
|
+
|
92
|
+
```
|
93
|
+
$ dep install
|
94
|
+
```
|
95
|
+
|
96
|
+
Set environment variables as explained before in this document, and run the tests:
|
97
|
+
|
98
|
+
```
|
99
|
+
$ make
|
100
|
+
```
|
101
|
+
|
102
|
+
### TO-DO:
|
103
|
+
|
104
|
+
First of all, add more tests! This gem stated as an experiment, so you can expect bugs.
|
105
|
+
|
106
|
+
In future versions, I'd like to improve the worker bin so that it can be started/stopped in a more elegant fashion than simply killing the process ;-)
|
107
|
+
|
108
|
+
Feel free to report bugs, open issues, comments and pull requests. Only keep in mind that I just want to keep this gem neat and simple.
|
109
|
+
|
110
|
+
## Credits
|
111
|
+
|
112
|
+
Most of the credit for this gem goes to [@soveran](https://github.com/soveran/ost) and [@djanowski](https://github.com/djanowski/ost-bin), who created the code this gem is based in. Kudos to them!
|
113
|
+
|
114
|
+
## License
|
115
|
+
Laboristo is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/bin/laboristo
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
stop = proc do
|
4
|
+
if defined?(Laboristo)
|
5
|
+
Laboristo.stop
|
6
|
+
else
|
7
|
+
exit 0
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
trap(:INT, &stop)
|
12
|
+
trap(:TERM, &stop)
|
13
|
+
|
14
|
+
usage = <<-EOS
|
15
|
+
Usage:
|
16
|
+
laboristo start <worker> [-d|--daemonize]
|
17
|
+
laboristo stop <worker>
|
18
|
+
EOS
|
19
|
+
|
20
|
+
require 'clap'
|
21
|
+
require 'laboristo'
|
22
|
+
|
23
|
+
opts = {}
|
24
|
+
|
25
|
+
args = Clap.run ARGV,
|
26
|
+
'-d' || '--daemonize' => -> {
|
27
|
+
opts[:daemonize] = true
|
28
|
+
}
|
29
|
+
|
30
|
+
command = args.shift
|
31
|
+
worker = args.shift
|
32
|
+
worker_path = File.expand_path("workers/#{worker}")
|
33
|
+
pid_path = File.expand_path("tmp/#{worker}.pid")
|
34
|
+
|
35
|
+
$stdout.sync = true
|
36
|
+
|
37
|
+
|
38
|
+
if opts[:daemonize]
|
39
|
+
Process.daemon(true)
|
40
|
+
File.open(pid_path, File::RDWR | File::EXCL | File::CREAT, 0600) { |io| io.write(Process.pid) }
|
41
|
+
at_exit do
|
42
|
+
File.delete(pid_path) if File.exist?(pid_path)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
require worker_path
|
data/laboristo.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'laboristo'
|
3
|
+
s.version = '0.1.0'
|
4
|
+
s.summary = 'Simple messages and workers for AWS SQS'
|
5
|
+
s.authors = ['matiasow']
|
6
|
+
s.email = ['matiasow@gmail.com']
|
7
|
+
s.homepage = 'https://github.com/matiasow/laboristo'
|
8
|
+
s.license = 'MIT'
|
9
|
+
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.bindir = 'bin'
|
12
|
+
s.executables << 'laboristo'
|
13
|
+
|
14
|
+
s.add_dependency 'aws-sdk-core', '~> 2'
|
15
|
+
s.add_dependency 'clap', '~> 1'
|
16
|
+
|
17
|
+
s.add_development_dependency 'cutest', '~> 1.2'
|
18
|
+
end
|
data/lib/laboristo.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'aws-sdk-core'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Laboristo
|
5
|
+
class Queue
|
6
|
+
attr_accessor :name
|
7
|
+
attr_accessor :url
|
8
|
+
attr_accessor :sqs
|
9
|
+
|
10
|
+
def initialize(name)
|
11
|
+
@name = name
|
12
|
+
@sqs = Aws::SQS::Client.new
|
13
|
+
@url = "https://sqs.#{ENV['AWS_REGION']}.amazonaws.com/#{ENV['AWS_ACCOUNT_ID']}/#{@name}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def push(message)
|
17
|
+
encoded = Base64.encode64(message.to_s)
|
18
|
+
@sqs.send_message(queue_url: @url, message_body: encoded)
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(&block)
|
22
|
+
loop do
|
23
|
+
resp = @sqs.receive_message(queue_url: @url,
|
24
|
+
attribute_names: ['All'],
|
25
|
+
max_number_of_messages: 10).data.to_hash
|
26
|
+
|
27
|
+
resp[:messages] && resp[:messages].each do |msg|
|
28
|
+
begin
|
29
|
+
block.call(Base64.decode64 msg[:body])
|
30
|
+
@sqs.delete_message(queue_url: @url,
|
31
|
+
receipt_handle: msg[:receipt_handle])
|
32
|
+
rescue StandardError => e
|
33
|
+
$stderr.puts "ERROR: Can't process message #{msg[:message_id]}.\n#{e}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
alias << push
|
40
|
+
alias pop each
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.[](queue)
|
44
|
+
Queue.new(queue)
|
45
|
+
end
|
46
|
+
end
|
data/makefile
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'laboristo'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
if ENV['TRAVIS']
|
5
|
+
require 'codeclimate-test-reporter'
|
6
|
+
CodeClimate::TestReporter.start
|
7
|
+
end
|
8
|
+
|
9
|
+
def send_message(message)
|
10
|
+
@client.send_message(queue_url: @url, message_body: Base64.encode64(message))
|
11
|
+
end
|
12
|
+
|
13
|
+
def fetch_last_message
|
14
|
+
fetched = @client.receive_message(queue_url: @url,
|
15
|
+
max_number_of_messages: 1,
|
16
|
+
wait_time_seconds: 0).data.to_hash
|
17
|
+
|
18
|
+
fetched[:messages].first
|
19
|
+
end
|
20
|
+
|
21
|
+
def message_body(msg)
|
22
|
+
Base64.decode64(msg[:body])
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete_message(msg)
|
26
|
+
@client.delete_message(queue_url: @url,
|
27
|
+
receipt_handle: msg[:receipt_handle])
|
28
|
+
end
|
29
|
+
|
30
|
+
test 'environment' do
|
31
|
+
assert ENV['AWS_REGION']
|
32
|
+
assert ENV['AWS_ACCESS_KEY_ID']
|
33
|
+
assert ENV['AWS_SECRET_ACCESS_KEY']
|
34
|
+
assert ENV['AWS_ACCOUNT_ID']
|
35
|
+
end
|
36
|
+
|
37
|
+
@account = ENV['AWS_ACCOUNT_ID']
|
38
|
+
@region = ENV['AWS_REGION']
|
39
|
+
@client = Aws::SQS::Client.new
|
40
|
+
|
41
|
+
@queue_name = SecureRandom.uuid
|
42
|
+
@url = @client.create_queue(queue_name: @queue_name).data.to_hash[:queue_url]
|
data/test/queue_test.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
test 'initialize' do
|
4
|
+
q = Laboristo::Queue.new(@queue_name)
|
5
|
+
assert q.name == @queue_name
|
6
|
+
assert q.sqs.is_a? Aws::SQS::Client
|
7
|
+
assert q.url == @url
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'initialize through []' do
|
11
|
+
q = Laboristo[@queue_name]
|
12
|
+
assert q.name == @queue_name
|
13
|
+
assert q.sqs.is_a? Aws::SQS::Client
|
14
|
+
assert q.url == @url
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'push message to queue' do
|
18
|
+
q = Laboristo[@queue_name]
|
19
|
+
msg = SecureRandom.uuid
|
20
|
+
|
21
|
+
q.push(msg)
|
22
|
+
|
23
|
+
fetched = fetch_last_message
|
24
|
+
delete_message(fetched)
|
25
|
+
|
26
|
+
assert msg == message_body(fetched)
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'push message to queue using <<' do
|
30
|
+
q = Laboristo[@queue_name]
|
31
|
+
msg = SecureRandom.uuid
|
32
|
+
|
33
|
+
q << msg
|
34
|
+
|
35
|
+
fetched = fetch_last_message
|
36
|
+
delete_message(fetched)
|
37
|
+
|
38
|
+
assert msg == message_body(fetched)
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'fetch messages using pop' do
|
42
|
+
q = Laboristo[@queue_name]
|
43
|
+
msg = SecureRandom.uuid
|
44
|
+
|
45
|
+
send_message(msg)
|
46
|
+
|
47
|
+
q.pop do |m|
|
48
|
+
assert m == msg
|
49
|
+
break # only fetch one message
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
test 'fetch messages using each' do
|
54
|
+
q = Laboristo[@queue_name]
|
55
|
+
msg = SecureRandom.uuid
|
56
|
+
|
57
|
+
send_message(msg)
|
58
|
+
|
59
|
+
q.each do |m|
|
60
|
+
assert m == msg
|
61
|
+
break # only fetch one message
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# And finally destroy the queue...
|
66
|
+
@client.delete_queue(queue_url: @url)
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: laboristo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- matiasow
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: clap
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: cutest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.2'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- matiasow@gmail.com
|
58
|
+
executables:
|
59
|
+
- laboristo
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gems"
|
64
|
+
- ".gitignore"
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- bin/laboristo
|
68
|
+
- laboristo.gemspec
|
69
|
+
- lib/laboristo.rb
|
70
|
+
- makefile
|
71
|
+
- test/helper.rb
|
72
|
+
- test/queue_test.rb
|
73
|
+
homepage: https://github.com/matiasow/laboristo
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.4
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Simple messages and workers for AWS SQS
|
97
|
+
test_files: []
|