from datetime import timedelta
from random import randint

from django.db import connection
from uwsgidecoratorsfallback import cron

from ocyan.main.utils import run_exclusive


# pylint: disable=unused-argument
@cron(randint(0, 59), -1, -1, -1, -1, target="spooler")  # very hour
def publish_scheduled_pages(args):
    try:
        if not connection.is_usable():
            connection.close()
    except AttributeError:
        connection.close_if_unusable_or_obsolete()

    run_exclusive("publish_scheduled_pages", max_age=timedelta(hours=2))
