Skip to content

Commit 8fbf235

Browse files
author
git
committed
增加celery架构和common部分
1 parent fbe1fe2 commit 8fbf235

14 files changed

+201
-0
lines changed

common/__init__.py

Whitespace-only changes.

common/base.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
########################################
5+
#
6+
# 公共模块
7+
#
8+
########################################
9+
import os
10+
import yaml
11+
import redis
12+
13+
14+
# 项目目录
15+
PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
16+
17+
# 获取配置文件信息
18+
with open(os.path.join(PROJECT_DIR, 'config', 'config.yaml')) as fd:
19+
config = yaml.load(fd)
20+
21+
redis_client = redis.Redis(host=config['redis']['host'],
22+
port=config['redis']['port'],
23+
db=config['redis']['db'],
24+
password=config['redis']['password']
25+
)
26+
27+
pool = None
28+
def get_redis_db():
29+
global pool
30+
if not pool:
31+
pool = redis.ConnectionPool(host=config['redis']['host'],
32+
port=config['redis']['port'],
33+
db=config['redis']['db'],
34+
password=config['redis']['password'])
35+
return redis.Redis(connection_pool=pool)
36+
redisdb = get_redis_db()
37+
38+

common/log.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! /usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
############################################
5+
#
6+
# 日志模块
7+
#
8+
############################################
9+
import os
10+
import sys
11+
import logging
12+
13+
DEFAULT_LOG_NAME = 'access.log'
14+
15+
def logger_init(name=None, level=logging.DEBUG):
16+
if not name:
17+
name = DEFAULT_LOG_NAME
18+
filepath = os.path.join(os.path.dirname(__file__), os.pardir, 'log', name)
19+
logger = logging.getLogger()
20+
logger.setLevel(level)
21+
ch = logging.FileHandler(filepath)
22+
formatter = logging.Formatter('[%(asctime)s] [%(filename)s:%(lineno)d] [%(levelname)s] %(message)s',
23+
datefmt='%Y-%m-%d %H:%M:%S')
24+
ch.setFormatter(formatter)
25+
logger.addHandler(ch)
26+
return logger
27+
28+
def simple_log_init(level=logging.DEBUG):
29+
log_format = '[%(asctime)s] [%(filename)s:%(lineno)d] [%(levelname)s] %(message)s'
30+
logging.basicConfig(stream=sys.stdout, level=level, format=log_format)
31+
return logging
32+
33+
logger = logger_init()
34+
35+
if __name__ == '__main__':
36+
logger = logger_init('hello.log')
37+
logger.debug('hello world')

config/config.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mysql:
2+
host: '127.0.0.1'
3+
user: root
4+
port: 3306
5+
password: '123456'
6+
db: db_test
7+
charset: utf8
8+
9+
redis:
10+
host: '127.0.0.1'
11+
port: 6379
12+
db: 1
13+
password: ''
14+
prefix: 'bd_test_'
15+

install.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
apt install -y redis-server
3+
pip install -r requirements.txt

mcelery/__init__.py

Whitespace-only changes.

mcelery/celery.conf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[program:celery]
2+
command=/root/venv3/bin/celery -A mcelery worker -B -l info
3+
directory=/root/learn-python
4+
stdout_logfile=/root/learn-python/log/celery.log
5+
autorestart=true
6+
redirect_stderr=true

mcelery/celery.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/python
2+
# -*- coding:utf-8 -*-
3+
4+
######################################
5+
# worker
6+
#
7+
######################################
8+
9+
from celery import Celery
10+
app = Celery('mcelery', include=['mcelery.tasks'])
11+
app.config_from_object('mcelery.config')
12+
13+
if __name__ == '__main__':
14+
app.start()

mcelery/config.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python
2+
# -*- coding:utf-8 -*-
3+
4+
#####################################
5+
# 配置celery的broker backend
6+
#
7+
######################################
8+
from datetime import timedelta
9+
from celery.schedules import crontab
10+
11+
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/5'
12+
BROKER_URL = 'redis://127.0.0.1:6379/6'
13+
14+
# 定时
15+
CELERY_TIMEZONE = 'Asia/Shanghai'
16+
CELERYBEAT_SCHEDULE = {
17+
'add-every-30-seconds': {
18+
'task': 'mcelery.tasks.add',
19+
'schedule': timedelta(seconds=5),
20+
'args': (16, 16)
21+
},
22+
# Executes every Monday morning at 7:30 A.M
23+
'add-every-monday-morning': {
24+
'task': 'mcelery.tasks.add',
25+
'schedule': crontab(hour=7, minute=30, day_of_week=1),
26+
'args': (5, 5),
27+
},
28+
}

mcelery/supervisor.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 安装
2+
pip2 install supervisor
3+
4+
# 配置文件
5+
echo_supervisord_conf > /etc/supervisord.conf
6+
mkdir /etc/supervisor.d
7+
在 supervisord.conf最后添加:
8+
[include]
9+
files = supervisor.d/*.conf
10+
11+
# 拷贝config文件
12+
cp mcelery/celery.conf /etc/supervisor.d/
13+
14+
# 启动supervisor服务(如果已启动勿理会)
15+
supervisord
16+
17+
# 变更配置文件之后,要加载最新的配置文件
18+
supervisorctl update
19+
20+
# 查看celery状态,是否正常启动
21+
supervisorctl status
22+
23+
# 如果有必要,重启celery
24+
supervisorctl restart celery
25+

mcelery/tasks.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python
2+
# -*- coding:utf-8 -*-
3+
4+
###################################
5+
# 任务
6+
#
7+
####################################
8+
from mcelery.celery import app
9+
10+
@app.task
11+
def add(x, y):
12+
return x + y

mcelery/trigger.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/python
2+
# -*- coding:utf-8 -*-
3+
4+
# 触发任务
5+
# 在mcelery同级目录执行 python -m mcelery.trigger
6+
7+
import time
8+
from mcelery.tasks import add
9+
10+
11+
result = add.delay(4, 4)
12+
while not result.ready():
13+
time.sleep(1)
14+
print ('task done: {0}'.format(result.get()))

requirements.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
celery==4.2.1
2+
numpy==1.16.0
3+
openpyxl==2.5.14
4+
pandas==0.24.0
5+
PyYAML==3.13
6+
redis==3.1.0

run.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+

0 commit comments

Comments
 (0)