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