Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The last_run_at time is always in UTC, which differs from my local configuration time. #759

Open
dxygit1 opened this issue Jun 18, 2024 · 3 comments

Comments

@dxygit1
Copy link

dxygit1 commented Jun 18, 2024

The last_run_at time is always in UTC, which differs from my local configuration time.
This causes tasks to still run despite being expired.
image

version

celery                  5.4.0
django-celery-beat      2.6.0
django-celery-results   2.5.1

config

USE_TZ = False

BROKER_URL = 'redis://:Password@[email protected]:6379/0'

RESULT_BACKEND  = 'redis://:Password@[email protected]:6379/0'


ACCEPT_CONTENT = ['json']
TASK_SERIALIZER = 'json'

RESULT_SERIALIZER = 'json'

TASK_RESULT_EXPIRES = 60 * 60 * 24

TIMEZONE = 'Asia/Shanghai'
CELERYBEAT_SCHEDULER = 'django_celery_beat.schedulers.DatabaseScheduler'

WORKER_CONCURRENCY = 20

WORKER_PREFETCH_MULTIPLIER = 20

WORKER_MAX_TASKS_PER_CHILD = 100

WORKER_DISABLE_RATE_LIMITS = True

ENABLE_UTC = False
DJANGO_CELERY_BEAT_TZ_AWARE = False
TASK_TIME_LIMIT = 30 * 60

Execute tasks.

from django_celery_beat.models import PeriodicTask, IntervalSchedule
schedule, created = IntervalSchedule.objects.get_or_create(
    every=10,
    period=IntervalSchedule.SECONDS,
)
from datetime import datetime, timedelta
expires=datetime.now() + timedelta(seconds=30)
periodic_task = PeriodicTask.objects.create(
    interval=schedule,  # 我们上面创建的调度
    name='task1',  # 唯一的任务名称
    task='task1',  # 任务的导包路径
    expires=expires,  # 任务的过期时间
)
@dxygit1
Copy link
Author

dxygit1 commented Jun 18, 2024

I only get the correct behavior when I set USE_TZ to True, but I don't want to change USE_TZ to True.

@ThankCat
Copy link

➕1️⃣ I have a same question.
Configuration in my setting.py file.

TIME_ZONE = "Asia/Shanghai"
USE_TZ = False
CELERY_TIMEZONE = TIME_ZONE
CELERY_ENABLE_UTC = False
DJANGO_CELERY_BEAT_TZ_AWARE = False
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"
def post(self, request: Request, *args, **kwargs):
        PeriodicTask.objects.all().delete()
        # 创建 IntervalSchedule
        interval_schedule, created = IntervalSchedule.objects.get_or_create(
            every=5, period=IntervalSchedule.SECONDS
        )
        # 计算结束时间
        end_time = datetime.now() + timedelta(seconds=30)

        # 创建PeriodicTask
        periodic_task = PeriodicTask(
            interval=interval_schedule,
            name=f"periodic_task_{timezone.now()}",
            task="publish.tasks.periodic_tasks",
            args=json.dumps([1, 2]),
            expires=end_time,
        )
        periodic_task.save()

        return ApiResponse()

expires this param is invalid

@ChanXing2023
Copy link

only support USE_TZ = True, to work properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants