Startcelery mgmt cmd now pipes output to logfile only if 'console' handler is not present in the Django LOGGING settings. Also updated stopcelery warnings to make it clear they are just warnings not errors.

This commit is contained in:
dvanaken 2020-04-03 01:20:51 -04:00 committed by Dana Van Aken
parent 66ad361654
commit 72580f65a0
2 changed files with 14 additions and 13 deletions

View File

@ -80,15 +80,14 @@ class Command(BaseCommand):
self.stdout = open(os.devnull, 'w') self.stdout = open(os.devnull, 'w')
# Stealth option that assigns where to pipe initial output # Stealth option that assigns where to pipe initial output
pipe = options.get('pipe', None) pipe = options.get('pipe', '')
if pipe is None: if not pipe:
pipe = '> /dev/null 2>&1' handler_names = settings.LOGGING.get('loggers', {}).get('celery', {}).get('handlers', [])
try: if 'console' not in handler_names and 'celery' in handler_names:
if 'celery' in settings.LOGGING['loggers']['celery']['handlers']: logfile = settings.LOGGING.get('handlers', {}).get('celery', {}).get('filename', None)
logfile = settings.LOGGING['handlers']['celery']['filename'] if logfile:
pipe = '>> {} 2>&1'.format(logfile) pipe = '>> {} 2>&1'.format(logfile)
except KeyError: pipe = pipe or ''
pass
loglevel = options['loglevel'] or ('DEBUG' if settings.DEBUG else 'INFO') loglevel = options['loglevel'] or ('DEBUG' if settings.DEBUG else 'INFO')
celery_options = [ celery_options = [
@ -101,7 +100,7 @@ class Command(BaseCommand):
'--pidfile={}'.format(options['celerybeat_pidfile']), '--pidfile={}'.format(options['celerybeat_pidfile']),
] + self._parse_suboptions(options['celerybeat_options']) ] + self._parse_suboptions(options['celerybeat_options'])
with lcd(settings.PROJECT_ROOT), hide('commands'): # pylint: disable=not-context-manager with lcd(settings.PROJECT_ROOT): # pylint: disable=not-context-manager
if not options['celerybeat_only']: if not options['celerybeat_only']:
local(self.celery_cmd( local(self.celery_cmd(
cmd='celery worker', opts=' '.join(celery_options), pipe=pipe)) cmd='celery worker', opts=' '.join(celery_options), pipe=pipe))

View File

@ -39,7 +39,7 @@ class Command(BaseCommand):
check_pidfiles.append((name, pidfile)) check_pidfiles.append((name, pidfile))
except Exception as e: # pylint: disable=broad-except except Exception as e: # pylint: disable=broad-except
self.stdout.write(self.style.NOTICE( self.stdout.write(self.style.NOTICE(
"ERROR: an exception occurred while stopping '{}':\n{}\n".format(name, e))) "WARNING: an exception occurred while stopping '{}': {}\n".format(name, e)))
if check_pidfiles: if check_pidfiles:
self.stdout.write("Waiting for processes to shutdown...\n") self.stdout.write("Waiting for processes to shutdown...\n")
@ -49,9 +49,12 @@ class Command(BaseCommand):
time.sleep(1) time.sleep(1)
wait_sec += 1 wait_sec += 1
if os.path.exists(pidfile): if os.path.exists(pidfile):
self.stdout.write(self.style.NOTICE( self.stdout.write(self.style.NOTICE((
"WARNING: file '{}' still exists after stopping {}.".format( "WARNING: file '{}' still exists after stopping {}. "
"Removing it manually.").format(
pidfile, name))) pidfile, name)))
with quiet():
local('rm -f {}'.format(pidfile))
else: else:
self.stdout.write(self.style.SUCCESS( self.stdout.write(self.style.SUCCESS(
"Successfully stopped '{}'.".format(name))) "Successfully stopped '{}'.".format(name)))
@ -59,4 +62,3 @@ class Command(BaseCommand):
with quiet(): with quiet():
local("ps auxww | grep '[c]elery worker' | awk '{print $2}' | xargs kill -9") local("ps auxww | grep '[c]elery worker' | awk '{print $2}' | xargs kill -9")
local("ps auxww | grep '[c]elerybeat' | awk '{print $2}' | xargs kill -9") local("ps auxww | grep '[c]elerybeat' | awk '{print $2}' | xargs kill -9")
local('rm -f {} {}'.format(options['celery_pidfile'], options['celerybeat_pidfile']))