-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathtaskqueue.rb
More file actions
executable file
·293 lines (248 loc) · 10.4 KB
/
Copy pathtaskqueue.rb
File metadata and controls
executable file
·293 lines (248 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/ruby -w
# First-party Ruby libraries
require 'posixpsutil'
require 'socket'
require 'timeout'
# Imports for AppController libraries
$:.unshift File.join(File.dirname(__FILE__))
require 'node_info'
require 'helperfunctions'
require 'service_helper'
# To implement support for the Google App Engine Task Queue API, we use
# the open source rabbitmq server and celery. This lets users dispatch background
# tasks, whose data are stored as items in rabbitmq. This module provides
# methods that automatically configure and deploy rabbitmq and celery as needed.
module TaskQueue
# Indicates an error when determining the version of rabbitmq.
class UnknownVersion < StandardError; end
# The default name of the service.
NAME = 'TaskQueue'.freeze
# The port that the RabbitMQ server runs on, by default.
SERVER_PORT = 5672
# The starting port for TaskQueue server processes.
STARTING_PORT = 17447
# HAProxy port for TaskQueue servers.
HAPROXY_PORT = 17446
# Default REST API public port.
TASKQUEUE_SERVER_SSL_PORT = 8199
# The path to the file that the shared secret should be written to.
COOKIE_FILE = '/var/lib/rabbitmq/.erlang.cookie'.freeze
# Name for flower service as per helper.
SERVICE_NAME_FLOWER = 'appscale-flower'.freeze
# Name for rabbit mq service as per helper.
SERVICE_NAME_RABBITMQ = 'appscale-rabbitmq-server.target'.freeze
# Name for task queue service template as per helper.
SERVICE_NAME_TASKQUEUE = 'appscale-taskqueue@'.freeze
# The location of taskqueue venv pip
TASKQUEUE_PIP = '/opt/appscale_venvs/appscale_taskqueue/bin/pip'.chomp
# Where to find the rabbitmqctl command.
RABBITMQCTL = `which rabbitmqctl`.chomp
# The longest we'll wait for RabbitMQ to come up in seconds.
MAX_WAIT_FOR_RABBITMQ = 60
# Location where celery workers back up state to.
CELERY_STATE_DIR = '/opt/appscale/celery'.freeze
# Optional features that can be installed for the taskqueue package.
OPTIONAL_FEATURES = ['celery_gui']
# TaskQueue server processes per core.
MULTIPLIER = 2
# If we fail to get the number of processors we set our default number of
# taskqueue servers to this value.
DEFAULT_NUM_SERVERS = 3
# Starts rabbitmq server.
def self.start_rabbitmq
if RABBITMQCTL.empty?
msg = "Couldn't find rabbitmqctl! Not starting rabbitmq server."
Djinn.log_error(msg)
raise AppScaleException.new(msg)
end
Ejabberd.ensure_correct_epmd
ServiceHelper.start(SERVICE_NAME_RABBITMQ)
end
# Starts a service that we refer to as a "taskqueue_master", a RabbitMQ
# service that other nodes can rely on to be running the taskqueue server.
#
# Args:
# clear_data: A boolean that indicates whether or not RabbitMQ state should
# be erased before starting RabbitMQ.
def self.start_master(clear_data, verbose)
Djinn.log_info('Starting TaskQueue Master')
write_cookie
if clear_data
Djinn.log_debug('Erasing RabbitMQ state')
erase_local_files
else
Djinn.log_debug('Not erasing RabbitMQ state')
end
# First, start up RabbitMQ and make sure the service is up.
start_rabbitmq
HelperFunctions.sleep_until_port_is_open('localhost',
SERVER_PORT)
# The master rabbitmq will set the policy for replication of messages
# and queues.
policy = '{"ha-mode":"all", "ha-sync-mode": "automatic"}'
Djinn.log_run("#{RABBITMQCTL} set_policy ha-all '' '#{policy}'")
# Next, start up the TaskQueue Server.
start_taskqueue_server(verbose)
HelperFunctions.sleep_until_port_is_open('localhost',
STARTING_PORT)
end
# Starts a service that we refer to as a "rabbitmq slave". Since all nodes in
# RabbitMQ are equal, this name isn't exactly fair, so what this role means
# here is "start a RabbitMQ server and connect it to the server on the machine
# playing the 'rabbitmq_master' role." We also start taskqueue servers on
# all taskqueue nodes.
#
# Args:
# master_ip: A String naming the IP address or FQDN where RabbitMQ is
# already running.
# clear_data: A boolean that indicates whether or not RabbitMQ state should
# be erased before starting up RabbitMQ.
def self.start_slave(master_ip, clear_data, verbose)
Djinn.log_info('Starting TaskQueue Slave')
write_cookie
if clear_data
Djinn.log_debug('Erasing RabbitMQ state')
erase_local_files
else
Djinn.log_debug('Not erasing RabbitMQ state')
end
# Start the local RabbitMQ server, and then wait for RabbitMQ on the master
# node to come up. The RabbitMQ server on the taskqueue master may not be
# able to start without this one if it previously stopped first.
Djinn.log_run("mkdir -p #{CELERY_STATE_DIR}")
start_rabbitmq
Djinn.log_debug('Waiting for RabbitMQ on master node to come up')
HelperFunctions.sleep_until_port_is_open(master_ip, SERVER_PORT)
# Look up the TaskQueue master's hostname (not the fqdn). To resolve
# it we use Addrinfo.getnameinfo since Resolv will not use /etc/hosts
# and this could cause issues on private clusters.
master_tq_host = nil
begin
master_tq_host = Addrinfo.ip(master_ip).getnameinfo[0]
if master_tq_host =~ /^[[:digit:]]/
Djinn.log_warn("#{master_ip} didn't resolve to a hostname! Expect" \
" problems with rabbitmq clustering.")
else
master_tq_host = master_tq_host.split('.')[0]
end
rescue SocketError
Djinn.log_error("Cannot resolv #{master_ip}!")
return false
end
Djinn.log_info("Using #{master_tq_host} as master taskqueue hostname.")
# Now we try to cluster with the master node.
HelperFunctions::RETRIES.downto(0) { |tries_left|
Djinn.log_debug('Waiting for RabbitMQ on local node to come up')
begin
Timeout.timeout(MAX_WAIT_FOR_RABBITMQ) do
HelperFunctions.sleep_until_port_is_open('localhost', SERVER_PORT)
Djinn.log_debug('Done starting rabbitmq_slave on this node')
Djinn.log_run("#{RABBITMQCTL} stop_app")
Djinn.log_run("#{RABBITMQCTL} join_cluster rabbit@#{master_tq_host}")
Djinn.log_run("#{RABBITMQCTL} start_app")
Djinn.log_debug('Starting TaskQueue servers on slave node')
start_taskqueue_server(verbose)
Djinn.log_debug('Waiting for TaskQueue servers on slave node to' \
' come up')
HelperFunctions.sleep_until_port_is_open('localhost', STARTING_PORT)
Djinn.log_debug('Done waiting for TaskQueue servers')
return true
end
rescue Timeout::Error
tries_left -= 1
Djinn.log_warn('Waited for RabbitMQ to start, but timed out. ' \
"Retries left #{tries_left}.")
Djinn.log_run("ps ax | grep rabbit | grep -v grep | awk '{print $1}' | xargs kill -9")
erase_local_files if clear_data
end
HelperFunctions.log_and_crash('RabbitMQ slave failed to come up') if tries_left.zero?
}
end
# Starts the AppScale TaskQueue server.
def self.start_taskqueue_server(verbose)
service_env = {}
service_env[:APPSCALE_OPTION_VERBOSE] = '--verbose' if verbose
ServiceHelper.write_environment('appscale-taskqueue', service_env)
Djinn.log_debug('Starting taskqueue servers on this node')
ports = get_server_ports
ServiceHelper.start(SERVICE_NAME_TASKQUEUE, ports)
Djinn.log_debug('Done starting taskqueue servers on this node')
end
# Stops the RabbitMQ, celery workers, and taskqueue server on this node.
def self.stop
Djinn.log_debug('Shutting down celery workers')
stop_script = \
"import celery\n" \
"celery = celery.Celery()\n" \
"with celery.control.app.connection_or_acquire(None) as conn:\n" \
" conn.ensure_connection(max_retries=2)\n" \
" celery.control.broadcast('shutdown', connection=conn)"
stop_cmd = %Q(/usr/bin/python2 -c "#{stop_script}")
Djinn.log_run(stop_cmd)
stop_rabbitmq
stop_taskqueue_server
end
def self.stop_rabbitmq
Djinn.log_debug('Shutting down RabbitMQ')
ServiceHelper.stop(SERVICE_NAME_RABBITMQ)
end
# Stops the AppScale TaskQueue server.
def self.stop_taskqueue_server
Djinn.log_debug('Stopping taskqueue servers on this node')
ServiceHelper.stop(SERVICE_NAME_TASKQUEUE)
Djinn.log_debug('Done stopping taskqueue servers on this node')
end
# Erlang processes use a secret value as a password to authenticate between
# one another. Since this is pretty much the same thing we do in AppScale
# with our secret key, use the same key here but hashed as to not reveal the
# actual key.
def self.write_cookie
HelperFunctions.write_file(COOKIE_FILE, HelperFunctions.get_taskqueue_secret)
end
# Erases all the files that RabbitMQ normally writes to, which can be useful
# to ensure that we start up RabbitMQ without left-over state from previous
# runs.
def self.erase_local_files
Djinn.log_run('rm -rf /var/log/rabbitmq/*')
Djinn.log_run('rm -rf /var/lib/rabbitmq/mnesia/*')
Djinn.log_run('rm -rf /etc/appscale/celery/')
Djinn.log_run("rm -rf #{CELERY_STATE_DIR}/*")
end
# Starts the Flower Server on this machine, which provides a web UI to celery
# and RabbitMQ. A link to Flower is given in the AppDashboard, for users to
# monitor their Task Queue tasks.
#
# Args:
# flower_password: A String that is used as the password to log into flower.
def self.start_flower(flower_password)
if flower_password.nil? || flower_password.empty?
Djinn.log_info("Flower password is empty: don't start flower.")
return
end
service_env = {
APPSCALE_FLOWER_OPTION_AUTH: "--basic_auth=appscale:#{flower_password}"
}
ServiceHelper.write_environment(SERVICE_NAME_FLOWER, service_env)
ServiceHelper.start(SERVICE_NAME_FLOWER)
end
# Stops the Flower Server on this machine.
def self.stop_flower
ServiceHelper.stop(SERVICE_NAME_FLOWER)
end
# Number of servers is based on the number of CPUs.
def self.number_of_servers
# If this is NaN then it returns 0
num_procs = `cat /proc/cpuinfo | grep processor | wc -l`.to_i
return DEFAULT_NUM_SERVERS if num_procs.zero?
(num_procs * MULTIPLIER)
end
# Returns a list of ports that should be used to host TaskQueue servers.
def self.get_server_ports
num_servers = number_of_servers
server_ports = []
num_servers.times { |i|
server_ports << STARTING_PORT + i
}
server_ports
end
end