circus 作爲批處理的守護進程

circus 是集成了zeromq,使用python編寫的一個進程以及socket 管理工具,使用circus 的進程管理,咱們能夠用來進行批任務的
處理,同時又能保證任務的準確python

項目使用docker+ docker-compose 運行git

案例

  • 簡單原理說明
    就是一個簡單的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 
  • 效果

 


統計監控github

 

說明

以上只是一個簡單的演示,實際上咱們基於circus 的watch 特性,能夠確保worker 任務的準確以及一致,使用circus
對於咱們確保任務可靠是一種很不錯的方案web

參考資料

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

相關文章
相關標籤/搜索