使用supervisor 启动 celery 并根据不同的环境需求,配置环境变量

以为在做公司项目,所以当然有开发环境和生产环境。我们公司使用 Jenkins来实现自动部署,将 Jenkins 生成的 docker部署到AWS上.

我们项目使用supervisor来启动服务。

但是由于同一个项目要做开发环境进行开发测试,然后再能部署到生产环境,而且我们公司的开发环境和生产环境是物理隔离的,所以必须要有两套不一样的环境。

查了很久 最终还是在官网上查到了配置方法。(不过官网文档有些混乱 等我想第二次查找时 已经找不到了。)

不多说 直接上代码

在主要的task 文件中需要加上默认的环境配置

from celery import Celery
import os

#: Set default configuration module name
os.environ.setdefault('CELERY_CONFIG_MODULE', 'celerytask.celeryconfig')
celery_environment =os.environ.setdefault('CELERY_CONFIG_MODULE', 'celerytask.celeryconfig')

if celery_environment=="celerytask.celeryconfig-dev":
    rw.set_conf_name("develop")
app = Celery()
app.config_from_envvar('CELERY_CONFIG_MODULE')


@app.task()
def run_algorithm(a, b):
    return a+b

然后就是两个不一样的配置文件 celeryconfig.py 以及celeryconfig-dev.py (这个名字随意起)

 

celeryconfig.py (prod 环境的 配置文件)

from kombu import Exchange, Queue

BROKER_URL = '你的 prod BROKER_URL'
RESULT_BACKEND = '你的 prod RESULT_BACKEND'

# defintion a exchange
default_exchange = Exchange('dedfault', type='direct')
my_exchange = Exchange('routeExchange', type='direct')

#一些其他配置参数

CELERY_QUEUES = (
    Queue('default', default_exchange, routing_key='default'),
    Queue('routeAlgorithm', my_exchange, routing_key='routeAlgorithm')
)


CELERY_DEFAULT_QUEUE = 'default'
CELERY_DEFAULT_EXCHANGE = 'default'
CELERY_DEFAULT_ROUTING_KEY = 'default'

CELERY_ROUTES = (
    {'tasks.run_algorithm': {
        'queue': 'routeAlgorithm',
        'routing_key': 'routeAlgorithm'
    }
    },
)

CELERY_IMPORTS = ("tasks",)

celeryconfig-dev.py (dev 环境的 配置文件) 

from kombu import Exchange, Queue

BROKER_URL = '你的 dev BROKER_URL'
RESULT_BACKEND = '你的 dev RESULT_BACKEND'

# defintion a exchange
default_exchange = Exchange('dedfault', type='direct')
my_exchange = Exchange('routeExchange', type='direct')

#一些其他配置参数

CELERY_QUEUES = (
    Queue('default', default_exchange, routing_key='default'),
    Queue('routeAlgorithm', my_exchange, routing_key='routeAlgorithm')
)


CELERY_DEFAULT_QUEUE = 'default'
CELERY_DEFAULT_EXCHANGE = 'default'
CELERY_DEFAULT_ROUTING_KEY = 'default'

CELERY_ROUTES = (
    {'tasks.run_algorithm': {
        'queue': 'routeAlgorithm',
        'routing_key': 'routeAlgorithm'
    }
    },
)

CELERY_IMPORTS = ("tasks",)

 

 

这里是 Dev环境中需要启动的 supervisord.conf

[supervisord]
nodaemon=true
[program:nginx]
command=/usr/sbin/nginx
[program:uwsgi]
command =/usr/local/bin/uwsgi --ini  /var/www/app/conf/uwsgi-dev.ini

[program:celery]
#Set full path to celery program if using virtualenv
command=celery worker -A tasks --loglevel=INFO
directory=/var/www/app/celerytask
environment=CELERY_CONFIG_MODULE="celerytask.celeryconfig-dev"

autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600

下面是 prod环境中的 supervisord.conf

[supervisord]
nodaemon=true

[program:nginx]
command=/usr/sbin/nginx

[program:uwsgi]
command =/usr/local/bin/uwsgi --ini  /var/www/app/conf/uwsgi.ini

[program:celery]
; Set full path to celery program if using virtualenv
command=celery worker -A tasks --loglevel=INFO

directory=/var/www/app/celerytask
environment=CELERY_CONFIG_MODULE="celerytask.celeryconfig"

autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600

唯一的区别就是  在这里改成你项目的配置文件名,分清不同环境。

environment=CELERY_CONFIG_MODULE="celerytask.celeryconfig"

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值