Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ajaidanial/849bda8a9bc97c979e1a94a35e43df93 to your computer and use it in GitHub Desktop.
Save ajaidanial/849bda8a9bc97c979e1a94a35e43df93 to your computer and use it in GitHub Desktop.

Revisions

  1. @VKen VKen created this gist Sep 19, 2019.
    41 changes: 41 additions & 0 deletions docker-start-celery-multi-worker-entrypoint
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/bin/sh

    # safety switch, exit script if there's error. Full command of shortcut `set -e`
    set -o errexit
    # safety switch, uninitialized variables will stop script. Full command of shortcut `set -u`
    set -o nounset

    # tear down function
    teardown()
    {
    echo " Signal caught..."
    echo "Stopping celery multi gracefully..."

    # send shutdown signal to celery workser via `celery multi`
    # command must mirror some of `celery multi start` arguments
    celery -A config.celery_app multi stop 3 --pidfile=./celery-%n.pid --logfile=./celery-%n%I.log

    echo "Stopped celery multi..."
    echo "Stopping last waited process"
    kill -s TERM "$child" 2> /dev/null
    echo "Stopped last waited process. Exiting..."
    exit 1
    }

    # start 3 celery worker via `celery multi` with declared logfile for `tail -f`
    celery -A config.celery_app multi start 3 -l INFO -Q:1 queue1 -Q:2 queue1 -Q:3 queue3,celery -c:1-2 1 \
    --pidfile=./celery-%n.pid \
    --logfile=./celery-%n%I.log

    # start trapping signals (docker sends `SIGTERM` for shudown)
    trap teardown SIGINT SIGTERM

    # tail all the logs continuously to console for `docker logs` to see
    tail -f ./celery*.log &

    # capture process id of `tail` for tear down
    child=$!

    # waits for `tail -f` indefinitely and allows external signals,
    # including docker stop signals, to be captured by `trap`
    wait "$child"