Ver Fonte

Expand hostnames in --hostname argument. Closes #495

Ask Solem há 12 anos atrás
pai
commit
31d4b4fb45
3 ficheiros alterados com 35 adições e 11 exclusões
  1. 8 0
      celery/bin/base.py
  2. 11 8
      celery/bin/celeryd.py
  3. 16 3
      docs/userguide/workers.rst

+ 8 - 0
celery/bin/base.py

@@ -60,6 +60,7 @@ from __future__ import absolute_import, print_function
 
 import os
 import re
+import socket
 import sys
 import warnings
 
@@ -86,6 +87,7 @@ Try --help?
 
 find_long_opt = re.compile(r'.+?(--.+?)(?:\s|,|$)')
 find_rst_ref = re.compile(r':\w+:`(.+?)`')
+find_sformat = re.compile(r'%(\w)')
 
 
 class HelpFormatter(IndentedHelpFormatter):
@@ -376,6 +378,12 @@ class Command(object):
         """
         pass
 
+    def simple_format(self, s, match=find_sformat, expand=r'\1', **keys):
+        host = socket.gethostname()
+        name, _, domain = host.partition('.')
+        keys = dict({'%': '%', 'h': host, 'n': name, 'd': domain}, **keys)
+        return match.sub(lambda m: keys[m.expand(expand)], s)
+
     def _get_default_app(self, *args, **kwargs):
         from celery.app import default_app
         return default_app._get_current_object()  # omit proxy

+ 11 - 8
celery/bin/celeryd.py

@@ -31,7 +31,8 @@ The :program:`celery worker` command (previously known as ``celeryd``)
 
 .. cmdoption:: -n, --hostname
 
-    Set custom hostname, e.g. 'foo.example.com'.
+    Set custom hostname, e.g. 'w1.%h'. Expands: %h (hostname),
+    %n (name) and %d, (domain).
 
 .. cmdoption:: -B, --beat
 
@@ -135,24 +136,26 @@ class WorkerCommand(Command):
             argv = list(sys.argv)
         return super(WorkerCommand, self).execute_from_commandline(argv)
 
-    def run(self, *args, **kwargs):
-        kwargs.pop('app', None)
+    def run(self, hostname=None, pool_cls=None, loglevel=None,
+            app=None, **kwargs):
         # Pools like eventlet/gevent needs to patch libs as early
         # as possible.
-        kwargs['pool_cls'] = concurrency.get_implementation(
-                    kwargs.get('pool_cls') or self.app.conf.CELERYD_POOL)
+        pool_cls = (concurrency.get_implementation(pool_cls) or
+                    self.app.conf.CELERYD_POOL)
         if self.app.IS_WINDOWS and kwargs.get('beat'):
             self.die('-B option does not work on Windows.  '
                      'Please run celerybeat as a separate service.')
-        loglevel = kwargs.get('loglevel')
+        hostname = self.simple_format(hostname)
         if loglevel:
             try:
-                kwargs['loglevel'] = mlevel(loglevel)
+                loglevel = mlevel(loglevel)
             except KeyError:  # pragma: no cover
                 self.die('Unknown level {0!r}. Please use one of {1}.'.format(
                     loglevel, '|'.join(l for l in LOG_LEVELS.keys()
                       if isinstance(l, basestring))))
-        return self.app.Worker(**kwargs).run()
+        return self.app.Worker(
+            hostname=hostname, pool_cls=pool_cls, loglevel=loglevel, **kwargs
+        ).run()
 
     def with_pool_option(self, argv):
         # this command support custom pools

+ 16 - 3
docs/userguide/workers.rst

@@ -32,9 +32,22 @@ You can also start multiple workers on the same machine. If you do so
 be sure to give a unique name to each individual worker by specifying a
 host name with the :option:`--hostname|-n` argument::
 
-    $ celery worker --loglevel=INFO --concurrency=10 -n worker1.example.com
-    $ celery worker --loglevel=INFO --concurrency=10 -n worker2.example.com
-    $ celery worker --loglevel=INFO --concurrency=10 -n worker3.example.com
+    $ celery worker --loglevel=INFO --concurrency=10 -n worker1.%h
+    $ celery worker --loglevel=INFO --concurrency=10 -n worker2.%h
+    $ celery worker --loglevel=INFO --concurrency=10 -n worker3.%h
+
+The hostname argument can expand the following variables:
+
+    - ``%h``:  Hostname including domain name.
+    - ``%n``:  Hostname only.
+    - ``%d``:  Domain name only.
+
+E.g. if the current hostname is ``george.example.com`` then
+these will expand to:
+
+    - ``worker1.%h`` -> ``worker1.george.example.com``
+    - ``worker1.%n`` -> ``worker1.george``
+    - ``worker1.%d`` -> ``worker1.example.com``
 
 .. _worker-stopping: