Просмотр исходного кода

Multi: %n is now the same as %N to be consistent with "celery worker".

Closes #1938
Ask Solem 11 лет назад
Родитель
Сommit
ec905fadc4
3 измененных файлов с 40 добавлено и 47 удалено
  1. 25 33
      celery/bin/multi.py
  2. 4 3
      celery/utils/__init__.py
  3. 11 11
      docs/tutorials/daemonizing.rst

+ 25 - 33
celery/bin/multi.py

@@ -13,19 +13,19 @@ Examples
 
     # Pidfiles and logfiles are stored in the current directory
     # by default.  Use --pidfile and --logfile argument to change
-    # this.  The abbreviation %N will be expanded to the current
+    # this.  The abbreviation %n will be expanded to the current
     # node name.
-    $ celery multi start Leslie -E --pidfile=/var/run/celery/%N.pid
-                                    --logfile=/var/log/celery/%N.log
+    $ celery multi start Leslie -E --pidfile=/var/run/celery/%n.pid
+                                    --logfile=/var/log/celery/%n.log
 
 
     # You need to add the same arguments when you restart,
     # as these are not persisted anywhere.
-    $ celery multi restart Leslie -E --pidfile=/var/run/celery/%N.pid
-                                     --logfile=/var/run/celery/%N.log
+    $ celery multi restart Leslie -E --pidfile=/var/run/celery/%n.pid
+                                     --logfile=/var/run/celery/%n.log
 
     # To stop the node, you need to specify the same pidfile.
-    $ celery multi stop Leslie --pidfile=/var/run/celery/%N.pid
+    $ celery multi stop Leslie --pidfile=/var/run/celery/%n.pid
 
     # 3 workers, with 3 processes each
     $ celery multi start 3 -c 3
@@ -101,6 +101,7 @@ import socket
 import sys
 
 from collections import defaultdict, namedtuple
+from functools import partial
 from subprocess import Popen
 from time import sleep
 
@@ -111,7 +112,8 @@ from kombu.utils.encoding import from_utf8
 from celery import VERSION_BANNER
 from celery.five import items
 from celery.platforms import Pidfile, IS_WINDOWS
-from celery.utils import term, nodesplit
+from celery.utils import term
+from celery.utils import host_format, node_format, nodesplit
 from celery.utils.text import pluralize
 
 __all__ = ['MultiTool']
@@ -247,8 +249,8 @@ class MultiTool(object):
         self.retcode = int(any(retcodes))
 
     def with_detacher_default_options(self, p):
-        _setdefaultopt(p.options, ['--pidfile', '-p'], '%N.pid')
-        _setdefaultopt(p.options, ['--logfile', '-f'], '%N.log')
+        _setdefaultopt(p.options, ['--pidfile', '-p'], '%n.pid')
+        _setdefaultopt(p.options, ['--logfile', '-f'], '%n.log')
         p.options.setdefault(
             '--cmd',
             '-m {0}'.format(celery_exe('worker', '--detach')),
@@ -320,7 +322,7 @@ class MultiTool(object):
             self.note('')
 
     def getpids(self, p, cmd, callback=None):
-        _setdefaultopt(p.options, ['--pidfile', '-p'], '%N.pid')
+        _setdefaultopt(p.options, ['--pidfile', '-p'], '%n.pid')
 
         nodes = []
         for node in multi_args(p, cmd):
@@ -491,25 +493,27 @@ def multi_args(p, cmd='celery worker', append='', prefix='', suffix=''):
                 raise KeyError('No node at index %r' % (ns_name, ))
 
     for name in names:
-        this_suffix = suffix
+        hostname = suffix
         if '@' in name:
-            this_name = options['-n'] = name
-            nodename, this_suffix = nodesplit(name)
-            name = nodename
+            nodename = options['-n'] = host_format(name)
+            shortname, hostname = nodesplit(nodename)
+            name = shortname
         else:
-            nodename = '%s%s' % (prefix, name)
-            this_name = options['-n'] = '%s@%s' % (nodename, this_suffix)
-        expand = abbreviations({'%h': this_name,
-                                '%n': name,
-                                '%N': nodename,
-                                '%d': this_suffix})
+            shortname = '%s%s' % (prefix, name)
+            nodename = options['-n'] = host_format(
+                '{0}@{1}'.format(shortname, hostname),
+            )
+
+        expand = partial(
+            node_format, nodename=nodename, N=shortname, d=hostname,
+        )
         argv = ([expand(cmd)] +
                 [format_opt(opt, expand(value))
                  for opt, value in items(p.optmerge(name, options))] +
                 [passthrough])
         if append:
             argv.append(expand(append))
-        yield multi_args_t(this_name, argv, expand, name)
+        yield multi_args_t(nodename, argv, expand, name)
 
 
 class NamespacedOptionParser(object):
@@ -591,18 +595,6 @@ def parse_ns_range(ns, ranges=False):
     return ret
 
 
-def abbreviations(mapping):
-
-    def expand(S):
-        ret = S
-        if S is not None:
-            for short_opt, long_opt in items(mapping):
-                ret = ret.replace(short_opt, long_opt)
-        return ret
-
-    return expand
-
-
 def findsig(args, default=signal.SIGTERM):
     for arg in reversed(args):
         if len(arg) == 2 and arg[0] == '-':

+ 4 - 3
celery/utils/__init__.py

@@ -352,7 +352,7 @@ def default_nodename(hostname):
 def node_format(s, nodename, **extra):
     name, host = nodesplit(nodename)
     return host_format(
-        s, host, n=name or NODENAME_DEFAULT, **extra)
+        s, host, name or NODENAME_DEFAULT, **extra)
 
 
 def _fmt_process_index(prefix='', default='0'):
@@ -362,9 +362,10 @@ def _fmt_process_index(prefix='', default='0'):
 _fmt_process_index_with_prefix = partial(_fmt_process_index, '-', '')
 
 
-def host_format(s, host=None, **extra):
+def host_format(s, host=None, name=None, **extra):
     host = host or socket.gethostname()
-    name, _, domain = host.partition('.')
+    hname, _, domain = host.partition('.')
+    name = name or hname
     keys = dict({
         'h': host, 'n': name, 'd': domain,
         'i': _fmt_process_index, 'I': _fmt_process_index_with_prefix,

+ 11 - 11
docs/tutorials/daemonizing.rst

@@ -96,9 +96,9 @@ This is an example configuration for a Python project.
     # Extra command-line arguments to the worker
     CELERYD_OPTS="--time-limit=300 --concurrency=8"
 
-    # %N will be replaced with the first part of the nodename.
-    CELERYD_LOG_FILE="/var/log/celery/%N.log"
-    CELERYD_PID_FILE="/var/run/celery/%N.pid"
+    # %n will be replaced with the first part of the nodename.
+    CELERYD_LOG_FILE="/var/log/celery/%n.log"
+    CELERYD_PID_FILE="/var/run/celery/%n.pid"
 
     # Workers should run as an unprivileged user.
     #   You need to create this user manually (or you can choose
@@ -153,10 +153,10 @@ Available options
     directory.
 
 * CELERYD_PID_FILE
-    Full path to the PID file. Default is /var/run/celery/%N.pid
+    Full path to the PID file. Default is /var/run/celery/%n.pid
 
 * CELERYD_LOG_FILE
-    Full path to the worker log file. Default is /var/log/celery/%N.log
+    Full path to the worker log file. Default is /var/log/celery/%n.log
 
 * CELERYD_LOG_LEVEL
     Worker log level. Default is INFO.
@@ -311,9 +311,9 @@ This is an example configuration for a Python project:
     # Extra command-line arguments to the worker
     CELERYD_OPTS="--time-limit=300 --concurrency=8"
 
-    # %N will be replaced with the first part of the nodename.
-    CELERYD_LOG_FILE="/var/log/celery/%N.log"
-    CELERYD_PID_FILE="/var/run/celery/%N.pid"
+    # %n will be replaced with the first part of the nodename.
+    CELERYD_LOG_FILE="/var/log/celery/%n.log"
+    CELERYD_PID_FILE="/var/run/celery/%n.pid"
 
 .. _generic-systemd-celeryd-django-example:
 
@@ -339,9 +339,9 @@ This is an example configuration for those using `django-celery`:
     # Extra command-line arguments to the worker
     CELERYD_OPTS="--time-limit=300 --concurrency=8"
 
-    # %N will be replaced with the first part of the nodename.
-    CELERYD_LOG_FILE="/var/log/celery/%N.log"
-    CELERYD_PID_FILE="/var/run/celery/%N.pid"
+    # %n will be replaced with the first part of the nodename.
+    CELERYD_LOG_FILE="/var/log/celery/%n.log"
+    CELERYD_PID_FILE="/var/run/celery/%n.pid"
 
 To add an environment variable such as DJANGO_SETTINGS_MODULE use the
 Environment in celery.service.