Celery를 데몬으로 구동 on Ubuntu



DJANGO X REDIS X CELERY X UBUNTU



연관된 글(딩그르르 포스트)
아래 글은 이 글의 전편이라 꼭 읽고 오셔야 해요!
아래 글은 이 포스트를 잘 이해하는데 도움이 됩니다.




1. 우리가 할려고 하는 일

리눅스가 편하신 분들은 상관없겠지만 '흰것은 글자고 검은 것은 콘솔창 배경이다..' 는 분들을 위하여 슈퍼 간결 복붙-구현 후 설명 테크를 타도록 하겠습니다. 그래도 뭘 할지는 알고 가야 겠지요? 전 편에 어떻게 Celery를 실행하는지 알아 보았습니다. 리눅스도 그와 같이 실행하면 됩니다. 근데 실행 하고 콘솔 끄면 또는 커맨드 닫으면 Celery 실행이 종료됩니다. 

그래서 우리는 Celery를 데몬으로 돌릴 것입니다. 

데몬으로 돌리는 여러 방법이 있는데요.

  • Init script로 돌리기
  • systemd 이용하기
  • Superuser Privileges를 주어 워커 구동하기
  • Supervisor 사용
  • MacOS launchd 사용

이 있습니다. 이중에 우리는 Init-Script로 구동할 예정입니다. 이유는 하단에 (빠르게, 빠르게)

Celery Daemonizing 공식문서



2. Celery Daemonizing


우리는 우선 celeryd와 celerybeat 2개를 Init-Script에 넣고 우분투가 시작 될때 자동으로 실행하도록 해 줄 것 입니다. 


$ sudo nano /etc/init.d/celeryd

** 우선 celeryd 파일을 만들어 줍시다


그런 다음,

Celery 공식 Github에 있는 init-script 에서 코드를 복사 해와서 붙여 넣습니다.


** 설정 파일을 만들어 줍니다.

$ sudo nano /etc/default/celeryd


그리고 아래와 같이 복붙 해줍니다.

# Names of nodes to start
#   most people will only start one node:
CELERYD_NODES="worker1"
#   but you can also start multiple and configure settings
#   for each in CELERYD_OPTS
#CELERYD_NODES="worker1 worker2 worker3"
#   alternatively, you can specify the number of nodes to start:
#CELERYD_NODES=10
# Absolute or relative path to the 'celery' command:
CELERY_BIN="/home/ubuntu/proj/venv/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="projectName"
# or fully qualified:
#CELERY_APP="app.tasks:app"
# Where to chdir at start.
CELERYD_CHDIR="/home/ubuntu/proj/"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=2"
# Configure node-specific settings by appending node name to arguments:
#CELERYD_OPTS="--time-limit=300 -c 8 -c:worker2 4 -c:worker3 2 -Ofair:worker1"
# Set logging level to DEBUG
#CELERYD_LOG_LEVEL="INFO"
# %n will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"

# Workers should run as an unprivileged user.
#   You need to create this user manually (or you can choose
#   a user/group combination that already exists (e.g., nobody).
CELERYD_USER="ubuntu"
CELERYD_GROUP="ubuntu"

# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1


** 주의

CELERYD_OPTS에 cocurrency가 있습니다. 동시에 몇개 작업까지 수행하느냐를 묻는데 저는 그냥 2로 합니다. 8(기본값)이면, 부하가 좀 있습니다. AWS 기준으로 Free Tier에 해당하는 머신들은 제대로 구동하지 않을 가능성이 높습니다.

해야 할일

  • CELERYD_CHDIR = Django 프로젝트 Path
  • CELERYD_USER = AWS라면 ubunutu, 또는 본인의 계정
  • CELERYD_GROUP = AWS라면 ubunutu, 또는 본인의 계정의
  • CELERY_APP = 본인 Django 프로젝트 이름
  • CELERY_BIN = 셀러리가 설치된 디렉토리(가상환경이면 그 가상환경 Path까지 다!)



$ sudo chmod 755 /etc/init.d/celeryd
$ sudo chown root:root /etc/init.d/celeryd

** Init-Script는 root로 실행해야 하기 때문에(Celery도 기본적으로 루트권한을 요구합니다) 권한 설정을 위 처럼 해 주세요.


$ sudo /etc/init.d/celeryd start 

... 
 
> Starting nodes... 
    > celery@ubuntu: OK

** 위처럼 실행 해주시면 됩니다.


** Tips:

  • 시작 : /etc/init.d/celeryd start
  • 중지 : /etc/init.d/celeryd stop
  • 재시작 : /etc/init.d/celeryd restart
  • 상태확인 : /etc/init.d/celeryd status




3. Celerybeat Daemonizing


$ sudo nano /etc/init.d/celerybeat

** 우선 celerybeat 파일을 만들어 줍시다


그런 다음,

Celery 공식 Github에 있는 init-script 에서 코드를 복사 해와서 붙여 넣습니다.


같은 세팅에서 구동된다면 Celerybeat 설정파일은 따로 없습니다. 공식문서에서 더 자세히 설명합니다.


$ sudo chmod 755 /etc/init.d/celerybeat
$ sudo chown root:root /etc/init.d/celerybeat

** 다시 권한 설정



$ sudo /etc/init.d/celerybeat start 

... 
Starting celerybeat: OK

** 실행


** Tips:

  • 시작 : /etc/init.d/celerybeat start
  • 중지 : /etc/init.d/celerybeat stop
  • 재시작 : /etc/init.d/celerybeat restart
  • 상태확인 : /etc/init.d/celerybeat status




내 Celery가 타이밍에 맞춰 돌고 있는지... 궁금해요

Celery 가 돌고 있는지 안돌고 있는지는 Redis에 가서 봐도 되고 로깅하는 기능을 넣어 DB에 로깅해도 되고 txt파일로 떨어 트려서 ElasticSearch로 분석해도 됩니다. 그리고 오류가 났을때 이메일을 보낼 수 있게 설정도 할 수 있습니다. 다른 포스트로 소개 하겠습니다.


그리고 지금 현상황에서 제대로 구동중인지 알고 싶다면 Redis에서 확인하실 수 있습니다. Redis에서 확인하시는 법은 이 글 상단에 링크된 딩그르르의 다른 포스트를 참고하시면 금방 하실수 있을겁니다. 이 글을 따라 하셨다면, Redis 인덱스는 현재 없습니다.



  • [[a.original_name]] ([[a.file_size | fileSizer]])
좋아요[[ postLike | likePlus ]]
공유
라이언

“Lead Python Engineer”

댓글 [[totalCommentCount]]
[[ comment.author__nick_name ]] [[ comment.datetime_updated | formatDate]] (수정됨)

[블라인드 처리된 글 입니다.]

답장
[[ sub.author__nick_name ]] [[ sub.datetime_created | formatDate ]] (수정됨)

취소
댓글을 남겨주세요.
'데브옵스' 관련 최신 포스트
[[ post.title ]]
[[ post.datetime_published_from | DateOnly ]]