Skip to content
Newer
Older
100644 153 lines (128 sloc) 4.19 KB
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
1 # -*- coding: utf-8 -
2 #
d37a9d8 edit header to tell the license.
Benoit Chesneau authored Jan 19, 2010
3 # This file is part of gunicorn released under the MIT license.
4 # See the NOTICE for more information.
bcfeed0 change code header to redirect to NOTICE file for more info.
Benoit Chesneau authored Jan 18, 2010
5
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
6
7 import logging
8 import optparse as op
80b4534 @benoitc first step for self daemonized. Still need to fix workers kill
benoitc authored Jan 29, 2010
9 import os
10 import sys
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
11
4bfdd60 remove relative imports. new close function
Benoit Chesneau authored Jan 18, 2010
12 from gunicorn.arbiter import Arbiter
6d4ef85 @benoitc inherit PIPE and fix bug in reap_workers
benoitc authored Jan 29, 2010
13 from gunicorn import util
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
14
15 LOG_LEVELS = {
16 "critical": logging.CRITICAL,
17 "error": logging.ERROR,
18 "warning": logging.WARNING,
19 "info": logging.INFO,
20 "debug": logging.DEBUG
21 }
22
6f4db28 @benoitc make daemonizing better
benoitc authored Jan 29, 2010
23 UMASK = 0
24
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
25 def options():
26 return [
57054f1 @benoitc don't set default values for command line options so we can use paster
benoitc authored Jan 27, 2010
27 op.make_option('--host', dest='host',
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
28 help='Host to listen on. [%default]'),
57054f1 @benoitc don't set default values for command line options so we can use paster
benoitc authored Jan 27, 2010
29 op.make_option('--port', dest='port', type='int',
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
30 help='Port to listen on. [%default]'),
23af620 @benoitc I thought global_conf would be enough to get all info from paster.
benoitc authored Jan 27, 2010
31 op.make_option('--workers', dest='workers', type='int',
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
32 help='Number of workers to spawn. [%default]'),
d5880e2 @benoitc add pidfile support. gunicorn --pid=/tmp/gunicorn.pid or paster server
benoitc authored Jan 29, 2010
33 op.make_option('-p','--pid', dest='pidfile',
34 help='set the background PID FILE'),
4be8e9f @benoitc some fixes. Now we have daemonized mode and pidfile.
benoitc authored Jan 30, 2010
35 op.make_option('-D', '--daemon', dest='daemon', action="store_true",
80b4534 @benoitc first step for self daemonized. Still need to fix workers kill
benoitc authored Jan 29, 2010
36 help='Run daemonized in the background.'),
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
37 op.make_option('--log-level', dest='loglevel', default='info',
38 help='Log level below which to silence messages. [%default]'),
39 op.make_option('--log-file', dest='logfile', default='-',
e830a8f add debug option and support in paster. It allows us to work with paster
Benoit Chesneau authored Jan 23, 2010
40 help='Log to a file. - is stdout. [%default]'),
41 op.make_option('-d', '--debug', dest='debug', action="store_true",
42 default=False, help='Debug mode. only 1 worker.')
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
43 ]
44
45 def configure_logging(opts):
46 handlers = []
47 if opts.logfile != "-":
48 handlers.append(logging.FileHandler(opts.logfile))
49 else:
50 handlers.append(logging.StreamHandler())
51
52 loglevel = LOG_LEVELS.get(opts.loglevel.lower(), logging.INFO)
53
54 logger = logging.getLogger()
55 logger.setLevel(loglevel)
56 for h in handlers:
57 h.setFormatter(logging.Formatter("%(levelname)s %(message)s"))
58 logger.addHandler(h)
80b4534 @benoitc first step for self daemonized. Still need to fix workers kill
benoitc authored Jan 29, 2010
59
4be8e9f @benoitc some fixes. Now we have daemonized mode and pidfile.
benoitc authored Jan 30, 2010
60 def daemonize():
80b4534 @benoitc first step for self daemonized. Still need to fix workers kill
benoitc authored Jan 29, 2010
61 if not 'GUNICORN_FD' in os.environ:
6f4db28 @benoitc make daemonizing better
benoitc authored Jan 29, 2010
62 if os.fork() == 0:
63 os.setsid()
64 if os.fork() == 0:
65 os.umask(UMASK)
66 else:
67 os._exit(0)
68 else:
69 os._exit(0)
70
6d4ef85 @benoitc inherit PIPE and fix bug in reap_workers
benoitc authored Jan 29, 2010
71 maxfd = util.get_maxfd()
6f4db28 @benoitc make daemonizing better
benoitc authored Jan 29, 2010
72
73 # Iterate through and close all file descriptors.
74 for fd in range(0, maxfd):
75 try:
76 os.close(fd)
77 except OSError: # ERROR, fd wasn't open to begin with (ignored)
78 pass
79
6d4ef85 @benoitc inherit PIPE and fix bug in reap_workers
benoitc authored Jan 29, 2010
80 os.open(util.REDIRECT_TO, os.O_RDWR)
6f4db28 @benoitc make daemonizing better
benoitc authored Jan 29, 2010
81 os.dup2(0, 1)
82 os.dup2(0, 2)
80b4534 @benoitc first step for self daemonized. Still need to fix workers kill
benoitc authored Jan 29, 2010
83
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
84 def main(usage, get_app):
85 parser = op.OptionParser(usage=usage, option_list=options())
86 opts, args = parser.parse_args()
ced1f39 @benoitc some fixes. still dont handle SIGQUIT on workers :/
benoitc authored Jan 30, 2010
87 configure_logging(opts)
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
88
89 app = get_app(parser, opts, args)
23af620 @benoitc I thought global_conf would be enough to get all info from paster.
benoitc authored Jan 27, 2010
90 workers = opts.workers or 1
e830a8f add debug option and support in paster. It allows us to work with paster
Benoit Chesneau authored Jan 23, 2010
91 if opts.debug:
92 workers = 1
57054f1 @benoitc don't set default values for command line options so we can use paster
benoitc authored Jan 27, 2010
93
94 host = opts.host or '127.0.0.1'
95 port = opts.port
96 if port is None:
97 if ':' in host:
98 host, port = host.split(':', 1)
99 port = int(port)
100 else:
101 port = 8000
d5880e2 @benoitc add pidfile support. gunicorn --pid=/tmp/gunicorn.pid or paster server
benoitc authored Jan 29, 2010
102
103 kwargs = dict(
104 debug=opts.debug,
105 pidfile=opts.pidfile
106 )
e830a8f add debug option and support in paster. It allows us to work with paster
Benoit Chesneau authored Jan 23, 2010
107
57054f1 @benoitc don't set default values for command line options so we can use paster
benoitc authored Jan 27, 2010
108 arbiter = Arbiter((host,port), workers, app,
d5880e2 @benoitc add pidfile support. gunicorn --pid=/tmp/gunicorn.pid or paster server
benoitc authored Jan 29, 2010
109 **kwargs)
80b4534 @benoitc first step for self daemonized. Still need to fix workers kill
benoitc authored Jan 29, 2010
110 if opts.daemon:
ced1f39 @benoitc some fixes. still dont handle SIGQUIT on workers :/
benoitc authored Jan 29, 2010
111 daemonize()
112 else:
113 os.setpgrp()
aa30df9 @davisp Add logging level command line options.
authored Jan 17, 2010
114 arbiter.run()
63d45ef gunicorn server factory for paster.
Benoit Chesneau authored Jan 23, 2010
115
1a3c09b 80 cols.
Benoit Chesneau authored Jan 23, 2010
116 def paste_server(app, global_conf=None, host="127.0.0.1", port=None,
117 *args, **kwargs):
ced1f39 @benoitc some fixes. still dont handle SIGQUIT on workers :/
benoitc authored Jan 29, 2010
118 configure_logging(opts)
63d45ef gunicorn server factory for paster.
Benoit Chesneau authored Jan 23, 2010
119 if not port:
120 if ':' in host:
121 host, port = host.split(':', 1)
122 else:
123 port = 8000
124 bind_addr = (host, int(port))
125
23af620 @benoitc I thought global_conf would be enough to get all info from paster.
benoitc authored Jan 27, 2010
126 workers = kwargs.get("workers", 1)
127 if global_conf:
128 workers = int(global_conf.get('workers', workers))
129
e830a8f add debug option and support in paster. It allows us to work with paster
Benoit Chesneau authored Jan 23, 2010
130 debug = global_conf.get('debug') == "true"
131 if debug:
132 # we force to one worker in debug mode.
133 workers = 1
d5880e2 @benoitc add pidfile support. gunicorn --pid=/tmp/gunicorn.pid or paster server
benoitc authored Jan 29, 2010
134
135 pid = kwargs.get("pid")
136 if global_conf:
137 pid = global_conf.get('pid', pid)
138
80b4534 @benoitc first step for self daemonized. Still need to fix workers kill
benoitc authored Jan 29, 2010
139 daemon = kwargs.get("daemon")
140 if global_conf:
141 daemon = global_conf.get('daemon', daemonize)
142
d5880e2 @benoitc add pidfile support. gunicorn --pid=/tmp/gunicorn.pid or paster server
benoitc authored Jan 29, 2010
143 kwargs = dict(
144 debug=debug,
145 pidfile=pid
146 )
96a0a0c @benoitc update test.ini
benoitc authored Jan 26, 2010
147
d5880e2 @benoitc add pidfile support. gunicorn --pid=/tmp/gunicorn.pid or paster server
benoitc authored Jan 29, 2010
148 arbiter = Arbiter(bind_addr, workers, app, **kwargs)
80b4534 @benoitc first step for self daemonized. Still need to fix workers kill
benoitc authored Jan 29, 2010
149 if daemon == "true":
ced1f39 @benoitc some fixes. still dont handle SIGQUIT on workers :/
benoitc authored Jan 29, 2010
150 daemonize()
151 else:
152 os.setpgrp()
1a3c09b 80 cols.
Benoit Chesneau authored Jan 23, 2010
153 arbiter.run()
Something went wrong with that request. Please try again.