Skip to content

Instantly share code, notes, and snippets.

@haroldrandom
Created January 11, 2019 06:05
Show Gist options
  • Save haroldrandom/0ddc6fe4be443f8c739ab558f3ec4398 to your computer and use it in GitHub Desktop.
Save haroldrandom/0ddc6fe4be443f8c739ab558f3ec4398 to your computer and use it in GitHub Desktop.
Django custom command for autorestart celery workers
import os
import time
import shlex
from datetime import datetime
from django.core.management.base import BaseCommand
from django.conf import settings
from watchdog.observers import Observer
from watchdog.tricks import AutoRestartTrick
class Command(BaseCommand):
help = 'Start celery workers. Autoreload when file changed.'
start_celery_cmd = 'celery -A LazyPipeline worker -l INFO'
def handle(self, *args, **kwargs):
self.stdout.write(self.style.WARNING(
'[%s] %s' % (self._now, 'starting celery workers by watchdog...')))
os.chdir(settings.BASE_DIR)
handler = AutoRestartTrick(
command=shlex.split(self.start_celery_cmd),
patterns=['*.py'])
handler.start() # start celery
ob = Observer()
ob.schedule(handler, settings.BASE_DIR, recursive=True)
ob.start()
try:
while True:
time.sleep(3)
except KeyboardInterrupt:
self.stdout.write(self.style.SUCCESS(
'[%s] %s' % (self._now, 'stopping celery workers...')))
ob.stop()
ob.join()
handler.stop()
@property
def _now(self):
return datetime.now().strftime('%B %d, %Y - %X')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment