Celery 起動スクリプトを作成する

シェアする

Celery 起動スクリプトを作成します。

前回、Django と Celery で非同期処理を実装しました。今回は、Celery の CentOS 7 用の起動スクリプトを作成し、systemd で管理できるようにし、systemctl コマンドで、スタートやストップ動作をできるようにしたいと思います。

環境バージョン

  • CentOS 7.4.1708
  • Python 3.6.4
  • Django 2.0.3
  • Celery 4.2.0
  • Redis 3.2.10

インストール

Python, Django のインストール

Python と Django のインストールは、Python Web フレームワーク Django の環境を構築するの記事を参考にしてください。

Celery のインストール

$ sudo pip install celery redis

pip で Celery と redis を Python から操作するパッケージをインストールします。

Redis のインストール

$ sudo yum install -y epel-release
$ sudo yum install -y redis

Broker 用に Redis をインストールします。

実装

実装については、前回の記事、Django と Celery で非同期処理を実装するを利用します。

tree

$ tree
.
├── app
│   ├── __init__.py
│   ├── apps.py
│   ├── tasks.py
│   ├── templates
│   │   └── app
│   │       └── index.html
│   └── views.py
├── manage.py
└── proj
    ├── __init__.py
    ├── celery.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

4 directories, 11 files

起動スクリプト

実行ユーザとグループの作成

$ sudo groupadd celery
$ sudo useradd celery -g celery

Celery 実行用のグループとユーザを作成します。

ログと PID ディレクトの作成

$ sudo mkdir /var/log/celery
$ sudo chown celery:celery /var/log/celery
$ sudo mkdir /var/run/celery
$ sudo chown celery:celery /var/run/celery

Celery のログと PID 用のディレクトリを作成します。

起動スクリプトの作成

/etc/systemd/system/celery.service

[Unit]
Description=Celery Service
After=network.target

[Service]
Type=forking
User=celery
Group=celery
EnvironmentFile=/etc/sysconfig/celery
WorkingDirectory=/home/vagrant/proj
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE}'
ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'

[Install]
WantedBy=multi-user.target

起動スクリプトは、Celery のオフィシャルに記載されているサンプルをベースにしています。

オフィシャルと違うのは、以下の二箇所です。

  • EnvironmentFile
  • WorkingDirectory

EnvironmentFile

後述する Celery の設定ファイルのパスを指定します。

WorkingDirectory

Django のプロジェクトルートを指定します。

設定ファイルの作成

/etc/sysconfig/celery

# Name of nodes to start
# here we have a single node
CELERYD_NODES="w1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"

# Absolute or relative path to the 'celery' command:
CELERY_BIN="/bin/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"

# App instance to use
# comment out this line if you don't use an app
CELERY_APP="proj"
# or fully qualified:
#CELERY_APP="proj.tasks:app"

# How to call manage.py
CELERYD_MULTI="multi"

# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"

# - %n will be replaced with the first part of the nodename.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"

Celery の設定ファイルです。こちらもオフィシャルに記載しているサンプルをベースにしています。

オフィシャルと違うのは、以下の一箇所です。

  • CELERY_BIN

CELERY_BIN

Celery 実行ファイルのパスを指定します。pip でインストールした Celery の実行ファイルです。以下のコマンドで確認できます。

$ which celery
/usr/bin/celery

起動スクリプトの反映

$ sudo systemctl daemon-reload

作成した起動スクリプト /etc/systemd/system/celery.service を systemd に反映するために、上記コマンドを実行します。

動作確認

$ sudo systemctl start celery
$ sudo systemctl status celery
● celery.service - Celery Service
Loaded: loaded (/etc/systemd/system/celery.service; disabled; vendor preset: disabled)
Active: active (running) since 水 2018-07-04 07:10:11 UTC; 52min ago
Process: 3990 ExecStart=/bin/sh -c ${CELERY_BIN} multi start ${CELERYD_NODES} -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS} (code=exited, status=0/SUCCESS)
Main PID: 3999 (python3.6)
CGroup: /system.slice/celery.service
├─3999 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...
├─4003 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...
├─4004 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...
├─4005 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...
├─4006 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...
├─4007 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...
├─4008 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...
├─4009 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...
└─4010 /usr/bin/python3.6 -m celery worker -A proj --loglevel=INFO --time-limit=30...

7月 04 07:10:10 localhost.localdomain systemd[1]: Starting Celery Service...
7月 04 07:10:11 localhost.localdomain sh[3990]: celery multi v4.2.0 (windowlicker)
7月 04 07:10:11 localhost.localdomain sh[3990]: > Starting nodes...
7月 04 07:10:11 localhost.localdomain sh[3990]: > w1@localhost.localdomain: OK
7月 04 07:10:11 localhost.localdomain systemd[1]: Started Celery Service.

上記コマンドを実行し、エラーが出ず、Celery が起動していれば成功です。

まとめ

Celery 起動スクリプトを作成しました。Celery を運用していくのであれば、他のアプリケーション同様に systemd で起動や停止できるようにするべきと思います。

本記事では、CentOS 7 の systemd 起動スクリプトについて記載しましたが、オフィシャルでは、CentOS 6 の init-script 起動スクリプトについてのサンプルも記載があり、同じ感じで動かすことができます。