# Path: history/utils.py
# def irc(message):
#     open(settings.PROJECT_PATH + "/log/irc", "a").write(message.strip() + "\n")


import sys
import traceback
import logging
from time import sleep
from django.conf import settings
from history.utils import irc
from operations import operations
from models import Task
                    from ipdb import post_mortem




def run_scheduler(continue_previous_tasks):
    settings.CACHE_SCRAPING = False

    if not continue_previous_tasks:
        # clean task list before starting
        Task.objects.all().delete()

    logging.info('[*] Waiting for events. To exit press CTRL+C')

    try:
        loop()
    except KeyboardInterrupt:
        pass


def loop():
    while True:
        for task in Task.objects.all():
            if task.function in operations.keys():
                logger.info("[x] Received %r, processing..." % task)
                try:
                    perform_task(task)
                except Exception, e:
                    traceback.print_exc(file=sys.stdout)
                    logger.error("/!\ %s didn't succed! Error: %s" % (task, e))
                    irc("\x034%s didn't succed! Error: %s\x03" % (task, e))

# Path: history/utils.py
# def irc(message):
#     open(settings.PROJECT_PATH + "/log/irc", "a").write(message.strip() + "\n")


# Path: scheduler/scheduler.py
import sys
import traceback
import logging
from time import sleep
from django.conf import settings
from history.utils import irc
from operations import operations
from models import Task
                    from ipdb import post_mortem





def run_scheduler(continue_previous_tasks):
    settings.CACHE_SCRAPING = False

    if not continue_previous_tasks:
        # clean task list before starting
        Task.objects.all().delete()

    logging.info('[*] Waiting for events. To exit press CTRL+C')

    try:
        loop()
    except KeyboardInterrupt:
        pass


def loop():
    while True:
        for task in Task.objects.all():
            if task.function in operations.keys():
                logger.info("[x] Received %r, processing..." % task)
                try:
                    perform_task(task)
                except Exception, e:
                    traceback.print_exc(file=sys.stdout)
                    logger.error("/!\ %s didn't succed! Error: %s" % (task, e))
                    irc("\x034%s didn't succed! Error: %s\x03" % (task, e))

                    irc("Bram: entering ipdb shell")