SHARE
TWEET

Untitled

a guest Dec 16th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import shlex
  3. import subprocess
  4.  
  5. from django.core.management.base import BaseCommand
  6. from django.utils import autoreload
  7.  
  8.  
  9. class Command(BaseCommand):
  10.     def add_arguments(self, parser):
  11.         parser.add_argument('--worker-pid-file', action='store', dest='worker_pid_file', default='/tmp/rqworker.pid')
  12.  
  13.     def handle(self, *args, **options):
  14.         autoreload.main(run_worker, (options['worker_pid_file'],))
  15.  
  16.  
  17. def run_worker(worker_pid_file):
  18.     if os.path.exists(worker_pid_file):
  19.         worker_pid = subprocess.run(['cat', worker_pid_file], stdout=subprocess.PIPE).stdout.decode('utf-8')
  20.         kill_worker_cmd = f'kill {worker_pid}'
  21.         subprocess.run(shlex.split(kill_worker_cmd), stderr=subprocess.PIPE)
  22.  
  23.     start_worker_cmd = f'{get_managepy_path()} rqworker --pid={worker_pid_file}'
  24.     subprocess.run(shlex.split(start_worker_cmd))
  25.  
  26.  
  27. def get_managepy_path():
  28.     managepy_path = None
  29.     search_path = os.path.dirname(__file__)
  30.     while not managepy_path:
  31.         if os.path.exists(os.path.join(search_path, 'manage.py')):
  32.             managepy_path = os.path.abspath(os.path.join(search_path, 'manage.py'))
  33.         else:
  34.             search_path = os.path.join(search_path, '../')
  35.     return managepy_path
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top