8、定时任务.md 9.0 KB

后台定时任务(pm2管理)

本项目部分定时任务用nodejs的pm2管理,代码位于后台CronController

(另一部分在trunk/api/protected/tasks目录,未采用pm2管理)

安装pm2

sudo npm install -g pm2

依赖于可执行文件

sudo chmod +x /data/webapps/test-shoes-admin.funfet.com/cli.php

环境注意事项

由于服务器环境配置的问题,PHP和PM2都要用sudo,否则redis扩展会读取不到

任务列表

sudo pm2 list

查看某个明细

sudo pm2 info Shoes-UpdateCommentLikeNum
tailf -n 100 ~/.pm2/logs/Shoes-UpdateCommentLikeNum-out.log
sudo pm2 log Shoes-UpdateCommentLikeNum

重启某个任务

sudo pm2 restart Shoes-UpdateCommentLikeNum

日志目录(请善用pm2 info)

/home/oujia/.pm2/logs/

日志分割

  • 安装模块

    sudo pm2 install pm2-logrotate

  • 配置分割参数

    • pm2 set pm2-logrotate:retain 50
    • pm2 set pm2-logrotate:max_size 200M
    • pm2 set pm2-logrotate:compress true
    • pm2 set pm2-logrotate:workerInterval 5
    • pm2 set pm2-logrotate:rotateModule true
    • pm2 set pm2-logrotate:rotateInterval 0 0 * * *
    • pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss
  • 停止日志切割

    sudo pm2 stop pm2-logrotate

  • 手动清理日志

    pm2 flush

保存列表快照

sudo pm2 save

恢复列表快照

sudo pm2 resurrect

干掉所有任务,重来

sudo pm2 kill

以下为测试环境(47.113.95.226)的定时任务

  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aAfterSubjectPost --env=test" --name 'Shoes-AfterSubjectPost'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aupdateSubjectLikeNum --env=test" --name 'Shoes-updateSubjectLikeNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateSubjectCmtNum --env=test" --name 'Shoes-UpdateSubjectCmtNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateSubjectShareNum --env=test" --name 'Shoes-UpdateSubjectShareNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateSubjectPopularity --env=test" --name 'Shoes-UpdateSubjectPopularity'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateSubjectViewNum --env=test" --name 'Shoes-UpdateSubjectViewNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentLikeNum --env=test" --name 'Shoes-UpdateCommentLikeNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentChildNum --env=test" --name 'Shoes-UpdateCommentChildNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentReplyNum --env=test" --name 'Shoes-UpdateCommentReplyNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentShareNum --env=test" --name 'Shoes-UpdateCommentShareNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentPopularity --env=test" --name 'Shoes-UpdateCommentPopularity'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateForumStats --env=test" --name 'Shoes-UpdateForumStats'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateHotSubject --env=test" --name 'Shoes-UpdateHotSubject'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateGameLikeNum --env=test" --name 'Shoes-UpdateGameLikeNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateBlockNum --env=test" --name 'Shoes-UpdateBlockNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aUpdateDailyPlayNum --env=test" --name 'Shoes-UpdateDailyPlayNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aClearNotices --env=test" --name 'Shoes-ClearNotices'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aStatGameuserDurNum --env=test" --name 'Shoes-StatGameuserDurNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aStatJoguserDurNum --env=test" --name 'Shoes-StatJoguserDurNum'
  • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/test-shoes-admin.funfet.com/cli.php -ccron -aStatForumuserNum --env=test" --name 'Shoes-StatForumuserNum'

正式环境(47.106.73.57)

  • PM2安装:

    参考:https://github.com/nodesource/distributions/blob/master/README.md#debinstall

    curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
    

    sudo apt-get install -y nodejs

  • 正式环境部署命令集

    PM2部署命令类似于测试环境,把命令里目录的"test-"去掉,并修改环境参数为 --env=form

    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aAfterSubjectPost --env=form" --name 'Shoes-AfterSubjectPost'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aupdateSubjectLikeNum --env=form" --name 'Shoes-updateSubjectLikeNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateSubjectCmtNum --env=form" --name 'Shoes-UpdateSubjectCmtNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateSubjectShareNum --env=form" --name 'Shoes-UpdateSubjectShareNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateSubjectPopularity --env=form" --name 'Shoes-UpdateSubjectPopularity'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateSubjectViewNum --env=form" --name 'Shoes-UpdateSubjectViewNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentLikeNum --env=form" --name 'Shoes-UpdateCommentLikeNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentChildNum --env=form" --name 'Shoes-UpdateCommentChildNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentReplyNum --env=form" --name 'Shoes-UpdateCommentReplyNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentShareNum --env=form" --name 'Shoes-UpdateCommentShareNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateCommentPopularity --env=form" --name 'Shoes-UpdateCommentPopularity'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateForumStats --env=form" --name 'Shoes-UpdateForumStats'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateHotSubject --env=form" --name 'Shoes-UpdateHotSubject'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateGameLikeNum --env=form" --name 'Shoes-UpdateGameLikeNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateBlockNum --env=form" --name 'Shoes-UpdateBlockNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aUpdateDailyPlayNum --env=form" --name 'Shoes-UpdateDailyPlayNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aClearNotices --env=form" --name 'Shoes-ClearNotices'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aStatGameuserDurNum --env=form" --name 'Shoes-StatGameuserDurNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aStatJoguserDurNum --env=form" --name 'Shoes-StatJoguserDurNum'
    • sudo pm2 start "sudo /usr/local/php/bin/php /data/webapps/shoes-admin.funfet.com/cli.php -ccron -aStatForumuserNum --env=form" --name 'Shoes-StatForumuserNum'
  • 其它优化建议

    pm2可以设置 --restart-delay <毫秒数> 来控制进程重启间隔。

    这样就可以考虑把部分PHP代码的sleep去掉