from __future__ import absolute_import, unicode_literals

import os

from celery import Celery
from celery.schedules import crontab
from datetime import timedelta

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ccvpn.settings')

app = Celery('ccvpn')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

app.conf.beat_schedule = {
    # - - - payments
    #
    # check new payments every hour to ensure progress tracking
    "payments__check_new": {
        "task": "payments.tasks.check_new",
        "schedule": timedelta(hours=1),
        "options": {"queue": "maintenance"},
    },
    # check payments that are supposed to renew soon
    "payments__check_pending_subscriptions": {
        "task": "payments.tasks.check_pending_subscriptions",
        "schedule": timedelta(hours=1),
        "options": {"queue": "maintenance"},
    },
    # check error'd payments every 12 hours to avoid them getting stuck there
    "payments__check_error": {
        "task": "payments.tasks.check_error_payments",
        "schedule": timedelta(hours=12),
        "options": {"queue": "maintenance"},
    },
    # check active subscriptions every week
    "payments__check_active_subscriptions": {
        "task": "payments.tasks.check_active_subscriptions",
        "schedule": timedelta(days=1),
        "options": {"queue": "maintenance"},
    },
    # cancel inactive payments every day
    "payments__cancel_old_payments": {
        "task": "payments.tasks.cancel_old_payments",
        "schedule": timedelta(hours=24),
        "options": {"queue": "maintenance"},
    },

    'lambdainst__resync': {
        'task': 'lambdainst.tasks.push_all_users',
        'schedule': timedelta(days=7),
    },
    'lambdainst__notify_account_expiration': {
        'task': 'lambdainst.tasks.notify_account_expiration',
        'schedule': timedelta(hours=6),
    },
    #'lambdainst__notify_vpn_auth_fails': {
    #    'task': 'lambdainst.tasks.notify_vpn_auth_fails',
    #    'schedule': timedelta(days=30),
    #},
}

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
