# 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 str, object
from .context import log, queue_job
import datetime
import ujson as json
import time
        self.config_tasks = config_tasks
        self.config_synced = False
        self.all_tasks = []

    def check_config_integrity(self):
        """ Make sure the scheduler config is valid """
        tasks_by_hash = {_hash_task(t): t for t in self.config_tasks}

        if len(tasks_by_hash) != len(self.config_tasks):
            raise Exception("Fatal error: there was a hash duplicate in the scheduled tasks config.")

        for h, task in tasks_by_hash.items():
            if task.get("monthday") and not task.get("dailytime"):
                raise Exception("Fatal error: you can't schedule a task with 'monthday' and without 'dailytime' (%s)" % h)
            if task.get("weekday") and not task.get("dailytime"):
                raise Exception("Fatal error: you can't schedule a task with 'weekday' and without 'dailytime' (%s)" % h)

            if not task.get("monthday") and not task.get("weekday") and not task.get("dailytime") and not task.get("interval"):
                raise Exception("Fatal error: scheduler must be specified one of monthday,weekday,dailytime,interval. (%s)" % h)

    def sync_config_tasks(self):
        """ Performs the first sync of a list of tasks, often defined in the config file. """

        tasks_by_hash = {_hash_task(t): t for t in self.config_tasks}

        for task in self.all_tasks:
            if tasks_by_hash.get(task["hash"]):
                del tasks_by_hash[task["hash"]]
            else:
                self.collection.remove({"_id": task["_id"]})

# 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/scheduler.py
from future.builtins import str, object
from .context import log, queue_job
import datetime
import ujson as json
import time

        self.config_tasks = config_tasks
        self.config_synced = False
        self.all_tasks = []

    def check_config_integrity(self):
        """ Make sure the scheduler config is valid """
        tasks_by_hash = {_hash_task(t): t for t in self.config_tasks}

        if len(tasks_by_hash) != len(self.config_tasks):
            raise Exception("Fatal error: there was a hash duplicate in the scheduled tasks config.")

        for h, task in tasks_by_hash.items():
            if task.get("monthday") and not task.get("dailytime"):
                raise Exception("Fatal error: you can't schedule a task with 'monthday' and without 'dailytime' (%s)" % h)
            if task.get("weekday") and not task.get("dailytime"):
                raise Exception("Fatal error: you can't schedule a task with 'weekday' and without 'dailytime' (%s)" % h)

            if not task.get("monthday") and not task.get("weekday") and not task.get("dailytime") and not task.get("interval"):
                raise Exception("Fatal error: scheduler must be specified one of monthday,weekday,dailytime,interval. (%s)" % h)

    def sync_config_tasks(self):
        """ Performs the first sync of a list of tasks, often defined in the config file. """

        tasks_by_hash = {_hash_task(t): t for t in self.config_tasks}

        for task in self.all_tasks:
            if tasks_by_hash.get(task["hash"]):
                del tasks_by_hash[task["hash"]]
            else:
                self.collection.remove({"_id": task["_id"]})

                log.debug("Scheduler: deleted %s" % task["hash"])