6

I've been wondering about and searching for solutions for this and I didn't find any.

I'm running Celery in a container built with docker-compose. My container is configured like this:

celery:
  build: .
  container_name: cl01
  env_file: ./config/variables.env
  entrypoint:
    - /celery-entrypoint.sh
  volumes:
    - ./django:/django
  depends_on:
    - web
    - db
    - redis
  stop_grace_period: 1m

And my entrypoint script looks like this:

#!/bin/sh
# Wait for django
sleep 10
su -m dockeruser -c "celery -A myapp worker -l INFO"

Now, if I run docker-compose stop, I would like to have a warm (graceful) shutdown, giving Celery the provided 1 minute (stop_grace_period) to finish already started tasks. However docker-compose stop seems to kill Celery straight away. Celery should also log that it is asked to shut down gracefully, but I don't see anything but an abrupt stop to my task logs.

What am I doing wrong or what do I need to change to make Celery shut down gracefully?

edit: Suggested answer below about providing the --timeout parameter to docker-compose stop does not solve my issue.

JanBrinker
  • 1,265
  • 1
  • 16
  • 23

3 Answers3

5

You need to mark celery process with exec, this way celery process will have the same ID as docker command and docker will be able to send a SIGTERM signal to it and gracefully close celery process.

# should be the last command in script
exec celery -A myapp worker -l INFO
aiven
  • 3,262
  • 2
  • 19
  • 42
1

Via docs

Usage: stop [options] [SERVICE...]

Options:
-t, --timeout TIMEOUT      Specify a shutdown timeout in seconds (default: 10).

Try with timeout set to 60 seconds at least.

Mateusz Knapczyk
  • 266
  • 1
  • 2
  • 15
  • Doesn't seem to work. Funnily enough, Celery, Celery Beat and Flower are the first things that are shut down within seconds. nginx, django, redis etc all take their time and close after 60 seconds.. And a question: I thought this was exactly what the `stop_grace_period` parameter in the docker-compose.yml was for? So you wouldn't need to set the timeout when running the bash command. – JanBrinker Apr 07 '17 at 12:46
  • Well, yes, but you set it only for celery service, when setting timeout while running `docker-compose stop` should work for all containers. I don't know why it shuts down that way though. – Mateusz Knapczyk Apr 07 '17 at 13:54
-2

Try using this:

docker-compose down 
Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Guest
  • 1
  • Posts here are required to be in English. I "translated" (very roughly anyway) it for you this time because it was such a small part of the answer (and the rest appears to be useful) - but please keep that in mind next time you post an answer, or a question for that matter. – Zoe stands with Ukraine May 23 '19 at 19:51
  • 1
    Try to explain more clearly why this is an answer to the question. – Jeroen Heier May 23 '19 at 20:14