stop_celery_worker.py 1.42 KB
#!/usr/bin/env python3


# OLD BASH SCRIPT
# ---------------

# (1) Empty only monitoring queue:
#celery -A pyros amqp queue.purge monitoring_q

# (2) Empty ALL queues
#celery -A pyros purge -f

# (3) THE HARD WAY : something like that...
#ps aux | grep \"celery worker\" | awk '{print $2}' | xargs kill -9



# NEW PYTHON SCRIPT
# -----------------

import subprocess
import sys
import platform

def main():
    # if len(sys.argv) > 1:
    #
    #     if sys.argv[1] == "--help":
    #         print("stop_celery_worker.py <arg>\n\t--all: purge all queue\n\t--hard: kill all celery worker "
    #               "\n\t--soft or no arg: purge monitoring queue (this is the standard way)\n\t--help:display this help")
    #         return True
    #     elif sys.argv[1] == "--all":
    #         p = subprocess.Popen("celery -A pyros purge -f", shell=True)
    #     elif sys.argv[1] == "--hard":
    if platform.system() == 'Windows':
        p = subprocess.Popen("taskkill /f /im celery.exe", shell=True)
    else:
        p = subprocess.Popen("ps aux | grep \"monitoring_q\" | awk '{print $2}' | xargs kill -9", shell=True)
    # else:
    #     p = subprocess.Popen("celery -A pyros amqp queue.purge monitoring_q", shell=True)
    p.wait()
    # if (p.returncode != 0):
    #     print("ERROR !: Worker's launch failed\r\n", file=sys.stderr)
    #     return False
    print("Workers stopped: OK")
    return True

if __name__ == '__main__':
    main()