Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose and graceful Celery shutdown

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.

like image 380
JanBrinker Avatar asked Apr 07 '17 10:04

JanBrinker


2 Answers

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
like image 120
aiven Avatar answered Nov 09 '22 19:11

aiven


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.

like image 1
Mateusz Knapczyk Avatar answered Nov 09 '22 18:11

Mateusz Knapczyk