codo-cron 本地部署方式



  • 定时任务

    定时任务模块,定时任务完全兼容crontab,支持到秒级,本章介绍使用pm2守护本地codo_cron程序

    前言

    基于Tornado开发一套定时任务模块,支持到秒级,由于之前都是容器运行的cron可能会导致很多人不理解我想要运行定时任务脚本怎么办?脚本里面关联的文件路径、import模块等各种问题,想对复杂的使用建议部署在单独一台机器上非Docker运行

    下载代码

    echo -e "\033[32m [INFO]: codo_cron(定时任务) Start install. \033[0m"
    if ! which wget &>/dev/null; then yum install -y wget >/dev/null 2>&1;fi
    if ! which git &>/dev/null; then yum install -y git >/dev/null 2>&1;fi
    [ ! -d /opt/codo/ ] && mkdir -p /opt/codo
    cd /opt/codo && git clone https://github.com/opendevops-cn/codo-cron.git
    cd codo-cron
    

    修改配置

    #导入环境变量文件,最开始准备的环境变量文件
    source /opt/codo/env.sh
    #后端数据库名称,建议不要修改,初始化data.sql已经指定了数据库名字,若需改请一块修改
    CRON_DB_DBNAME='codo_cron' 
    
    sed -i "s#cookie_secret = .*#cookie_secret = '${cookie_secret}'#g" settings.py
    
    #mysql配置
    sed -i "s#DEFAULT_DB_DBHOST = .*#DEFAULT_DB_DBHOST = os.getenv('DEFAULT_DB_DBHOST', '${DEFAULT_DB_DBHOST}')#g" settings.py
    sed -i "s#DEFAULT_DB_DBPORT = .*#DEFAULT_DB_DBPORT = os.getenv('DEFAULT_DB_DBPORT', '${DEFAULT_DB_DBPORT}')#g" settings.py
    sed -i "s#DEFAULT_DB_DBUSER = .*#DEFAULT_DB_DBUSER = os.getenv('DEFAULT_DB_DBUSER', '${DEFAULT_DB_DBUSER}')#g" settings.py
    sed -i "s#DEFAULT_DB_DBPWD = .*#DEFAULT_DB_DBPWD = os.getenv('DEFAULT_DB_DBPWD', '${DEFAULT_DB_DBPWD}')#g" settings.py
    sed -i "s#DEFAULT_DB_DBNAME = .*#DEFAULT_DB_DBNAME = os.getenv('DEFAULT_DB_DBNAME', '${CRON_DB_DBNAME}')#g" settings.py
    
    #只读MySQL配置
    sed -i "s#READONLY_DB_DBHOST = .*#READONLY_DB_DBHOST = os.getenv('READONLY_DB_DBHOST', '${READONLY_DB_DBHOST}')#g" settings.py
    sed -i "s#READONLY_DB_DBPORT = .*#READONLY_DB_DBPORT = os.getenv('READONLY_DB_DBPORT', '${READONLY_DB_DBPORT}')#g" settings.py
    sed -i "s#READONLY_DB_DBUSER = .*#READONLY_DB_DBUSER = os.getenv('READONLY_DB_DBUSER', '${READONLY_DB_DBUSER}')#g" settings.py
    sed -i "s#READONLY_DB_DBPWD = .*#READONLY_DB_DBPWD = os.getenv('READONLY_DB_DBPWD', '${READONLY_DB_DBPWD}')#g" settings.py
    sed -i "s#READONLY_DB_DBNAME = .*#READONLY_DB_DBNAME = os.getenv('READONLY_DB_DBNAME', '${CRON_DB_DBNAME}')#g" settings.py
    

    拷贝到/var/www/codo-cron/

    我习惯性的将本地部署的程序都放在这里,具体看你习惯

    rsync -avz /opt/codo/codo-cron/ /var/www/codo-cron
    

    安装依赖

     pip3 install -U git+https://github.com/ss1917/ops_sdk.git
     pip3 install -r /var/www/codo-cron/doc/requirements.txt
    

    创建数据库

    mysql -h127.0.0.1 -uroot -p${MYSQL_PASSWORD}
    create database `codo_cron` default character set utf8mb4 collate utf8mb4_unicode_ci;
    

    初始化数据

    docker exec -ti codo-cron_codo_cron_1  /usr/local/bin/python3 /var/www/codo-cron/db_sync.py
    

    安装守护程序pm2

    很多人不明白为什么python也用pm2守护,因为我想用pm2 不想用supervisord了,哈哈哈哈

    直接给你脚本,又不会太麻烦了

    #!/bin/bash
    [ -f /usr/local/bin/node ] && echo "Node already exists" && exit -1
    source /root/.public
    cd /usr/local/src && rm -rf node-v8.11.3-linux-x64.tar.xz
    wget -q -c https://nodejs.org/dist/v8.11.3/node-v8.11.3-linux-x64.tar.xz
    tar xf node-v8.11.3-linux-x64.tar.xz -C  /usr/local/ >/dev/null 2>&1
    rm -rf /usr/local/bin/node
    rm -rf /usr/local/bin/npm
    ln -s /usr/local/node-v8.11.3-linux-x64/bin/node /usr/local/bin/node
    ln -s /usr/local/node-v8.11.3-linux-x64/bin/node /usr/bin/node
    ln -s /usr/local/node-v8.11.3-linux-x64/bin/npm  /usr/local/bin/npm
    ln -s /usr/local/node-v8.11.3-linux-x64/bin/npm  /usr/bin/npm
    /usr/local/bin/node -v
    /usr/local/bin/npm -v
    sudo npm i -g pm2 >/dev/null 2>&1
    ln -s /usr/local/node-v8.11.3-linux-x64/bin/pm2 /usr/bin/
    
    

    部署启动

    pm2.json文件

    • 单进程
    • 端口默认9900(修改的话一定要和网关注册对应起来)
    {
      "apps": [{
        "name": "codo-cron",
        "script": "python3 startup.py --service=cron --port=9900",
        "cwd": "/var/www/codo-cron/",
        "max_memory_restart": "500M",
        "log_date_format"  : "YYYY-MM-DD HH:mm Z",
        "log_file"   : "/var/log/supervisor/cron.log",
        "autorestart": true,
        "node_args": [],
        "args": [],
        "env": {
        }
      }]
    }
    
    
    pm2 start pm2.json  #单进程即可
    $ pm2 list
    ┌───────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬──────┬───────────┬──────┬──────────┐
    │ App name  │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu  │ mem       │ user │ watching │
    ├───────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼──────┼───────────┼──────┼──────────┤
    │ codo-cron │ 0  │ N/A     │ fork │ 17664 │ online │ 0      │ 3h     │ 0.7% │ 42.3 MB   │ root │ disabled │
    └───────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴──────┴───────────┴──────┴──────────┘
     Use `pm2 show <id|name>` to get more details about an app
    

登录后回复