opener-daemons 1.3.0 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,177 +0,0 @@
1
- require 'optparse'
2
-
3
- module Opener
4
- module Daemons
5
- class OptParser
6
- attr_accessor :option_parser, :options
7
-
8
- def initialize(&block)
9
- @options = {}
10
- @option_parser = construct_option_parser(options, &block)
11
- end
12
-
13
- def parse(args)
14
- process(:parse, args)
15
- end
16
-
17
- def parse!(args)
18
- process(:parse!, args)
19
- end
20
-
21
- def pre_parse!(args)
22
- delete_double_dash = false
23
- process(:parse!, args, delete_double_dash)
24
- end
25
-
26
- def pre_parse(args)
27
- delete_double_dash = false
28
- process(:parse, args, delete_double_dash)
29
- end
30
-
31
- def self.parse(args)
32
- new.parse(args)
33
- end
34
-
35
- def self.parse!(args)
36
- new.parse!(args)
37
- end
38
-
39
- def self.pre_parse!(args)
40
- new.pre_parse!(args)
41
- end
42
-
43
- def self.pre_parse(args)
44
- new.pre_parse(args)
45
- end
46
-
47
- private
48
-
49
- def process(call, args, delete_double_dash=true)
50
- args.delete("--") if delete_double_dash
51
- option_parser.send(call, args)
52
- return options
53
- end
54
-
55
- def construct_option_parser(options, &block)
56
- script_name = File.basename($0, ".rb")
57
- unless script_name.include?("-daemon")
58
- script_name = "#{script_name}-daemon"
59
- end
60
-
61
- OptionParser.new do |opts|
62
- if block_given?
63
- opts.banner = "Usage: #{script_name} <start|stop|restart> [daemon_options] -- [component_options]"
64
- else
65
- opts.banner = "Usage: #{script_name} <start|stop|restart> [options]"
66
- end
67
-
68
- opts.separator ""
69
- opts.separator "When calling #{script_name} without <start|stop|restart> the daemon will start as a foreground process"
70
- opts.separator ""
71
-
72
- opts.separator "Environment Variables:"
73
- opts.separator ""
74
-
75
- opts.separator "These daemons make use of Amazon SQS queues and other Amazon services."
76
- opts.separator "The access to these services and other environment variables can be configured"
77
- opts.separator "using a .opener-daemons-env file in the home directory of the current user."
78
- opts.separator ""
79
- opts.separator "It is also possible to provide the environment variables directly to the deamon."
80
- opts.separator ""
81
- opts.separator "For example:"
82
- opts.separator "AWS_REGION='eu-west-1' #{script_name} start [other options]"
83
- opts.separator ""
84
- opts.separator "We advise to have the following environment variables available: "
85
- opts.separator "* AWS_ACCESS_KEY_ID"
86
- opts.separator "* AWS_SECRET_ACCESS_KEY"
87
- opts.separator "* AWS_REGION"
88
- opts.separator ""
89
-
90
- if block_given?
91
- opts.separator "Component Specific options:"
92
- opts.separator ""
93
- yield opts, options
94
- opts.separator ""
95
- end
96
-
97
- opts.separator "Daemon options:"
98
-
99
- opts.on("-i", "--input QUEUE_NAME", "Input queue name") do |v|
100
- options[:input_queue] = v
101
- end
102
-
103
- opts.on("-o", "--output QUEUE_NAME", "Output queue name") do |v|
104
- options[:output_queue] = v
105
- end
106
-
107
- opts.on("--batch-size COUNT", Integer, "Request x messages at once where x is between 1 and 10") do |v|
108
- options[:batch_size] = v
109
- end
110
-
111
- opts.on("--buffer-size COUNT", Integer, "Size of input and output buffer. Defaults to 4 * batch-size") do |v|
112
- options[:buffer_size] = v
113
- end
114
-
115
- opts.on("--sleep-interval SECONDS", Integer, "The interval to sleep when the queue is empty (seconds)") do |v|
116
- options[:sleep_interval] = v
117
- end
118
-
119
- opts.on("-r", "--readers COUNT", Integer, "number of reader threads") do |v|
120
- options[:readers] = v
121
- end
122
-
123
- opts.on("-w", "--workers COUNT", Integer, "number of worker thread") do |v|
124
- options[:workers] = v
125
- end
126
-
127
- opts.on("-p", "--writers COUNT", Integer, "number of writer / pusher threads") do |v|
128
- options[:writers] = v
129
- end
130
-
131
- opts.on("-l", "--logfile FILENAME", "--log FILENAME", "Filename and path of logfile. Defaults to STDOUT") do |v|
132
- options[:log] = v
133
- end
134
-
135
- opts.on("-P", "--pidfile FILENAME", "--pid FILENAME", "Filename and path of pidfile. Defaults to /var/run/#{script_name}.pid") do |v|
136
- options[:pid] = v
137
- end
138
-
139
- opts.on("--pidpath DIRNAME", "Directory where to put the PID file. Is Overwritten by --pid if that option is present") do |v|
140
- options[:pidpath] = v
141
- end
142
-
143
- opts.on("--debug", "Turn on debug log level") do |v|
144
- options[:debug] = true
145
- end
146
-
147
- opts.on("--relentless", "Be relentless, fail fast, fail hard, do not continue processing when encountering component errors") do |v|
148
- options[:relentless] = true
149
- end
150
-
151
- opts.on("--bucket-name BUCKET_NAME", "The name of the bucket where the output should be stored.") do |v|
152
- options[:bucket_name] = v
153
- end
154
-
155
- opts.on("--bucket-dir BUCKET_DIR", "The directory that the output should be stored in. Requires --bucket-name option.") do |v|
156
- options[:bucket_dir] = v
157
- end
158
-
159
- opts.on("--file-suffix FILE_SUFFIX", "The suffix of the filename. Requires --bucket-name option.") do |v|
160
- options[:bucket_name] = v
161
- end
162
-
163
- opts.separator ""
164
-
165
- opts.separator "Common options:"
166
-
167
- # No argument, shows at tail. This will print an options summary.
168
- # Try it and see!
169
- opts.on_tail("-h", "--help", "Show this message. Usage: #{script_name} -h") do
170
- puts opts
171
- exit
172
- end
173
- end
174
- end
175
- end
176
- end
177
- end
@@ -1,34 +0,0 @@
1
- require 'aws-sdk'
2
-
3
- module Opener
4
- module Daemons
5
- class S3
6
- attr_reader :s3_client, :bucket_name, :content, :filename, :directory, :url
7
-
8
- def initialize(bucket_name, content, filename, directory = nil, filename_suffix = nil)
9
- @s3_client = AWS::S3.new
10
- @bucket_name = bucket_name
11
- @filename = [filename, filename_suffix].compact.reject{|e| e.empty?}.join("-")
12
- @content = content
13
- @directory = directory
14
- end
15
-
16
- def upload
17
- @filename = File.join(directory, filename) if directory
18
- bucket = s3_client.buckets[bucket_name]
19
- object = bucket.objects["#{filename}.kaf"]
20
-
21
- object.write(content)
22
-
23
- @url = object.url_for(
24
- :read,
25
- :secure => false,
26
- :force_path_style => false,
27
- :response_content_type => "application/xml",
28
- :response_content_disposition => "attachment",
29
- :expires => 7 * 24 * 60 * 60 # 7 days
30
- )
31
- end
32
- end
33
- end
34
- end
@@ -1,54 +0,0 @@
1
- require 'aws-sdk-core'
2
-
3
- module Opener
4
- module Daemons
5
- class SQS
6
- attr_reader :sqs, :name, :url
7
-
8
- def self.find(name)
9
- new(name)
10
- end
11
-
12
- def initialize(name)
13
- @sqs = Aws::SQS::Client.new
14
- @name = name
15
- begin
16
- @url = sqs.get_queue_url(:queue_name=>name)[:queue_url]
17
- rescue Aws::SQS::Errors::NonExistentQueue => e
18
- sqs.create_queue(:queue_name=>name)
19
- retry
20
- end
21
- end
22
-
23
- def send_message(message)
24
- sqs.send_message(:queue_url=>url, :message_body=>message)
25
- end
26
-
27
- def delete_message(handle)
28
- sqs.delete_message(:queue_url=>url, :receipt_handle=>handle)
29
- end
30
-
31
- def receive_messages(limit)
32
- result = sqs.receive_message(:queue_url=>url,
33
- :max_number_of_messages=>limit)[:messages] rescue []
34
-
35
- result ? to_hash(result) : []
36
-
37
- end
38
-
39
- def to_hash(messages)
40
- messages.map do |m|
41
- hash = m.to_hash
42
- json_body = JSON.parse(hash.delete(:body))
43
- hash[:body] = json_body["body"] ? json_body["body"] : json_body
44
- hash
45
- end
46
- end
47
-
48
- def queue_url
49
- url
50
- end
51
-
52
- end
53
- end
54
- end