Show More
@@ -1,113 +1,151 b'' | |||||
1 | """ |
|
1 | """ | |
2 | gunicorn config extension and hooks. Sets additional configuration that is |
|
2 | gunicorn config extension and hooks. Sets additional configuration that is | |
3 | available post the .ini config. |
|
3 | available post the .ini config. | |
4 |
|
4 | |||
5 | - workers = ${cpu_number} |
|
5 | - workers = ${cpu_number} | |
6 | - threads = 1 |
|
6 | - threads = 1 | |
7 | - proc_name = ${gunicorn_proc_name} |
|
7 | - proc_name = ${gunicorn_proc_name} | |
8 | - worker_class = sync |
|
8 | - worker_class = sync | |
9 | - worker_connections = 10 |
|
9 | - worker_connections = 10 | |
10 | - max_requests = 1000 |
|
10 | - max_requests = 1000 | |
11 | - max_requests_jitter = 30 |
|
11 | - max_requests_jitter = 30 | |
12 | - timeout = 21600 |
|
12 | - timeout = 21600 | |
13 |
|
13 | |||
14 | """ |
|
14 | """ | |
15 |
|
15 | |||
16 | import multiprocessing |
|
16 | import multiprocessing | |
17 | import sys |
|
17 | import sys | |
18 | import time |
|
18 | import time | |
19 | import datetime |
|
19 | import datetime | |
20 | import threading |
|
20 | import threading | |
21 | import traceback |
|
21 | import traceback | |
22 | from gunicorn.glogging import Logger |
|
22 | from gunicorn.glogging import Logger | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | # GLOBAL |
|
25 | # GLOBAL | |
26 | errorlog = '-' |
|
26 | errorlog = '-' | |
27 | accesslog = '-' |
|
27 | accesslog = '-' | |
28 | loglevel = 'debug' |
|
28 | loglevel = 'debug' | |
29 |
|
29 | |||
30 | # SECURITY |
|
30 | # SECURITY | |
|
31 | ||||
|
32 | # The maximum size of HTTP request line in bytes. | |||
31 | limit_request_line = 4094 |
|
33 | limit_request_line = 4094 | |
32 | limit_request_fields = 100 |
|
34 | ||
33 | limit_request_field_size = 8190 |
|
35 | # Limit the number of HTTP headers fields in a request. | |
|
36 | limit_request_fields = 1024 | |||
|
37 | ||||
|
38 | # Limit the allowed size of an HTTP request header field. | |||
|
39 | # Value is a positive number or 0. | |||
|
40 | # Setting it to 0 will allow unlimited header field sizes. | |||
|
41 | limit_request_field_size = 0 | |||
|
42 | ||||
|
43 | ||||
|
44 | # Timeout for graceful workers restart. | |||
|
45 | # After receiving a restart signal, workers have this much time to finish | |||
|
46 | # serving requests. Workers still alive after the timeout (starting from the | |||
|
47 | # receipt of the restart signal) are force killed. | |||
|
48 | graceful_timeout = 30 | |||
|
49 | ||||
|
50 | ||||
|
51 | # The number of seconds to wait for requests on a Keep-Alive connection. | |||
|
52 | # Generally set in the 1-5 seconds range. | |||
|
53 | keepalive = 2 | |||
|
54 | ||||
34 |
|
55 | |||
35 | # SERVER MECHANICS |
|
56 | # SERVER MECHANICS | |
36 | # None == system temp dir |
|
57 | # None == system temp dir | |
37 | worker_tmp_dir = None |
|
58 | worker_tmp_dir = None | |
38 | tmp_upload_dir = None |
|
59 | tmp_upload_dir = None | |
39 |
|
60 | |||
40 | # Custom log format |
|
61 | # Custom log format | |
41 | access_log_format = ( |
|
62 | access_log_format = ( | |
42 | '%(t)s GNCRN %(p)-8s %(h)-15s rqt:%(L)s %(s)s %(b)-6s "%(m)s:%(U)s %(q)s" usr:%(u)s "%(f)s" "%(a)s"') |
|
63 | '%(t)s GNCRN %(p)-8s %(h)-15s rqt:%(L)s %(s)s %(b)-6s "%(m)s:%(U)s %(q)s" usr:%(u)s "%(f)s" "%(a)s"') | |
43 |
|
64 | |||
44 | # self adjust workers based on CPU count |
|
65 | # self adjust workers based on CPU count | |
45 | # workers = multiprocessing.cpu_count() * 2 + 1 |
|
66 | # workers = multiprocessing.cpu_count() * 2 + 1 | |
46 |
|
67 | |||
47 |
|
68 | |||
48 | def post_fork(server, worker): |
|
69 | def post_fork(server, worker): | |
49 | server.log.info("[<%-10s>] WORKER spawned", worker.pid) |
|
70 | server.log.info("[<%-10s>] WORKER spawned", worker.pid) | |
50 |
|
71 | |||
51 |
|
72 | |||
52 | def pre_fork(server, worker): |
|
73 | def pre_fork(server, worker): | |
53 | pass |
|
74 | pass | |
54 |
|
75 | |||
55 |
|
76 | |||
56 | def pre_exec(server): |
|
77 | def pre_exec(server): | |
57 | server.log.info("Forked child, re-executing.") |
|
78 | server.log.info("Forked child, re-executing.") | |
58 |
|
79 | |||
59 |
|
80 | |||
|
81 | def on_starting(server): | |||
|
82 | server.log.info("Server is starting.") | |||
|
83 | ||||
|
84 | ||||
60 | def when_ready(server): |
|
85 | def when_ready(server): | |
61 | server.log.info("Server is ready. Spawning workers") |
|
86 | server.log.info("Server is ready. Spawning workers") | |
62 |
|
87 | |||
63 |
|
88 | |||
|
89 | def on_reload(server): | |||
|
90 | pass | |||
|
91 | ||||
|
92 | ||||
64 | def worker_int(worker): |
|
93 | def worker_int(worker): | |
65 | worker.log.info("[<%-10s>] worker received INT or QUIT signal", worker.pid) |
|
94 | worker.log.info("[<%-10s>] worker received INT or QUIT signal", worker.pid) | |
66 |
|
95 | |||
67 | # get traceback info, on worker crash |
|
96 | # get traceback info, on worker crash | |
68 | id2name = dict([(th.ident, th.name) for th in threading.enumerate()]) |
|
97 | id2name = dict([(th.ident, th.name) for th in threading.enumerate()]) | |
69 | code = [] |
|
98 | code = [] | |
70 | for thread_id, stack in sys._current_frames().items(): |
|
99 | for thread_id, stack in sys._current_frames().items(): | |
71 | code.append( |
|
100 | code.append( | |
72 | "\n# Thread: %s(%d)" % (id2name.get(thread_id, ""), thread_id)) |
|
101 | "\n# Thread: %s(%d)" % (id2name.get(thread_id, ""), thread_id)) | |
73 | for fname, lineno, name, line in traceback.extract_stack(stack): |
|
102 | for fname, lineno, name, line in traceback.extract_stack(stack): | |
74 | code.append('File: "%s", line %d, in %s' % (fname, lineno, name)) |
|
103 | code.append('File: "%s", line %d, in %s' % (fname, lineno, name)) | |
75 | if line: |
|
104 | if line: | |
76 | code.append(" %s" % (line.strip())) |
|
105 | code.append(" %s" % (line.strip())) | |
77 | worker.log.debug("\n".join(code)) |
|
106 | worker.log.debug("\n".join(code)) | |
78 |
|
107 | |||
79 |
|
108 | |||
80 | def worker_abort(worker): |
|
109 | def worker_abort(worker): | |
81 | worker.log.info("[<%-10s>] worker received SIGABRT signal", worker.pid) |
|
110 | worker.log.info("[<%-10s>] worker received SIGABRT signal", worker.pid) | |
82 |
|
111 | |||
83 |
|
112 | |||
|
113 | def worker_exit(server, worker): | |||
|
114 | worker.log.info("[<%-10s>] worker exit", worker.pid) | |||
|
115 | ||||
|
116 | ||||
|
117 | def child_exit(server, worker): | |||
|
118 | worker.log.info("[<%-10s>] worker child exit", worker.pid) | |||
|
119 | ||||
|
120 | ||||
84 | def pre_request(worker, req): |
|
121 | def pre_request(worker, req): | |
85 | return |
|
122 | return | |
86 | worker.log.debug("[<%-10s>] PRE WORKER: %s %s", |
|
123 | worker.log.debug("[<%-10s>] PRE WORKER: %s %s", | |
87 | worker.pid, req.method, req.path) |
|
124 | worker.pid, req.method, req.path) | |
88 |
|
125 | |||
89 |
|
126 | |||
90 | def post_request(worker, req, environ, resp): |
|
127 | def post_request(worker, req, environ, resp): | |
91 | return |
|
128 | return | |
92 | worker.log.debug("[<%-10s>] POST WORKER: %s %s resp: %s", worker.pid, |
|
129 | worker.log.debug("[<%-10s>] POST WORKER: %s %s resp: %s", worker.pid, | |
93 | req.method, req.path, resp.status_code) |
|
130 | req.method, req.path, resp.status_code) | |
94 |
|
131 | |||
95 |
|
132 | |||
|
133 | ||||
96 | class RhodeCodeLogger(Logger): |
|
134 | class RhodeCodeLogger(Logger): | |
97 | """ |
|
135 | """ | |
98 | Custom Logger that allows some customization that gunicorn doesn't allow |
|
136 | Custom Logger that allows some customization that gunicorn doesn't allow | |
99 | """ |
|
137 | """ | |
100 |
|
138 | |||
101 | datefmt = r"%Y-%m-%d %H:%M:%S" |
|
139 | datefmt = r"%Y-%m-%d %H:%M:%S" | |
102 |
|
140 | |||
103 | def __init__(self, cfg): |
|
141 | def __init__(self, cfg): | |
104 | Logger.__init__(self, cfg) |
|
142 | Logger.__init__(self, cfg) | |
105 |
|
143 | |||
106 | def now(self): |
|
144 | def now(self): | |
107 | """ return date in RhodeCode Log format """ |
|
145 | """ return date in RhodeCode Log format """ | |
108 | now = time.time() |
|
146 | now = time.time() | |
109 | msecs = int((now - long(now)) * 1000) |
|
147 | msecs = int((now - long(now)) * 1000) | |
110 | return time.strftime(self.datefmt, time.localtime(now)) + '.{0:03d}'.format(msecs) |
|
148 | return time.strftime(self.datefmt, time.localtime(now)) + '.{0:03d}'.format(msecs) | |
111 |
|
149 | |||
112 |
|
150 | |||
113 | logger_class = RhodeCodeLogger |
|
151 | logger_class = RhodeCodeLogger |
General Comments 0
You need to be logged in to leave comments.
Login now