Django celery task repeats every hour, it should be executed only once per day

The course_message_schedule_task runs every day at 04:00:00. In it, we register the course_message_send_task to run at 10:00:00 every day.

However, if you look at the log below, course_message_send_task repeats every hour from 04:00:00 to 10:00:00.

(businessletter_send_task is being repeated in the same way.)

It should be run once every day, I don't know why this task is repeating.

settings

        # ...

        settings.CELERYBEAT_SCHEDULE.update(
            {
                "send-emon-data-task": {
                    "task": "klms.emon_api_task.send_emon_data_task",
                    "schedule": crontab(hour="*", minute="0"),
                    "kwargs": {},
                },
                "course-message-schedule-task": {
                    "task": "klms.tasks.course_message_schedule_task",
                    "schedule": crontab(hour="4", minute="0"),
                    "kwargs": {},
                },
                "create-coursereport-task": {
                    "task": "klms.tasks.create_coursereport_task",
                    "schedule": crontab(hour="5", minute="0"),
                    "kwargs": {},
                },
                "businessletter-schedule-task": {
                    "task": "klms.tasks.businessletter_schedule_task",
                    "schedule": crontab(hour="6", minute="0"),
                    "kwargs": {},
                },
            }
        )

        # ...

tasks

@shared_task(base=LoggedPersistOnFailureTask, default_retry_delay=30)
def course_message_send_task():
    """
    today's messages
    """
    # default 10:00:00
    time_part = settings.COURSE_MESSAGE_SENDING_SCHEDULE_TIME.split(":")
    schedule_time = localtime().replace(
        hour=int(time_part[0]),
        minute=int(time_part[1]),
        second=int(time_part[2]),
        microsecond=0,
    )

    # filter
    ids = list(
        MessageService.objects.filter(schedule=schedule_time)
        .exclude(
            status__isnull=False,
        )
        .values_list("id", flat=True)
    )

    # send!
    MessageService.status_handler(ids)


@shared_task(base=LoggedPersistOnFailureTask, default_retry_delay=30)
def course_message_schedule_task():
    """
    schedule course messages
    """
    enrollmentinfo = (
        EnrollmentInfo.objects.message_context()
        .filter(
            # 학습 시작일, n주차, 종료일, 종료 다음날
            Q(days_from_study_start=1)
            | Q(ordinal_in_week=1)
            | Q(days_to_study_end__in=[0, -1]),
            # 종료되기 전 과정
            days_to_study_end__gt=-1,
        )
        .exclude(email_notification=False, text_notification=False)
        .distinct()
    )

    messages = []

    # default 10:00:00
    time_part = settings.COURSE_MESSAGE_SENDING_SCHEDULE_TIME.split(":")
    schedule_time = localtime().replace(
        hour=int(time_part[0]),
        minute=int(time_part[1]),
        second=int(time_part[2]),
        microsecond=0,
    )

    for info in enrollmentinfo:
        # next day after course end
        if info.days_to_study_end == -1:
            if info.in_scoring:
                message_type = MessageService.STUDY_END_IN_SCORING
            else:
                message_type = MessageService.STUDY_END_FINAL

        # course end
        elif info.days_to_study_end == 0:
            message_type = MessageService.STUDY_END_TODAY

        # course start
        elif info.days_from_study_start == 1:
            message_type = MessageService.STUDY_START

        # every week
        elif info.ordinal_in_week == 1:
            message_type = MessageService.STUDY_PROGRESS

        else:
            # exam reattempt
            if info.can_reattempt:
                message_type = MessageService.EXAM_REATTEMPT

            else:
                message_type = MessageService.STUDY_PROGRESS

        messages.append(
            MessageService(
                receiver_id=info.user_id,
                message_type=message_type,
                data=MessageService.build_course_notification_data(info, message_type),
                course_id=info.course_id,
                schedule=schedule_time,
            )
        )

    if messages:
        MessageService.objects.bulk_create(messages, ignore_conflicts=True)

    # schedule
    course_message_send_task.apply_async(
        eta=schedule_time,
    )

celery log

highlighted log

2022-09-02 00:00:00,728 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[2613a8f2-35b9-4bdf-ba02-f841c2fbf8ad]
2022-09-02 00:00:00,747 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[2613a8f2-35b9-4bdf-ba02-f841c2fbf8ad] succeeded in 0.017624889500439167s: None
2022-09-02 01:00:00,064 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[c4a724a1-52f9-4eb3-80df-e796d6868c68]
2022-09-02 01:00:00,084 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[c4a724a1-52f9-4eb3-80df-e796d6868c68] succeeded in 0.018882584758102894s: None
2022-09-02 02:00:00,077 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[4d853b0e-44f0-4c34-b7f2-452c1ae40fb1]
2022-09-02 02:00:00,100 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[4d853b0e-44f0-4c34-b7f2-452c1ae40fb1] succeeded in 0.021019396372139454s: None
2022-09-02 03:00:00,027 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[772b23ab-7162-422c-8c9d-811385ffa2d2]
2022-09-02 03:00:00,053 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[772b23ab-7162-422c-8c9d-811385ffa2d2] succeeded in 0.024848720990121365s: None
2022-09-02 04:00:00,105 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.course_message_schedule_task[a4d95a0d-262d-416c-bfc4-7d8671d90df7]
2022-09-02 04:00:00,107 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: celery.backend_cleanup[299d5804-91fc-4f22-847f-6c3d892c60e5]   expires:[2022-09-02 16:00:00.106178+09:00]
2022-09-02 04:00:00,109 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[b87740f8-9a23-43f7-a240-b2af9d22eb8a]
2022-09-02 04:00:00,112 INFO 2458332 [celery.app.trace] [user None] [ip None] trace.py:125 - Task celery.backend_cleanup[299d5804-91fc-4f22-847f-6c3d892c60e5] succeeded in 0.0015913862735033035s: None
2022-09-02 04:00:00,137 INFO 2458335 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[b87740f8-9a23-43f7-a240-b2af9d22eb8a] succeeded in 0.019908799789845943s: None
2022-09-02 04:00:00,205 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae]  ETA:[2022-09-02 10:00:00+09:00]
2022-09-02 04:00:00,205 INFO 2458329 [celery_utils.logged_task] [user None] [ip None] logged_task.py:25 - Task klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae] submitted with arguments None, None
2022-09-02 04:00:00,206 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.course_message_schedule_task[a4d95a0d-262d-416c-bfc4-7d8671d90df7] succeeded in 0.10004309378564358s: None
2022-09-02 05:00:00,107 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[dac47aa6-70ba-4d0e-8201-8e8f52d8a1c6]
2022-09-02 05:00:00,111 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.create_coursereport_task[338eef97-a16f-4dca-b0e8-d584f952e3a3]
2022-09-02 05:00:00,127 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[dac47aa6-70ba-4d0e-8201-8e8f52d8a1c6] succeeded in 0.01754765957593918s: None
2022-09-02 05:00:22,235 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae]  ETA:[2022-09-02 10:00:00+09:00]
2022-09-02 05:01:59,569 INFO 2458332 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.create_coursereport_task[338eef97-a16f-4dca-b0e8-d584f952e3a3] succeeded in 119.45647316519171s: None
2022-09-02 06:00:00,108 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[75282246-2ab8-438c-a019-f517c0fd9b3d]
2022-09-02 06:00:00,110 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.businessletter_schedule_task[e564cb69-870e-461d-a34d-440fcbc4f779]
2022-09-02 06:00:00,129 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[75282246-2ab8-438c-a019-f517c0fd9b3d] succeeded in 0.018104100599884987s: None
2022-09-02 06:00:16,763 INFO 2458332 [celery_utils.logged_task] [user None] [ip None] logged_task.py:25 - Task klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228] submitted with arguments None, None
2022-09-02 06:00:16,763 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228]  ETA:[2022-09-02 11:00:00+09:00]
2022-09-02 06:00:16,764 INFO 2458332 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.businessletter_schedule_task[e564cb69-870e-461d-a34d-440fcbc4f779] succeeded in 16.653761941939592s: None
2022-09-02 06:00:22,719 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae]  ETA:[2022-09-02 10:00:00+09:00]
2022-09-02 07:00:00,103 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[acfda672-c4bc-4b70-be6d-f0d672c0d16a]
2022-09-02 07:00:00,123 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[acfda672-c4bc-4b70-be6d-f0d672c0d16a] succeeded in 0.01796681620180607s: None
2022-09-02 07:00:23,193 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae]  ETA:[2022-09-02 10:00:00+09:00]
2022-09-02 07:00:23,195 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228]  ETA:[2022-09-02 11:00:00+09:00]
2022-09-02 08:00:00,089 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[eea68791-e527-4caa-964d-5cfe090666e3]
2022-09-02 08:00:00,114 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[eea68791-e527-4caa-964d-5cfe090666e3] succeeded in 0.02293369546532631s: None
2022-09-02 08:00:23,701 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228]  ETA:[2022-09-02 11:00:00+09:00]
2022-09-02 08:00:23,703 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae]  ETA:[2022-09-02 10:00:00+09:00]
2022-09-02 09:00:00,063 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[25e8c568-6899-476b-a543-bb8c35a0ecff]
2022-09-02 09:00:00,083 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[25e8c568-6899-476b-a543-bb8c35a0ecff] succeeded in 0.018376661464571953s: None
2022-09-02 09:00:24,136 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae]  ETA:[2022-09-02 10:00:00+09:00]
2022-09-02 09:00:24,138 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228]  ETA:[2022-09-02 11:00:00+09:00]
2022-09-02 10:00:00,071 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[fa125594-d46e-46b3-a144-abe15a22eac4]
2022-09-02 10:00:00,099 INFO 2458341 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[fa125594-d46e-46b3-a144-abe15a22eac4] succeeded in 0.02654403168708086s: None
2022-09-02 10:00:00,138 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae] succeeded in 0.06554881483316422s: None
2022-09-02 10:00:00,138 INFO 2458338 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae] succeeded in 0.06552940886467695s: None
2022-09-02 10:00:00,186 INFO 2458335 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae] succeeded in 0.11421166267246008s: None
2022-09-02 10:00:00,199 INFO 2458332 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae] succeeded in 0.1270568910986185s: None
2022-09-02 10:00:00,265 INFO 2458290 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae] succeeded in 0.1922083543613553s: None
2022-09-02 10:00:00,279 INFO 2458293 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.course_message_send_task[bdfa1caf-9e08-41fa-96d8-41393521c7ae] succeeded in 0.20689951535314322s: None
2022-09-02 10:00:24,601 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228]  ETA:[2022-09-02 11:00:00+09:00]
2022-09-02 11:00:00,049 INFO 2457659 [celery.worker.strategy] [user None] [ip None] strategy.py:157 - Received task: klms.emon_api_task.send_emon_data_task[1d07a9ef-b12f-4429-865d-11be4be43329]
2022-09-02 11:00:00,214 INFO 2458293 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.emon_api_task.send_emon_data_task[1d07a9ef-b12f-4429-865d-11be4be43329] succeeded in 0.1625786107033491s: None
2022-09-02 11:00:05,216 INFO 2458329 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228] succeeded in 5.164953834377229s: None
2022-09-02 11:00:05,375 INFO 2458335 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228] succeeded in 5.32372659817338s: None
2022-09-02 11:00:05,375 INFO 2458290 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228] succeeded in 5.323759630322456s: None
2022-09-02 11:00:05,375 INFO 2458338 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228] succeeded in 5.323728712275624s: None
2022-09-02 11:00:05,522 INFO 2458332 [celery.app.trace] [user None] [ip None] trace.py:125 - Task klms.tasks.businessletter_send_task[f7124a9d-6257-4c2a-aa58-689a4cdc7228] succeeded in 5.470887743867934s: None
Back to Top