def update_webhook_schedule():
    """
    A scheduled background job that runs every 30 minutes to
    poll the database for changes and reschedule the webhook time
    """
    session = create_session()
    all_groups: list[GroupInDB] = db.groups.get_all(session)

    for group in all_groups:

        time = cron_parser(group.webhook_time)
        job = JOB_STORE.get(group.name)

        if not job:
            logger.error(f"No job found for group: {group.name}")
            logger.info(f"Creating scheduled task for {group.name}")
            JOB_STORE.update(add_group_to_schedule(scheduler, group))
            continue

        scheduler.reschedule_job(
            job.scheduled_task.id,
            trigger="cron",
            hour=time.hours,
            minute=time.minutes,
        )

    session.close()
    logger.info(scheduler.print_jobs())
def add_group_to_schedule(scheduler, group: GroupInDB):
    cron = cron_parser(group.webhook_time)

    return {
        group.name:
        ScheduledFunction(
            scheduler,
            post_webhooks,
            cron=cron,
            name=group.name,
            args=[group.id],
        )
    }
Exemple #3
0
def init_webhook_schedule(scheduler, job_store: dict):
    session = create_session()
    all_groups: list[GroupInDB] = db.groups.get_all(session)

    for group in all_groups:
        cron = cron_parser(group.webhook_time)

        job_store.update({
            group.name:
            ScheduledFunction(
                scheduler,
                post_webhooks,
                cron=cron,
                name=group.name,
                args=[group.id],
            )
        })

    session.close()

    return job_store
Exemple #4
0
def update_webhook_schedule():
    """
    A scheduled background job that runs every 30 minutes to
    poll the database for changes and reschedule the webhook time
    """
    session = create_session()
    all_groups: list[GroupInDB] = db.groups.get_all(session)

    for group in all_groups:

        time = cron_parser(group.webhook_time)
        job = JOB_STORE.get(group.name)

        scheduler.reschedule_job(
            job.scheduled_task.id,
            trigger="cron",
            hour=time.hours,
            minute=time.minutes,
        )

    session.close()
    logger.info(scheduler.print_jobs())