mikezter-workling 0.4.9.7 → 0.4.9.8
Sign up to get free protection for your applications and to get access to all the features.
@@ -8,8 +8,7 @@ module Workling
|
|
8
8
|
|
9
9
|
def self.installed?
|
10
10
|
begin
|
11
|
-
gem 'rudeq'
|
12
|
-
require 'rudeq'
|
11
|
+
gem 'matthewrudy-rudeq', :lib => 'rude_q'
|
13
12
|
rescue LoadError
|
14
13
|
end
|
15
14
|
|
@@ -18,8 +17,7 @@ module Workling
|
|
18
17
|
|
19
18
|
def self.load
|
20
19
|
begin
|
21
|
-
gem 'rudeq'
|
22
|
-
require 'rudeq'
|
20
|
+
gem 'matthewrudy-rudeq', :lib => 'rude_q'
|
23
21
|
rescue Gem::LoadError
|
24
22
|
Workling::Base.logger.info "WORKLING: couldn't find rudeq library. Install: \"gem install matthewrudy-rudeq\". "
|
25
23
|
end
|
@@ -33,7 +31,7 @@ module Workling
|
|
33
31
|
def close
|
34
32
|
end
|
35
33
|
|
36
|
-
# implements the client job request and retrieval
|
34
|
+
# implements the client job request and retrieval
|
37
35
|
def request(key, value)
|
38
36
|
RudeQueue.set(key, value)
|
39
37
|
end
|
@@ -45,3 +43,4 @@ module Workling
|
|
45
43
|
end
|
46
44
|
end
|
47
45
|
end
|
46
|
+
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'right_aws'
|
3
|
-
|
4
1
|
#
|
5
2
|
# An SQS client
|
6
3
|
#
|
@@ -23,11 +20,31 @@ require 'right_aws'
|
|
23
20
|
#
|
24
21
|
# # The SQS visibility timeout for retrieved messages. Defaults to 30 seconds.
|
25
22
|
# visibility_timeout: 15
|
26
|
-
#
|
23
|
+
#
|
27
24
|
module Workling
|
28
25
|
module Clients
|
29
26
|
class SqsClient < Workling::Clients::BrokerBase
|
30
27
|
|
28
|
+
def self.installed?
|
29
|
+
begin
|
30
|
+
require 'json'
|
31
|
+
require 'right_aws'
|
32
|
+
rescue LoadError
|
33
|
+
end
|
34
|
+
|
35
|
+
Object.const_defined? "RightAws"
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.load
|
39
|
+
begin
|
40
|
+
gem 'right_aws'
|
41
|
+
require 'json'
|
42
|
+
require 'right_aws'
|
43
|
+
rescue Gem::LoadError
|
44
|
+
Workling::Base.logger.info "WORKLING: couldn't find memcache-client. Install: \"gem install memcache-client\". "
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
31
48
|
unless defined?(AWS_MAX_QUEUE_NAME)
|
32
49
|
AWS_MAX_QUEUE_NAME = 80
|
33
50
|
|
@@ -37,13 +54,13 @@ module Workling
|
|
37
54
|
DEFAULT_VISIBILITY_TIMEOUT = 30
|
38
55
|
DEFAULT_VISIBILITY_RESERVE = 10
|
39
56
|
end
|
40
|
-
|
57
|
+
|
41
58
|
# Mainly exposed for testing purposes
|
42
59
|
attr_reader :sqs_options
|
43
60
|
attr_reader :messages_per_req
|
44
61
|
attr_reader :visibility_timeout
|
45
|
-
|
46
|
-
# Starts the client.
|
62
|
+
|
63
|
+
# Starts the client.
|
47
64
|
def connect
|
48
65
|
@sqs_options = Workling.config[:sqs_options]
|
49
66
|
|
@@ -52,12 +69,12 @@ module Workling
|
|
52
69
|
@sqs_options.include?('aws_secret_access_key'))
|
53
70
|
raise WorklingError, 'Unable to start SqsClient due to missing SQS options'
|
54
71
|
end
|
55
|
-
|
72
|
+
|
56
73
|
# Optional settings
|
57
74
|
@messages_per_req = @sqs_options['messages_per_req'] || DEFAULT_MESSAGES_PER_REQ
|
58
75
|
@visibility_timeout = @sqs_options['visibility_timeout'] || DEFAULT_VISIBILITY_TIMEOUT
|
59
76
|
@visibility_reserve = @sqs_options['visibility_reserve'] || DEFAULT_VISIBILITY_RESERVE
|
60
|
-
|
77
|
+
|
61
78
|
begin
|
62
79
|
@sqs = RightAws::SqsGen2.new(
|
63
80
|
@sqs_options['aws_access_key_id'],
|
@@ -67,13 +84,13 @@ module Workling
|
|
67
84
|
raise WorklingError, "Unable to connect to SQS. Error: #{e}"
|
68
85
|
end
|
69
86
|
end
|
70
|
-
|
87
|
+
|
71
88
|
# No need for explicit closing, since there is no persistent
|
72
89
|
# connection to SQS.
|
73
90
|
def close
|
74
91
|
true
|
75
92
|
end
|
76
|
-
|
93
|
+
|
77
94
|
# Retrieve work.
|
78
95
|
def retrieve(key)
|
79
96
|
begin
|
@@ -105,13 +122,13 @@ module Workling
|
|
105
122
|
# Need to wrap in HashWithIndifferentAccess, as JSON serialization
|
106
123
|
# loses symbol keys.
|
107
124
|
parsed_msg = HashWithIndifferentAccess.new(JSON.parse(msg.body))
|
108
|
-
|
125
|
+
|
109
126
|
# Delete the msg from SQS, so we don't re-retrieve it after the
|
110
127
|
# visibility timeout. Ideally we would defer deleting a msg until
|
111
128
|
# after Workling has successfully processed it, but it currently
|
112
129
|
# doesn't provide the necessary hooks for this.
|
113
130
|
msg.delete
|
114
|
-
|
131
|
+
|
115
132
|
parsed_msg
|
116
133
|
end
|
117
134
|
end
|
@@ -119,7 +136,7 @@ module Workling
|
|
119
136
|
rescue => e
|
120
137
|
logger.error "Error retrieving msg for key: #{key}; Error: #{e}\n#{e.backtrace.join("\n")}"
|
121
138
|
end
|
122
|
-
|
139
|
+
|
123
140
|
end
|
124
141
|
|
125
142
|
# Request work.
|
@@ -131,14 +148,14 @@ module Workling
|
|
131
148
|
raise WorklingError, "Error sending msg for key: #{key}, value: #{value.inspect}; Error: #{e}"
|
132
149
|
end
|
133
150
|
end
|
134
|
-
|
151
|
+
|
135
152
|
# Returns the queue that corresponds to the specified key. Creates the
|
136
153
|
# queue if it doesn't exist yet.
|
137
154
|
def queue_for_key(key)
|
138
155
|
# Use thread local for storing queues, for the same reason as for buffers
|
139
156
|
Thread.current["queue_#{key}"] ||= @sqs.queue(queue_name(key), true, @visibility_timeout)
|
140
157
|
end
|
141
|
-
|
158
|
+
|
142
159
|
# Returns the queue name for the specified key. The name consists of an
|
143
160
|
# optional prefix, followed by the environment and the key itself. Note
|
144
161
|
# that with a long worker class / method name, the name could exceed the
|
@@ -148,16 +165,17 @@ module Workling
|
|
148
165
|
def queue_name(key)
|
149
166
|
"#{@sqs_options['prefix'] || ''}#{env}_#{key}"[0, AWS_MAX_QUEUE_NAME]
|
150
167
|
end
|
151
|
-
|
168
|
+
|
152
169
|
private
|
153
|
-
|
170
|
+
|
154
171
|
def logger
|
155
172
|
Rails.logger
|
156
173
|
end
|
157
|
-
|
174
|
+
|
158
175
|
def env
|
159
176
|
Rails.env
|
160
177
|
end
|
161
178
|
end
|
162
179
|
end
|
163
|
-
end
|
180
|
+
end
|
181
|
+
|