Skip to content

Instantly share code, notes, and snippets.

@KristobalJunta
Last active June 21, 2022 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KristobalJunta/ad698eb492cc64ae73ca17e78caaff90 to your computer and use it in GitHub Desktop.
Save KristobalJunta/ad698eb492cc64ae73ca17e78caaff90 to your computer and use it in GitHub Desktop.
Example for Celery 4.4.0 with timezoned beat task

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.

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),
}
}
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
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
celery==4.4.0
pytz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment