You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.8 KiB
PHP
102 lines
3.8 KiB
PHP
<?php
|
|
class TimerTrigger {
|
|
private $env;
|
|
private $host;
|
|
private $phpBinFile;
|
|
private $crontabLogDir;
|
|
private $ywCurl;
|
|
private $appName;
|
|
|
|
public function __construct($appName) {
|
|
$this->appName = $appName;
|
|
|
|
if (stripos(__FILE__, '/alidata/www/') !== false) {
|
|
$this->host = 'pdd.chengji-inc.com';
|
|
$this->phpBinFile = '/alidata/server/php/bin/php';
|
|
$this->crontabLogDir = '/alidata/log/crontab';
|
|
$this->env = 'live';
|
|
} else {
|
|
$this->host = 'pdd.chengji-inc.me';
|
|
$this->phpBinFile = '/usr/bin/php';
|
|
$this->crontabLogDir = '/Users/tangjianhui/Documents/log/biz/timer';
|
|
$this->env = 'dev';
|
|
}
|
|
|
|
if ($this->appName == 'pdd-od') {
|
|
$this->host = ($this->env == 'live') ? 'pdd-od.chengji-inc.com' : 'pdd-od.chengji-inc.me';
|
|
}
|
|
if ($this->appName == 'pdd-sp') {
|
|
$this->host = ($this->env == 'live') ? 'pdd-sp.chengji-inc.com' : 'pdd-sp.chengji-inc.me';
|
|
}
|
|
if ($this->appName == 'pdddz') {
|
|
$this->host = ($this->env == 'live') ? 'pdddz.jiancent.com' : 'pdddz.jiancent.me';
|
|
}
|
|
if ($this->appName == 'pdddz-sh') {
|
|
$this->host = ($this->env == 'live') ? 'sh.pdddz.jiancent.com' : 'sh.pdddz.jiancent.me';
|
|
}
|
|
if ($this->appName == 'pdddz-od') {
|
|
$this->host = ($this->env == 'live') ? 'pdddz-od.jiancent.com' : 'pdddz-od.jiancent.me';
|
|
}
|
|
if ($this->appName == 'pdddz-sp') {
|
|
$this->host = ($this->env == 'live') ? 'pdddz-sp.jiancent.com' : 'pdddz-sp.jiancent.me';
|
|
}
|
|
if ($this->appName == 'agate') {
|
|
$this->host = ($this->env == 'live') ? 'agate-pdd.ry-inc.com' : 'agate-pdd.ry-inc.com';
|
|
}
|
|
|
|
$this->host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $this->host;
|
|
|
|
Zc::import('zc.vendors.YwCurl');
|
|
$this->ywCurl = new YwCurl(array (
|
|
'useCookie' => false,
|
|
'timeOut' => 7200
|
|
));
|
|
}
|
|
|
|
private function log($content) {
|
|
$logPath = dirname(dirname(dirname(dirname(__FILE__)))) . '/log/pdd-biz/timer/';
|
|
$logFile = $logPath . 'timer_trigger.log.' . date('Y-m-d', time());
|
|
|
|
if (!file_exists($logFile)) {
|
|
is_dir($logPath) || (mkdir($logPath, 0777, true) && exec('chown -R daemon:daemon ' . $logPath));
|
|
error_log('Happy new day!', 3, $logFile);
|
|
exec('chown -R daemon:daemon ' . $logFile);
|
|
}
|
|
|
|
$content = date('Y-m-d H:i:s') . " $content\r\n";
|
|
error_log($content, 3, $logFile);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* 参考:
|
|
* https://stackoverflow.com/questions/732832/php-exec-vs-system-vs-passthru
|
|
* https://stackoverflow.com/questions/45953/php-execute-a-background-process
|
|
* eg: /alidata/server/php/bin/php /alidata/www/jmonitor/shell/batch_task_timing_task.php >> /alidata/log/crontab/batch_task_timing_task.log 2>&1 &
|
|
* @param string $dbIds
|
|
*/
|
|
public function trigger($dbIds = 'zc') {
|
|
$getActiveTimerSpecListUrl = sprintf('http://%s/%s', $this->host, 'timer/timer_trigger/get_active_timer_spec_list');
|
|
$this->log("start getTimerSpecList from [$getActiveTimerSpecListUrl], dbIds[$dbIds]");
|
|
|
|
$ret = $this->ywCurl->get($getActiveTimerSpecListUrl, array('dbIds' => $dbIds));
|
|
$timerSpecList = json_decode($ret, true);
|
|
|
|
$this->log("end getTimerSpecList count [" . count($timerSpecList) . ']');
|
|
foreach ($timerSpecList as $ts) {
|
|
if ((substr($ts['timer_code'], -4) == '.php') && file_exists(__DIR__ . '/' . $ts['timer_code'])) {
|
|
$cmd = sprintf('%s %s >> %s/%s.log 2>&1 &', $this->phpBinFile, __DIR__ . '/' . $ts['timer_code'], $this->crontabLogDir, str_replace(['/', '.'], '_', $ts['timer_code']));
|
|
} else {
|
|
$cmd = sprintf('%s %s/timer.php "%s" %s %s %s >> %s/%s.log 2>&1 &', $this->phpBinFile, __DIR__, $ts['timer_code'], $ts['wait_sleep_second'], $ts['next_sleep_second'], $this->appName, $this->crontabLogDir, str_replace('/', '_', $ts['timer_code']));
|
|
}
|
|
|
|
$output = array();
|
|
$returnVar = '';
|
|
exec($cmd, $output, $returnVar);
|
|
$this->log($cmd . ' -> ' . $returnVar);
|
|
}
|
|
}
|
|
}
|
|
|
|
|