daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 72 in 147 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. A python script which starts celery worker and auto reload it when any code change happens.
  3. '''
  4.  
  5. import time
  6. from watchdog.observers import Observer
  7. from watchdog.events import PatternMatchingEventHandler
  8. import psutil
  9. import os
  10. import subprocess
  11.  
  12. code_dir_to_monitor = '.'
  13. celery_working_dir = code_dir_to_monitor
  14. celeryworker_cmdline = 'celery worker -A tasks:app -l INFO'.split(" ")
  15. celerybeat_cmdline = 'celery -A tasks:app beat'.split(" ")
  16.  
  17. class MyHandler(PatternMatchingEventHandler):
  18.  
  19.     def on_any_event(self, event):
  20.         print("detected change. event = {}".format(event))
  21.  
  22.         for proc in psutil.process_iter():
  23.             proc_cmdline = self._get_proc_cmdline(proc)
  24.             if not proc_cmdline or len(proc_cmdline) < len(celeryworker_cmdline):
  25.                 continue
  26.  
  27.             is_celery_worker = 'python' in proc_cmdline[0].lower() \
  28.                                and celeryworker_cmdline[0] == proc_cmdline[1] \
  29.                                and celeryworker_cmdline[1] == proc_cmdline[2]
  30.  
  31.             if not is_celery_worker:
  32.                 continue
  33.  
  34.             proc.kill()
  35.             print("Just killed {} on working dir {}".format(proc_cmdline, proc.cwd()))
  36.  
  37.         run_worker()
  38.  
  39.     def _get_proc_cmdline(self, proc):
  40.         try:
  41.             return proc.cmdline()
  42.         except Exception as e:
  43.             return []
  44.  
  45.  
  46. def run_worker():
  47.     print("Ready to call {} ".format(celeryworker_cmdline))
  48.     os.chdir(celery_working_dir)
  49.     subprocess.Popen(celeryworker_cmdline)
  50.     print("Done callling {} ".format(celeryworker_cmdline))
  51.  
  52. def run_beat():
  53.     print("Ready to call {} ".format(celerybeat_cmdline))
  54.     os.chdir(celery_working_dir)
  55.     subprocess.Popen(celerybeat_cmdline)
  56.     print("Done callling {} ".format(celerybeat_cmdline))
  57.  
  58.  
  59. if __name__ == "__main__":
  60.  
  61.     run_worker()
  62.     run_beat()
  63.  
  64.     event_handler = MyHandler(patterns=["*.py"])
  65.     observer = Observer()
  66.     observer.schedule(event_handler, code_dir_to_monitor, recursive=True)
  67.     observer.start()
  68.     print("File change observer started")
  69.     print("Celery Beat started")
  70.  
  71.     try:
  72.         while True:
  73.             time.sleep(1)
  74.     except KeyboardInterrupt:
  75.         observer.stop()
  76.     observer.join()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top