Example for timezoned beat task with Celery 4.4.0. Two ways to create datetime object inside crontab's nowfun that seemingly produce same result actually behave differentlty.
Last active
June 21, 2022 15:29
-
-
Save KristobalJunta/ad698eb492cc64ae73ca17e78caaff90 to your computer and use it in GitHub Desktop.
Example for Celery 4.4.0 with timezoned beat task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import pytz | |
from celery import Celery | |
from celery.schedules import crontab | |
app = Celery( | |
"worker", | |
broker="pyamqp://rabbitmq:5672/", | |
) | |
@app.task | |
def say_hello(): | |
print("TIME TO HELLO") | |
def nowfun(): | |
# this does not work: datetime.now(tzinfo=pytz.timezone("US/Eastern")) | |
# and this does: pytz.timezone("US/Eastern").fromutc(datetime.utcnow()) | |
return pytz.timezone("US/Eastern").fromutc(datetime.utcnow()) | |
app.conf.timezone = "UTC" | |
app.conf.beat_schedule = { | |
"say_hello": { | |
"task": "app.say_hello", | |
"schedule": crontab(hour=4, minute=30, nowfun=nowfun), | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.7" | |
services: | |
rabbitmq: | |
image: rabbitmq:3-management | |
ports: | |
- 5672:5672 | |
- 15672:15672 | |
worker: | |
image: worker | |
build: | |
context: ./ | |
dockerfile: Dockerfile | |
command: celery -A app worker -B | |
stdin_open: true | |
tty: true | |
volumes: | |
- ./src:/code | |
depends_on: | |
- rabbitmq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.7.12-bullseye | |
WORKDIR /code | |
COPY ./requirements.txt . | |
RUN python -m pip install -r requirements.txt | |
COPY app.py /code | |
CMD celery -A app worker -B |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
celery==4.4.0 | |
pytz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment