Skip to content

Commit

Permalink
Adds name and versions on the commandline.
Browse files Browse the repository at this point in the history
The name and version are used to display proctitles. If name is
omitted, the module name is used to be backwards compatible.

If version is omitted, the last part of basepath is used to be
backwards compatible.
  • Loading branch information
Eskil Olsen committed Jan 2, 2013
1 parent 1a1e045 commit 174a2bc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
5 changes: 4 additions & 1 deletion ChangeLog
@@ -1,2 +1,5 @@
0.2 Wed Jan 2 10:38:23 PST 2013
* Add a --name/-n option to set proctitle name (eskil@eskil.org).

0.1 Wed Nov 14 11:50:44 PST 2012
* Starting a ChangeLog file (eskil@yelp.com)
* Starting a ChangeLog file (eskil@yelp.com)
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -4,7 +4,7 @@

setup(
name = 'zygote',
version = '0.1',
version = '0.2',
author = 'Evan Klitzke',
author_email = 'evan@eklitzke.org',
description = 'A tornado HTTP worker management tool',
Expand Down
2 changes: 2 additions & 0 deletions zygote/main.py
Expand Up @@ -27,6 +27,8 @@ def main():
parser.add_option('-b', '--basepath', default=os.environ.get('BASEPATH', ''), help='The basepath to use')
parser.add_option('--control-port', type='int', default=5100, help='The control port to listen on')
parser.add_option('-d', '--debug', default=False, action='store_true', help='Enable debugging')
parser.add_option('-n', '--name', default=None, help='The name of the application to set in proctitle, otherwise use the app module.')
parser.add_option('--version', default=None, help='The version of the application to set in proctitle.')
parser.add_option('-m', '--module', default=None, help='The name of the module holding get_application()')
parser.add_option('-p', '--port', type='int', default=0, help='The port to bind on')
parser.add_option('-i', '--interface', default='', help='The interface to bind on')
Expand Down
12 changes: 10 additions & 2 deletions zygote/master.py
Expand Up @@ -50,6 +50,8 @@ def __init__(
sock,
basepath,
module,
name,
version,
num_workers,
control_port,
control_socket_path,
Expand All @@ -68,6 +70,8 @@ def __init__(
self.sock = sock
self.basepath = basepath
self.module = module
self.name = name
self.version = version
self.num_workers = num_workers
self.control_port = control_port
self.control_socket_path = control_socket_path
Expand Down Expand Up @@ -419,6 +423,8 @@ def create_zygote(self, canary=False):
sock=self.sock,
basepath=realbase,
module=self.module,
name=self.name,
version=self.version,
args=self.application_args,
ssl_options=self.ssl_options,
canary=canary,
Expand All @@ -433,9 +439,9 @@ def start(self):
self.io_loop.start()

def main(opts, extra_args):
setproctitle('zygote master %s' % (opts.module,))
setproctitle('zygote master %s' % (opts.name or opts.module,))
zygote_logger = get_logger('zygote', opts.debug)

if not logging.root.handlers:
# XXX: WARNING
#
Expand Down Expand Up @@ -486,6 +492,8 @@ def main(opts, extra_args):
sock,
basepath=opts.basepath,
module=opts.module,
name=opts.name or opts.module,
version=opts.version,
num_workers=opts.num_workers,
control_port=opts.control_port,
control_socket_path=opts.control_socket_path,
Expand Down
17 changes: 12 additions & 5 deletions zygote/worker.py
Expand Up @@ -70,7 +70,11 @@ class ZygoteWorker(object):
# how many seconds to wait before sending SIGKILL to children
WAIT_FOR_KILL_TIME = 10.0

def __init__(self, sock, basepath, module, args, ssl_options=None, canary=False, debug=False):
def __init__(self, sock, basepath, module, name, version, args, ssl_options=None, canary=False, debug=False):
self.module = module
self.name = name
self.version = version
self.basepath = basepath
self.args = args
self.ssl_options = ssl_options
self.ppid = os.getppid()
Expand All @@ -91,16 +95,19 @@ def __init__(self, sock, basepath, module, args, ssl_options=None, canary=False,
sys.exit(INIT_FAILURE_EXIT_CODE)

try:
self._real_init(sock, basepath, module, args)
self._real_init(sock, self.basepath, self.module, args)
except Exception:
self.logger.exception("Error performing initialization of %s", self)
sys.exit(INIT_FAILURE_EXIT_CODE)

def _version(self):
"""Get the actual version as specified or from the basepath."""
return self.version or self.basepath.split('/')[-1]

def _real_init(self, sock, basepath, module, args):
"""Actual initialization function. Broken out for error handling"""

self.version = basepath.split('/')[-1]
setproctitle('zygote version=%s' % (self.version,))
setproctitle('zygote name=%s version=%s' % (self.name, self._version(),))

# Create a pipe(2) pair. This will be used so workers can detect when
# the intermediate zygote exits -- when this happens, a read event will
Expand Down Expand Up @@ -259,7 +266,7 @@ def on_close(disconnected=False):
notify(sock, message.MessageHTTPEnd)

notify(sock, message.MessageWorkerStart, '%d %d' % (int(time_created * 1e6), os.getppid()))
setproctitle('zygote-worker version=%s' % self.version)
setproctitle('zygote-worker name=%s version=%s' % (self.name, self._version(),))
try:
# io_loop is passed into get_application for program to add handler
# or schedule task on the main io_loop. Program that uses this
Expand Down

0 comments on commit 174a2bc

Please sign in to comment.