-
Notifications
You must be signed in to change notification settings - Fork 9
/
launcher.py
314 lines (281 loc) · 12.4 KB
/
launcher.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Copyright 2015-2024 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from cartola import sysexits
import firenado.conf
from importlib import reload
import logging
import os
import sys
logger = logging.getLogger(__name__)
class FirenadoLauncher:
def __init__(self, **settings):
self.app = settings.get("app", None)
self.path = settings.get("path", None)
if self.path is not None:
sys.path.append(self.path)
if self.app:
os.environ["CURRENT_APP"] = self.app
if os.environ.get("CURRENT_APP"):
self.app = os.environ.get("CURRENT_APP")
self.addresses = settings.get("addresses", None)
self.dir = settings.get("dir", None)
self.port = settings.get("port", None)
self.socket = settings.get("socket", None)
# Fixing and cleaning PYTHONPATH and sys.path
# This is useful so we can run a process launcher with the same
# PYTHONPATH from the parent process
real_pythonpaths = sys.path[:]
if "PYTHONPATH" in os.environ and os.environ['PYTHONPATH'] is not None:
current_pythonpaths = os.environ['PYTHONPATH'].split(":")
for path in current_pythonpaths:
if path.strip() != "" and path.strip() not in real_pythonpaths:
real_pythonpaths.append(path.strip())
for path in sys.path:
if path.strip() != "" and path.strip() not in real_pythonpaths:
real_pythonpaths.append(path.strip())
sys.path = real_pythonpaths
os.environ['PYTHONPATH'] = ":".join(real_pythonpaths)
if self.dir is not None:
os.chdir(self.dir)
if self.app is not None or self.dir is not None:
reload(firenado.conf)
self.configure_logging()
def configure_logging(self, **kargs):
format = kargs.get("format", firenado.conf.log['format'])
level = kargs.get("level", firenado.conf.log['level'])
# Set logging basic configurations
for handler in logging.root.handlers[:]:
# clearing loggers, solution from: https://bit.ly/2yTchyx
logging.root.removeHandler(handler)
logging.basicConfig(level=level, format=format)
def load(self):
raise NotImplementedError()
def launch(self):
raise NotImplementedError()
def shutdown(self):
raise NotImplementedError()
class ProcessLauncher(FirenadoLauncher):
try:
import pexpect
process: pexpect.spawn = None
except ImportError:
logger.debug("The pexpect module ins't isn't installed. Consider "
"installing it in order to launch applications using "
"ProcessLauncher.")
def __init__(self, **settings):
super().__init__(**settings)
self.process = None
self.process_callback = None
self.process_callback_time = 100
self.logfile = settings.get("logfile", None)
self.command = None
self.response = None
def load(self):
firenado_script = os.path.join(firenado.conf.ROOT, "bin",
"firenado-cli.py")
self.command = f"{sys.executable} {firenado_script} app run"
if self.dir is not None:
self.command = f"{self.command} --dir={self.dir}"
if self.socket is None and self.addresses is not None:
self.command = f"{self.command} --addresses={self.addresses}"
if self.socket is None and self.port is not None:
self.command = f"{self.command} --port={self.port}"
if self.socket is not None:
self.command = f"{self.command} --socke={self.socket}"
async def read_process(self):
import pexpect
self.process_callback.stop()
try:
# Simple way to catch everything is wait for a new line:
#
await self.process.expect("\n", async_=True)
except pexpect.TIMEOUT:
logger.debug("Reached timeout, getting back in the loop.")
self.process_callback.start()
async def launch(self):
import pexpect
import tornado.ioloop
logger.info("Launching %s", self.command)
parameters = {
'command': self.command,
'encoding': "utf-8"
}
if self.dir:
parameters['cwd'] = self.dir
if self.logfile is not None:
parameters['logfile'] = self.logfile
self.process = pexpect.spawn(**parameters)
await self.process.expect(
[r"[Firenado server started successfully].*"], async_=True)
self.process_callback = tornado.ioloop.PeriodicCallback(
self.read_process,
self.process_callback_time
)
self.process_callback.start()
def send(self, line):
logger.info("Sending line %s", line)
self.process.sendline(line)
def shutdown(self):
logger.warning("Shutting down process launcher.")
self.process.terminate(force=True)
def is_alive(self):
return self.process.isalive()
class TornadoLauncher(FirenadoLauncher):
def __init__(self, **settings):
super().__init__(**settings)
self.http_server = None
self.application = None
self.MAX_WAIT_SECONDS_BEFORE_SHUTDOWN = firenado.conf.app[
'wait_before_shutdown']
self.addresses = firenado.conf.app['default_addresses']
if ((self.addresses is None or self.addresses == ['']) and
firenado.conf.app['addresses']):
self.addresses = firenado.conf.app['addresses']
if self.port is None:
self.port = firenado.conf.app['port']
def load(self):
from .tornadoweb import TornadoApplication
# TODO: Resolve module if doesn't exists
if firenado.conf.app['pythonpath']:
sys.path.append(firenado.conf.app['pythonpath'])
self.application = TornadoApplication(**firenado.conf.app['settings'])
def launch(self):
import signal
from tornado.ioloop import IOLoop
import tornado.httpserver
signal.signal(signal.SIGTERM, self.sig_handler)
signal.signal(signal.SIGINT, self.sig_handler)
if os.name == "posix":
signal.signal(signal.SIGTSTP, self.sig_handler)
self.http_server = tornado.httpserver.HTTPServer(self.application)
if firenado.conf.app['xheaders'] is not None and isinstance(
firenado.conf.app['xheaders'], bool):
logger.debug("Setting http server xheaders as %s.",
firenado.conf.app['xheaders'])
self.http_server.xheaders = firenado.conf.app['xheaders']
if firenado.conf.app['xheaders'] is not None and isinstance(
firenado.conf.app['xheaders'], bool):
logger.warning("The xheaders defined in the application section"
"must be bool instead of %s. Ignoring the "
"configuration item.",
type(firenado.conf.app['xheaders']).__name__)
listening_count = 0
listening_what = "socket"
if firenado.conf.app['socket'] or self.socket:
from tornado.netutil import bind_unix_socket
socket_path = firenado.conf.app['socket']
if self.socket:
socket_path = self.socket
socket = bind_unix_socket(socket_path)
self.http_server.add_socket(socket)
logger.info("Firenado listening at socket %s",
socket.getsockname())
listening_count += 1
else:
listening_what = "addresses:port"
if len(self.addresses):
for address in self.addresses:
if self.add_sockets(self.port, address):
listening_count += 1
else:
if self.add_sockets(self.port):
listening_count += 1
if listening_count:
if listening_count > 1:
listening_what = f"{listening_what}s"
logger.info("Firenado server started successfully. Listening at %s"
" %s.", listening_count, listening_what)
if firenado.conf.app['process']['num_processes'] is not None:
from tornado.process import fork_processes
num_processes = firenado.conf.app['process']['num_processes']
max_restarts = firenado.conf.app['process']['max_restarts']
num_processes_alert = num_processes
if num_processes == 0:
num_processes_alert = (
f"0 (assuming {tornado.process.cpu_count()} as cpu "
"count)")
logger.info("Tornado set to start %s processes with %s max "
"restarts.", num_processes_alert, max_restarts)
fork_processes(num_processes, max_restarts)
IOLoop.current().start()
else:
logger.critical("Firenado unable to start.")
sysexits.exit_fatal(sysexits.EX_SOFTWARE)
def sig_handler(self, sig, _):
""" Handle the signal sent to the process
:param sig: Signal set to the process
:param _: Frame is not being used
"""
from tornado.ioloop import IOLoop
from tornado.process import task_id
tid = task_id()
pid = os.getpid()
if tid is None:
logger.warning("main process (pid %s) caught signal: %s", pid, sig)
else:
logger.warning("child %s (pid %s) caught signal: %s", tid, pid,
sig)
IOLoop.current().add_callback_from_signal(self.shutdown)
def add_sockets(self, port: int, address: str = None):
from socket import gaierror
from tornado.netutil import bind_sockets
try:
if address is None:
address = "127.0.0.1"
sockets = bind_sockets(port, address.strip())
self.http_server.add_sockets(sockets)
logger.info("Firenado listening at http://%s:%s.", address.strip(),
port)
return True
except gaierror:
logger.error("Firenado failed to listen at http://%s:%s.",
address.strip(), port)
except OSError:
logger.error("Firenado failed to listen at http://%s:%s. Check if "
"another process is listening at this port or if you "
"provided a dns name and the correspondent ip in the "
"addresses configuration or if the machine owns the "
"ip Firenado will start to listen.", address, port)
return False
def shutdown(self):
from tornado.ioloop import IOLoop
from tornado.process import task_id
import time
def log_message(message: str, pid: int, tid: int = None):
if tid is None:
logger.info("main process (pid %s): %s", pid, message)
return
logger.info("child %s (pid %s): %s", tid, pid, message)
tid = task_id()
pid = os.getpid()
log_message("stopping http server", pid, tid)
for key, component in self.application.components.items():
component.shutdown()
self.http_server.stop()
io_loop: IOLoop = IOLoop.current()
if self.MAX_WAIT_SECONDS_BEFORE_SHUTDOWN == 0:
io_loop.stop()
log_message("application is down", pid, tid)
return
log_message(f"shutdown in {self.MAX_WAIT_SECONDS_BEFORE_SHUTDOWN}s"
" seconds", pid, tid)
deadline = time.time() + self.MAX_WAIT_SECONDS_BEFORE_SHUTDOWN
def stop_loop():
now = time.time()
if now < deadline:
io_loop.add_timeout(now + 1, stop_loop)
return
io_loop.stop()
log_message("application is down", pid, tid)
stop_loop()