import datetime

from platypush.cron import cron

from tests.test_cron import test_timeout, cron_queue


def make_cron_expr(cron_time: datetime.datetime):
    return '{min} {hour} {day} {month} * {sec}'.format(
        min=cron_time.minute,
        hour=cron_time.hour,
        day=cron_time.day,
        month=cron_time.month,
        sec=cron_time.second,
    )


# Use a cron expression that runs every 3 seconds to ensure it runs soon after scheduler starts
# This is more reliable than calculating a specific time at import
@cron('* * * * * */3')
def cron_test(**_):
    cron_queue.put('cron_test')


# Prepare another cronjob that should start 1hr + test_timeout/2 seconds from the application start
cron_time = datetime.datetime.now() + datetime.timedelta(
    hours=1, seconds=test_timeout / 2
)


@cron(make_cron_expr(cron_time))
def cron_1hr_test(**_):
    cron_queue.put('cron_1hr_test')
