陈华编程的程序已经一年多没更新了,我也一年多没写php了。最近在录一个Python的基金看板的课程,所以也想把这个运用集成到陈华编程的网站里面来。其中有一个场景,就是定时爬取基金和大盘数据,然后更新库再展示

以下是我的crontab任务调度:

* * * * * php /path/core/artisan schedule:run >> /path/www.ichenhua.cn.crontab.log 2>&1
和Laravel命令配置:
protected function schedule(Schedule $schedule)
{
   $schedule->command('fund:holds-info')->between('9:30', '15:10')->everyFiveMinutes();
   $schedule->command('fund:index-info')->between('9:30', '15:10')->everyFiveMinutes();
   $schedule->command('fund:market-info')->between('9:30', '16:30')->everyFiveMinutes();
}

功能上线没多久,就收到了CPU 100%的警告。先是心里一阵嘀咕,以为是服务器配置低,就先去给服务器升了个级,然后就正常了。但是1个多小时后,又一次收到了CPU 100%的警告,这就诡异了,就跑个采集脚本不至于啊。

于是我去查了一下日志,发现中间有这么一段:

No scheduled commands are ready to run.
No scheduled commands are ready to run.
Running scheduled command: '/www/server/php/72/bin/php' 'artisan' fund:holds-info > '/dev/null' 2>&1
Running scheduled command: '/www/server/php/72/bin/php' 'artisan' fund:holds-info > '/dev/null' 2>&1
Running scheduled command: '/www/server/php/72/bin/php' 'artisan' fund:market-info > '/dev/null' 2>&1
Running scheduled command: '/www/server/php/72/bin/php' 'artisan' fund:holds-info > '/dev/null' 2>&1
Running scheduled command: '/www/server/php/72/bin/php' 'artisan' fund:holds-info > '/dev/null' 2>&1
No scheduled commands are ready to run.
No scheduled commands are ready to run.

哦,原因是本来计划5分钟执行一次的 fund:holds-info 任务,每分钟都在执行。再一查进程,好家伙,160多个php卡死的进程,于是先用awk配合kill都给干掉:

ps -ef |grep php |awk '{print $2}'|xargs kill -9

然后把任务配置加上了withoutOverlapping,让他上一次没执行完,下一次先不要执行,问题解决。

protected function schedule(Schedule $schedule)
{
   $schedule->command('fund:holds-info')->between('9:30', '15:10')->everyFiveMinutes()->withoutOverlapping(10);
   $schedule->command('fund:index-info')->between('9:30', '15:10')->everyFiveMinutes()->withoutOverlapping(10);
   $schedule->command('fund:market-info')->between('9:30', '16:30')->everyFiveMinutes()->withoutOverlapping(10);
}

唉,太久没写代码了,开发能力下降的厉害。问题定位错误,本来不用升级服务器的,花了几百块的冤枉钱。

本文为 陈华 原创,欢迎转载,但请注明出处:http://code.ichenhua.cn/read/203