請(qǐng)問(wèn)
我把一個(gè)\Workerman\Lib\Timer放在start_gateway.php中執(zhí)行,每?jī)擅雸?zhí)行一次,每次執(zhí)行首先記錄一段文字到文本文件。
當(dāng)我以 php start.php start 的方式運(yùn)行的時(shí)候是沒(méi)有問(wèn)題的。
但當(dāng)我以php start.php start -d 的方式運(yùn)行的時(shí)候好像計(jì)時(shí)器并沒(méi)有運(yùn)行起來(lái)。
start_gateway.php代碼如下
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://wtbis.cn/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;
use \TySoft\Qian;
use \TySoft\Pushlist;
use \TySoft\Queue;
use \TySoft\Log;
use \Workerman\Lib\Timer;
// 自動(dòng)加載類
require_once __DIR__ . '/../../Workerman/Autoloader.php';
Autoloader::setRootPath(__DIR__);
$gateway = new Gateway("Websocket://0.0.0.0:8400");
$gateway->name = 'EdisonWeb';
$gateway->count = 4;
$gateway->lanIp = '127.0.0.1';
$gateway->startPort = 2900;
$gateway->registerAddress = '127.0.0.1:1238';
$gateway->pingInterval = 10;
$gateway->pingNotResponseLimit = 2;
$gateway->pingData = '{"type":"ping"}';
$gateway = new Gateway("Text://0.0.0.0:8200");
$gateway->name = 'EdisonText';
$gateway->count = 4;
$gateway->lanIp = '127.0.0.1';
$gateway->startPort = 2800;
$gateway->registerAddress = '127.0.0.1:1238';
$gateway->pingInterval = 10;
$gateway->pingNotResponseLimit = 2;
$gateway->pingData = '{"type":"ping"}';
Timer::add(2, function(){
$keys=Queue::smembers('Queue');
$lasttime=time()-7200;
Log::log('進(jìn)入計(jì)時(shí)器循環(huán)');
foreach ($keys as $value) {
if(Queue::get($value.':addtime')<$lasttime){
Log::log('需要清除:'.$value);
$searchinfo=json_decode(Queue::get($value.':info'),true);
$searchid=$searchinfo;
$unrecommand_coaches=Queue::sdiff($value.':pushcoache',$value.':recommandcoache');
Pushlist::clearSearch($value,$searchid);
$callback="http://zhaosheng.maxiucai.com/api/Search/changeSearchstatus?searchid=".$searchid."&status=cancel";
Log::log('訪問(wèn)地址:'.$callback);
$response=file_get_contents($callback);
Log::log('返回內(nèi)容:'.$response);
}
}
});
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
Log.php代碼如下
<?php
namespace TySoft;
class Log{
public static function log($str){
echo $str."\n";
file_put_contents('Edison.log', date('Y-m-j G:i:s').$str."\n",FILE_APPEND);
}
}
業(yè)務(wù)邏輯都要在onXXX回調(diào)中運(yùn)行的(手冊(cè)中有強(qiáng)調(diào)過(guò)),換句話說(shuō)定時(shí)器不能在Worker::runAll();執(zhí)行前運(yùn)行,因?yàn)閃orker::runAll();執(zhí)行前運(yùn)行的代碼都是屬于主進(jìn)程的,主進(jìn)程不能有業(yè)務(wù)代碼,主進(jìn)程的業(yè)務(wù)代碼會(huì)被子進(jìn)程繼承,導(dǎo)致與預(yù)期結(jié)果不符。你的例子中可以放到onWorkerStart中運(yùn)行定時(shí)器,建議放到start_businessworker.php中$businessworker->onWorkerStart回調(diào)中。
我用您給的方法確實(shí)可以了,但是還有個(gè)小問(wèn)題就是 bussinessWorker進(jìn)程有4個(gè),但是我只需要一個(gè)進(jìn)程執(zhí)行這段代碼。也就是說(shuō)我的計(jì)時(shí)器里的代碼兩秒執(zhí)行一次就可以了,在這里執(zhí)行了四次。有些冗余了。