-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathservice.py
199 lines (170 loc) · 5.6 KB
/
service.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
# Copyright 2018 Autodesk, Inc. All rights reserved.
#
# Use of this software is subject to the terms of the Autodesk license agreement
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
import os
import logging
import time
from daemonize import Daemonize
import argparse
import signal
DESCRIPTION = """
Run the PTR Jira web app as a Linux service.
This script can be used with a systemd setup and handles the usual start, stop,
restart and status actions.
The running process daemonizes itself and its pid is stored in a pid file.
"""
logger = logging.getLogger("service")
# Ensure basic logging is always enabled
logging.basicConfig(format="%(levelname)s:%(name)s:%(message)s")
logger.setLevel(logging.DEBUG)
def status(pid_file):
"""
Return the pid of the service if it is running.
:param str pid_file: Full path to the pid file used by the service.
:returns: The process pid as an int if it is running, `None` otherwise.
"""
if not os.path.exists(pid_file):
return None
pid = None
with open(pid_file, "r") as pf:
pid = pf.read().strip()
if not pid:
logger.error("Unable to retrieve pid from %s" % pid_file)
return None
if not pid.isdigit():
logger.error("Invalid pid %s read from %s" % (pid, pid_file))
return None
pid = int(pid)
try:
# Send 0 signal to check if the process is alive.
os.kill(pid, 0)
except OSError as e:
logger.debug("%s" % e, exc_info=True)
return None
return pid
def start(pid_file, port_number, listen_address, settings, log_file=None):
"""
Start the service.
:param str pid_file: Full path to the pid file used by the service.
:param int port_number: The port number for the web app to listen on.
:param int listen_address: The IPv4 address that the server binds to.
:param str settings: Full path to settings file for the web app.
:param str log_file: An optional log file to use for the daemon output. By
default the daemon uses a syslog handler.
"""
keep_fds = []
if log_file:
fh = logging.FileHandler(log_file, "a")
fh.setLevel(logging.INFO)
logger.addHandler(fh)
keep_fds = [fh.stream.fileno()]
# Inline function so we can pass a callable to Daemonize with our parameters
# set.
def start_wep_app():
import logging
logger = logging.getLogger("service").getChild("sg_jira")
logger.info("Starting wep app...")
try:
import webapp
webapp.run_server(
port=port_number, listen_address=listen_address, settings=settings
)
except Exception as e:
logger.exception(e)
logger.warning("bye")
logger.info("Starting daemon with pid file %s" % pid_file)
daemon = Daemonize(
app="sg_jira",
pid=pid_file,
action=start_wep_app,
keep_fds=keep_fds,
logger=logger if log_file else None,
)
daemon.start()
def stop(pid_file):
"""
Stop the service if it is running.
:param str pid_file: Full path to the pid file used by the service.
"""
# Get the running process pid, if any
pid = status(pid_file)
if not pid:
return
try:
os.kill(pid, signal.SIGTERM)
# Give the process some time to exit nicely
time.sleep(0.1)
# Send a SIGKILL signal but ignore errors
try:
os.kill(pid, signal.SIGKILL)
except OSError:
pass
except OSError as e:
# Catch the error in case the process exited between our check and our
# attempt to stop it.
logger.warning(
"Unable to stop process %d, assuming it is already stopped: %s" % (pid, e)
)
logger.debug(str(e), exc_info=True)
# Clean up
if os.path.exists(pid_file):
os.remove(pid_file)
def main():
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument(
"--pid_file",
default="/tmp/sg_jira.pid",
help="Full path to a file where to write the process pid.",
)
parser.add_argument(
"--log_file",
help="Full path to a file where to log output.",
)
parser.add_argument(
"--port",
type=int,
default=9090,
help="The port number to listen on.",
)
parser.add_argument(
"--listen_address",
default="127.0.0.1",
help="The IPv4 address that the server binds to. Use 0.0.0.0 to bind on all network interfaces.",
)
parser.add_argument("--settings", help="Full path to settings file.", required=True)
parser.add_argument(
"action",
choices=["start", "stop", "restart", "status"],
help="Action to perform.",
)
args = parser.parse_args()
if args.action == "start":
start(
args.pid_file,
args.port,
args.listen_address,
os.path.abspath(args.settings),
args.log_file,
)
elif args.action == "stop":
stop(args.pid_file)
elif args.action == "status":
pid = status(args.pid_file)
if pid:
logger.info("Service is running with pid %d" % pid)
else:
logger.info("Service is not running.")
elif args.action == "restart":
stop(args.pid_file)
start(
args.pid_file,
args.port,
args.listen_address,
os.path.abspath(args.settings),
args.log_file,
)
if __name__ == "__main__":
main()