請問
我把一個\Workerman\Lib\Timer放在start_gateway.php中執(zhí)行,每兩秒執(zhí)行一次,每次執(zhí)行首先記錄一段文字到文本文件。
當我以 php start.php start 的方式運行的時候是沒有問題的。
但當我以php start.php start -d 的方式運行的時候好像計時器并沒有運行起來。
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://www.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;
// 自動加載類
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('進入計時器循環(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('訪問地址:'.$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);
}
}
我用您給的方法確實可以了,但是還有個小問題就是 bussinessWorker進程有4個,但是我只需要一個進程執(zhí)行這段代碼。也就是說我的計時器里的代碼兩秒執(zhí)行一次就可以了,在這里執(zhí)行了四次。有些冗余了。