circus 做为批处理的守护进程

circus 是集成了zeromq,使用python编写的一个进程以及socket 管理工具,使用circus 的进程管理,我们可以用来进行批任务的
处理,同时又能保证任务的准确

项目使用docker+ docker-compose 运行

案例

  • 简单原理说明
    就是一个简单的worker 分了三个,通过circus 的进程watch,确保worker 的唯一,同时可以进行任务的状态监控

项目结构

  • 目录结构
 
├── Dockerfile
├── README.md
├── circus.ini
├── docker-compose.yaml
├── entrypoint.sh
└── webapp
    ├── info.py
    ├── info2.py
    └── info3.py
  • 代码说明
    worker: info.py info2.py info3.py
 
info.py:
#!/usr/bin/env python
def main(args):
        print(1,"this is a demo")
if __name__ == '__main__':
    main("demo")
info2.py:
#!/usr/bin/env python
def main(args):
        print(2,"this is a demo")
if __name__ == '__main__':
    main("demo")
info3.py:
#!/usr/bin/env python
def main(args):
        print(3,"this is a demo")
if __name__ == '__main__':
    main("demo")
  • circus 配置 (circus.ini)
[circus]
statsd = True
httpd = True
httpd_host = 0.0.0.0
check_delay = 5
endpoint = tcp://0.0.0.0:5555
pubsub_endpoint = tcp://0.0.0.0:5556
[watcher:webapp]
cmd = python /app/webapp/info.py
numprocesses = 1
shell = True
[watcher:webapp2]
cmd = python /app/webapp/info2.py
numprocesses = 1
shell = True
[watcher:webapp3]
cmd = python /app/webapp/info3.py
numprocesses = 1
shell = True
 
  • dockerfile
FROM dalongrong/circus:2.7-slim-stretch
WORKDIR /app
COPY circus.ini /app/
COPY webapp/ /app/webapp/
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
  • entrypoint 文件
#!/bin/sh
circusd /app/circus.ini
  • docker-compose 文件
version: "3"
services: 
  web:
    build: ./
    hostname: web
    ports: 
    - "9999:9999"
    - "8080:8080"
    - "5555:5555"

运行&&效果

  • 启动
docker-compose up -d 
  • 效果


统计监控

说明

以上只是一个简单的演示,实际上我们基于circus 的watch 特性,可以确保worker 任务的准确以及一致,使用circus
对于我们确保任务可靠是一种很不错的方案

参考资料

https://github.com/rongfengliang/circus-batch-worker-docker-compose
https://github.com/circus-tent/circus
https://circus.readthedocs.io/en/latest/for-ops/configuration/

原文地址:https://www.cnblogs.com/rongfengliang/p/10999698.html