# Path: mrq/context.py
# _GLOBAL_CONTEXT = {
# 
#     # Contains all the running greenlets for this worker. greenletid => Job object
#     "greenlets": {},
# 
#     # pointer to the current worker
#     "worker": None,
# 
#     # pointer to the current config
#     "config": {}
# }
# def setup_context(**kwargs):
# def set_current_job(job):
# def get_current_job(greenlet_id=None):
# def set_current_worker(worker):
# def get_current_worker():
# def set_logger_config():
# def set_current_config(config):
# def get_current_config():
# def retry_current_job(delay=None, max_retries=None, queue=None):
# def abort_current_job():
# def _connections_factory(attr):
#     def versiontuple(v):
# def enable_greenlet_tracing():
#     def trace(*args):
# def run_task(path, params):
# def set_current_job_progress(ratio, save=False):
# def queue_raw_jobs(*args, **kwargs):
# def queue_job(*args, **kwargs):
# def queue_jobs(*args, **kwargs):
# def metric(*args, **kwargs):


from future.builtins import object
from .context import log
import psutil
import os
import time
import signal
import shlex
import gevent
    import subprocess32 as subprocess
    import subprocess

        if self.stopping and len(self.processes) == 0:
            self.stop_watch()

    def watch_process(self, process):
        """ Manages the status of a single process """

        status = process["psutil"].status()

        # TODO: how to avoid zombies?
        # print process["pid"], status

        if process.get("terminate"):
            if status in ("zombie", "dead"):
                process["dead"] = True
            elif process.get("terminate_at"):
                if time.time() > (process["terminate_at"] + 5):
                    log.warning("Process %s had to be sent SIGKILL" % (process["pid"], ))
                    process["subprocess"].send_signal(signal.SIGKILL)
                elif time.time() > process["terminate_at"]:
                    log.warning("Process %s had to be sent SIGTERM" % (process["pid"], ))
                    process["subprocess"].send_signal(signal.SIGTERM)

        else:
            if status in ("zombie", "dead"):
                # Restart a new process right away (TODO: sleep a bit? max retries?)
                process["dead"] = True
                self.spawn(process["command"])

            elif status not in ("running", "sleeping"):

# Path: mrq/context.py
# _GLOBAL_CONTEXT = {
# 
#     # Contains all the running greenlets for this worker. greenletid => Job object
#     "greenlets": {},
# 
#     # pointer to the current worker
#     "worker": None,
# 
#     # pointer to the current config
#     "config": {}
# }
# def setup_context(**kwargs):
# def set_current_job(job):
# def get_current_job(greenlet_id=None):
# def set_current_worker(worker):
# def get_current_worker():
# def set_logger_config():
# def set_current_config(config):
# def get_current_config():
# def retry_current_job(delay=None, max_retries=None, queue=None):
# def abort_current_job():
# def _connections_factory(attr):
#     def versiontuple(v):
# def enable_greenlet_tracing():
#     def trace(*args):
# def run_task(path, params):
# def set_current_job_progress(ratio, save=False):
# def queue_raw_jobs(*args, **kwargs):
# def queue_job(*args, **kwargs):
# def queue_jobs(*args, **kwargs):
# def metric(*args, **kwargs):


# Path: mrq/processes.py
from future.builtins import object
from .context import log
import psutil
import os
import time
import signal
import shlex
import gevent
    import subprocess32 as subprocess
    import subprocess


        if self.stopping and len(self.processes) == 0:
            self.stop_watch()

    def watch_process(self, process):
        """ Manages the status of a single process """

        status = process["psutil"].status()

        # TODO: how to avoid zombies?
        # print process["pid"], status

        if process.get("terminate"):
            if status in ("zombie", "dead"):
                process["dead"] = True
            elif process.get("terminate_at"):
                if time.time() > (process["terminate_at"] + 5):
                    log.warning("Process %s had to be sent SIGKILL" % (process["pid"], ))
                    process["subprocess"].send_signal(signal.SIGKILL)
                elif time.time() > process["terminate_at"]:
                    log.warning("Process %s had to be sent SIGTERM" % (process["pid"], ))
                    process["subprocess"].send_signal(signal.SIGTERM)

        else:
            if status in ("zombie", "dead"):
                # Restart a new process right away (TODO: sleep a bit? max retries?)
                process["dead"] = True
                self.spawn(process["command"])

            elif status not in ("running", "sleeping"):

                log.warning("Process %s was in status %s" % (process["pid"], status))