where('autotaskstatus<', 90)->where('nexttimes<=', tostamp());
$systemrow = $db->getone($csql);
if (!is_array($systemrow))
break;
self::run($db, $systemrow);
}
}
public static function json_main() {
global $db;
//获取本地文件夹内所有.php文件
$files = glob(PATH_ROOT . 'web/admin/autotask/at_*.php');
//遍历所有文件
foreach ($files as $file) {
require $file;
clog('已加载: ' . $file);
}
set_time_limit(0);
if (isset($_SERVER['HTTP_HOST']))
ob_end_clean();
$db = new \ciy\db();
echo '
';
echo '';
echo 'AutoTask Running';
echo '';
//status: 2等待执行,3执行中,9禁止
$runid = getint('runid');
if ($runid == 0) {
clog('runid参数错误');
}
$rsuser = verifyuser();
if ($rsuser == null)
return clog('您未登录');
if (nopower($db, $rsuser['id'], 'p602r'))
return clog('您未被授权操作');
$csql = new \ciy\sql('zc_autotask');
$csql->where('id', $runid);
$taskrow = $db->getone($csql);
if (!is_array($taskrow))
return clog('任务ID不存在:' . $runid);
$runtaskid = self::run($db, $taskrow);
$csql = new \ciy\sql('zc_autotsk_run');
$csql->where('id', $runtaskid);
$tskrunrow = $db->getone($csql);
if(!is_array($tskrunrow))
return clog('任务运行记录不存在:' . $runtaskid);
$csql = new \ciy\sql('zc_autotsk_log');
$csql->where('runtaskid', $runtaskid);
$csql->column('addtimes,msg');
$tsklogrows = $db->get($csql);
$html = '';
$html .= '';
$html .= '| 任务名称 | ' . $taskrow['name'] . ' |
';
$html .= '| 入口函数 | ' . $taskrow['runfunc'] . ' |
';
$html .= '| 执行参数 | ' . $taskrow['runparam'] . ' |
';
$html .= '| 执行时长 | ' . $tskrunrow['runsec'] . 's |
';
$html .= '| 任务简报 | ' . $tskrunrow['msg'] . ' |
';
$html .= '
';
if (count($tsklogrows) > 0) {
$html .= '
';
$html .= '| 时间 | 数据 |
';
foreach ($tsklogrows as $row) {
$html .= '| ' . date('Y-m-d H:i:s', $row['addtimes']) . ' | ' . $row['msg'] . ' |
';
}
$html .= '
';
} else {
$html .= '无日志记录';
}
echo $html;
echo '';
return 'html';
}
// exit
static function run($db, $taskrow) {
if (empty($taskrow['runfunc']) || $taskrow['runfunc'][0] == '_')
return;
$sysid = $taskrow['id'];
$nexttimes = $taskrow['nexttimes'];
$runcycle = toint($taskrow['runcycle']);
while (true) {
if ($nexttimes > tostamp())
break;
if ($runcycle < 0)
$nexttimes = fixmonth(-$runcycle, $nexttimes);
else
$nexttimes += $runcycle;
}
$updata = array();
if ($taskrow['autotaskstatus'] == 30) {
$runningmsg = '任务执行中,本次跳过';
if ($taskrow['runtimes'] < tostamp() - 3600) {
$updata['autotaskstatus'] = 20;
$runningmsg = '任务执行中,超过1小时,强制复位';
}
} else {
$updata['autotaskstatus'] = 30;
$updata['runtimes'] = tostamp();
}
$updata['nexttimes'] = $nexttimes;
$csql = new \ciy\sql('zc_autotask');
$csql->where('id', $sysid);
$db->update($csql, $updata);
if ($taskrow['autotaskstatus'] == 30) {
$updata = array();
$updata['autotaskid'] = $sysid;
$updata['addtimes'] = tostamp();
$updata['runsec'] = -1;
$updata['logcnt'] = 0;
$updata['msg'] = $runningmsg;
$csql = new \ciy\sql('zc_autotsk_run');
$db->insert($csql, $updata);
return $db->insert_id();
}
$runlogstart = tostamp();
$updata = array();
$updata['autotaskid'] = $sysid;
$updata['addtimes'] = $runlogstart;
$csql = new \ciy\sql('zc_autotsk_run');
$db->insert($csql, $updata);
$runtaskid = $db->insert_id();
$taskrow['runtaskid'] = $runtaskid;
$taskrow['runtimes'] = tostamp();
$taskrow['running'] = function () use ($db, &$taskrow) {
if (time() - $taskrow['runtimes'] < 3)
return;
$taskrow['runtimes'] = tostamp();
$csql = new \ciy\sql('zc_autotask');
$csql->where('id', $taskrow['id']);
$db->update($csql, array('runtimes' => $taskrow['runtimes']));
};
$taskrow['log'] = function ($msg) use ($db, &$taskrow) {
if(empty($msg))
return;
$updata = array();
$updata['autotaskid'] = $taskrow['id'];
$updata['runtaskid'] = $taskrow['runtaskid'];
$updata['addtimes'] = tostamp();
$updata['msg'] = $msg;
$csql = new \ciy\sql('zc_autotsk_log');
$db->insert($csql, $updata);
};
$msg = '';
try {
clog($taskrow['name'] . ' 开始执行...');
if (!function_exists($taskrow['runfunc']))
$msg .= '缺少函数' . $taskrow['runfunc'];
else
$msg = call_user_func($taskrow['runfunc'], $taskrow);
clog($taskrow['name'] . ' 执行完成。
' . $msg);
} catch (\Exception $ex) {
$msg = 'try:' . print_r($ex, true);
}
$runsec = tostamp() - $runlogstart;
$updata = array();
$updata['autotaskstatus'] = 20;
$updata['runtimes'] = 0;
$csql = new \ciy\sql('zc_autotask');
$csql->where('id', $sysid);
$db->update($csql, $updata);
$csql = new \ciy\sql('zc_autotsk_log');
$csql->where('runtaskid', $runtaskid);
$logcnt = $db->get1($csql);
$updata = array();
$updata['runsec'] = $runsec;
$updata['msg'] = dbstr($msg, 200);
$updata['logcnt'] = $logcnt;
$csql = new \ciy\sql('zc_autotsk_run');
$csql->where('id', $runtaskid);
$db->update($csql, $updata);
return $runtaskid;
}
}