# Path: django_datawatch/backends/synchronous.py
# class Backend(BaseBackend):
#     def enqueue(self, slug, run_async=True):
#     def refresh(self, slug, run_async=True):
#     def run(self, slug, identifier, run_async=True, user_forced_refresh=False):
#     def _get_check_instance(self, slug):
# 
# Path: django_datawatch/datawatch.py
# class Scheduler(object):
#     def run_checks(self, force=False, slug=None):
#         """
#         :param force: <bool> force all registered checks to be executed
#         :return:
#         """
#         checks = datawatch.get_all_registered_checks()
#         last_executions = self.get_last_executions()
# 
#         for check_class in checks:
#             check = check_class()
# 
#             # only update a single slug if requested
#             if slug and check.slug != slug:
#                 continue
# 
#             # check is not meant to be run periodically
#             if check_class.run_every is None:
#                 continue
# 
#             # schedule defined in an invalid format
#             if not isinstance(check_class.run_every, crontab):
#                 logger.warning('run_every must be an instance of crontab')
#                 continue
# 
#             # shall the check be run again?
#             if not force and check.slug in last_executions:
#                 last_execution = last_executions[check.slug]
#                 if timezone.is_aware(last_execution):
#                     last_execution = last_execution.astimezone(timezone.get_current_timezone())
#                 if not check_class.run_every.is_due(last_run_at=last_execution).is_due:
#                     continue
# 
#             # enqueue the check and save execution state
#             check.run()
# 
#     def get_last_executions(self):
#         from django_datawatch.models import CheckExecution
#         return dict([(obj.slug, obj.last_run)
#                      for obj in CheckExecution.objects.all()])


from celery import shared_task
from celery.utils.log import get_task_logger
from django_datawatch.backends import synchronous
from django_datawatch.datawatch import Scheduler
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals



logger = get_task_logger(__name__)


@shared_task
def django_datawatch_enqueue(slug, *args, **kwargs):
    logger.debug('enqueuing checks for %s', slug)
    synchronous.Backend().enqueue(slug=slug)


@shared_task
def django_datawatch_refresh(slug, *args, **kwargs):
    logger.debug('refreshing check results for %s', slug)
    synchronous.Backend().refresh(slug=slug)


@shared_task
def django_datawatch_run(slug, identifier, user_forced_refresh=False, *args, **kwargs):
    logger.debug('running check %s for identifier %s (forced refresh %s)', slug, identifier, user_forced_refresh)

# Path: django_datawatch/backends/synchronous.py
# class Backend(BaseBackend):
#     def enqueue(self, slug, run_async=True):
#     def refresh(self, slug, run_async=True):
#     def run(self, slug, identifier, run_async=True, user_forced_refresh=False):
#     def _get_check_instance(self, slug):
# 
# Path: django_datawatch/datawatch.py
# class Scheduler(object):
#     def run_checks(self, force=False, slug=None):
#         """
#         :param force: <bool> force all registered checks to be executed
#         :return:
#         """
#         checks = datawatch.get_all_registered_checks()
#         last_executions = self.get_last_executions()
# 
#         for check_class in checks:
#             check = check_class()
# 
#             # only update a single slug if requested
#             if slug and check.slug != slug:
#                 continue
# 
#             # check is not meant to be run periodically
#             if check_class.run_every is None:
#                 continue
# 
#             # schedule defined in an invalid format
#             if not isinstance(check_class.run_every, crontab):
#                 logger.warning('run_every must be an instance of crontab')
#                 continue
# 
#             # shall the check be run again?
#             if not force and check.slug in last_executions:
#                 last_execution = last_executions[check.slug]
#                 if timezone.is_aware(last_execution):
#                     last_execution = last_execution.astimezone(timezone.get_current_timezone())
#                 if not check_class.run_every.is_due(last_run_at=last_execution).is_due:
#                     continue
# 
#             # enqueue the check and save execution state
#             check.run()
# 
#     def get_last_executions(self):
#         from django_datawatch.models import CheckExecution
#         return dict([(obj.slug, obj.last_run)
#                      for obj in CheckExecution.objects.all()])


# Path: django_datawatch/tasks.py
from celery import shared_task
from celery.utils.log import get_task_logger
from django_datawatch.backends import synchronous
from django_datawatch.datawatch import Scheduler

# -*- coding: UTF-8 -*-
from __future__ import unicode_literals



logger = get_task_logger(__name__)


@shared_task
def django_datawatch_enqueue(slug, *args, **kwargs):
    logger.debug('enqueuing checks for %s', slug)
    synchronous.Backend().enqueue(slug=slug)


@shared_task
def django_datawatch_refresh(slug, *args, **kwargs):
    logger.debug('refreshing check results for %s', slug)
    synchronous.Backend().refresh(slug=slug)


@shared_task
def django_datawatch_run(slug, identifier, user_forced_refresh=False, *args, **kwargs):
    logger.debug('running check %s for identifier %s (forced refresh %s)', slug, identifier, user_forced_refresh)

    synchronous.Backend().run(slug=slug, identifier=identifier, user_forced_refresh=user_forced_refresh)