public static function save($data, $id = 0) { if (0 == $id) { $path = $data['path']; $managerId = $data['manager_id']; // 判断manager是否存在 $managerInfo = RootManagerInterface::getById(array('id' => $managerId)); if (empty($managerInfo)) { throw new InterfaceException('管理员不存在!'); } // 判断路径是否存在 if (!RootPermissionInterface::findPath(array('path' => $path))) { throw new InterfaceException('路径不存在!'); } // 判断是否已经添加 $check = RootManagerInterface::checkPermission(array('id' => $managerId, 'path' => $path)); if ($check) { return 0; } $trans = new Trans(DbConfig::$SERVER_TRANS); $trans->begin(); $model = new RootRelationModel($trans); // 删除重复权限 $dir = rtrim($path, '/') . '/'; $where = array(array('manager_id', '=', $managerId), array('path', 'LIKE', "{$dir}%")); $model->delete($where); $insertData = $data; $id = $model->insert($insertData); $trans->commit(); self::syncToRedis($managerId); return $id; } else { $model = new RootRelationModel(); $updateData = $data; $affects = $model->updateById($id, $updateData); return $affects; } }
public static function deleteById($id) { $managerInfo = self::getById($id); if (empty($managerInfo)) { return 0; } // 发起事务 $trans = new Trans(DbConfig::$SERVER_TRANS); $trans->begin(); $model = new RootManagerModel($trans); // 删除管理员权限 -> 删除管理员 RootRelationInterface::deleteByManagerId(array('manager_id' => $id, 'trans' => $trans)); $affects = $model->deleteById($id); // 删除redis中的数据 $redis = RedisClient::getInstance(RedisConfig::$SERVER_COMMON); // 删除path $key = RedisKeys::ROOT_PATH_SET_ . $id; $redis->delete($key); // 删除user_id $key = RedisKeys::ROOT_ENABLED_HASH; $redis->hDel($key, $managerInfo['user_id']); $trans->commit(); return $affects; }
*/ require_once __DIR__ . '/../../../bootstrap.php'; // 获取命令行参数 $solutionId = $argv[1]; if (empty($solutionId)) { Logger::error('judge', '缺少参数:$solutionId'); exit(1); } // 从评判队列中获取该条信息 $queueInfo = OjJudgeInterface::getRow(array('where' => array(array('solution_id', '=', $solutionId)))); if (empty($queueInfo)) { Logger::error('judge', "评判队列中不存在Solution!solutionId={$solutionId}"); exit(1); } $trans = new Trans(DbConfig::$SERVER_TRANS); $trans->begin(); // 保存result try { OjSolutionInterface::save(array('id' => $solutionId, 'time_cost' => $queueInfo['time_cost'], 'memory_cost' => $queueInfo['memory_cost'], 'judge_time' => $queueInfo['judge_time'], 'run_id' => $queueInfo['id'], 'result' => $queueInfo['result'], 'trans' => $trans)); } catch (Exception $e) { Logger::error('judge', "solutionId={$solutionId},保存solution失败," . $e->getMessage()); $trans->rollback(); throw $e; } // 保存log try { OjSolutionLogInterface::save(array('trans' => $trans, 'solution_id' => $solutionId, 'ce' => $queueInfo['ce'], 're' => $queueInfo['re'], 'detail' => $queueInfo['detail'])); } catch (Exception $e) { Logger::error('judge', "solutionId={$solutionId},保存log失败," . $e->getMessage()); $trans->rollback(); throw $e;
/** * 同步数据到oj_solution * * @throws Exception */ public function sync() { $solutionInfo = OjSolutionInterface::getById(array('id' => $this->solutionId)); if (empty($solutionInfo)) { Logger::error('judge', "SOLUTION_ID:{$this->solutionId},Solution不存在!"); throw new Exception("SOLUTION_ID:{$this->solutionId},Solution不存在!"); } if ($solutionInfo['remote'] != StatusVars::REMOTE_POJ) { Logger::error('judge', "SOLUTION_ID:{$this->solutionId},必须是POJ的Solution!"); throw new Exception("SOLUTION_ID:{$this->solutionId},必须是POJ的Solution!"); } if (empty($solutionInfo['run_id'])) { Logger::error('judge', "RUN_ID为0!"); throw new Exception("SOLUTION_ID:{$this->solutionId},RUN_ID为0!"); } if ($solutionInfo['remote_uid'] != -1 && $solutionInfo['remote_uid'] != $this->uid) { Logger::error('judge', "SOLUTION_ID:{$this->solutionId},当前uid不等于remote_uid,无法获取其他用户的状态!"); throw new Exception("SOLUTION_ID:{$this->solutionId},当前uid不等于remote_uid,无法获取其他用户的状态!"); } // 尝试多次获取结果 $rowInfo = array(); $i = 1; while ($i <= 10) { $rowInfo = $this->getResult($solutionInfo['run_id']); if (false === $rowInfo || in_array($rowInfo['result'], StatusVars::$pojResultMap)) { break; } $i > 5 ? sleep(2) : usleep(500000); $i++; } // 获取获取结果超时 if (false === $rowInfo) { Logger::info('judge', "SOLUTION_ID:{$this->solutionId},获取结果失败2,写入TIME_OUT"); $data = array('id' => $this->solutionId, 'result' => StatusVars::TIME_OUT, 'time_cost' => 0, 'memory_cost' => 0); OjSolutionInterface::save($data); } else { Logger::info('judge', "SOLUTION_ID:{$this->solutionId},POJ远程judge成功!尝试次数:{$i}"); $trans = new Trans(DbConfig::$SERVER_TRANS); $trans->begin(); $data = array('trans' => $trans, 'id' => $this->solutionId, 'result' => $rowInfo['result'], 'time_cost' => $rowInfo['time_cost'], 'memory_cost' => $rowInfo['memory_cost']); OjSolutionInterface::save($data); // 保存Log $dataLog = array('trans' => $trans, 'solution_id' => $this->solutionId, 'ce' => Arr::get('ce', $rowInfo['judge_log'], ''), 're' => Arr::get('re', $rowInfo['judge_log'], '')); OjSolutionLogInterface::save($dataLog); $trans->commit(); } }
private static function updateResult($id, $result, $trans = null) { $solutionInfo = self::getById($id); if (empty($solutionInfo)) { throw new InterfaceException('solution不存在!'); } $innerTrans = $trans; if (null == $trans) { $innerTrans = new Trans(DbConfig::$SERVER_TRANS); $innerTrans->begin(); } $solutionModel = new OjSolutionModel($innerTrans); $remote = $solutionInfo['remote']; $globalId = $solutionInfo['problem_global_id']; $userId = $solutionInfo['user_id']; $contestId = $solutionInfo['contest_id']; // 如果状态从 非AC -> AC,那么修改user表和problem表,竞赛状态下不改变 if (0 == $contestId && $result == StatusVars::ACCEPTED && $solutionInfo['result'] != StatusVars::ACCEPTED) { // 判断是否没有AC过 $where = array(array('problem_global_id', '=', $globalId), array('user_id', '=', $userId), array('result', '=', StatusVars::ACCEPTED), array('contest_id', '=', 0)); $count = self::getCount($where); if (0 == $count) { $userInfo = UserCommonInterface::getById(array('id' => $userId)); $problemInfo = OjProblemInterface::getById(array('id' => $globalId)); $remoteStr = strtolower(StatusVars::$REMOTE_SCHOOL[$remote]); UserCommonInterface::save(array('trans' => $innerTrans, 'id' => $userId, 'solved_all' => $userInfo['solved_all'] + 1, "solved_{$remoteStr}" => $userInfo["solved_{$remoteStr}"] + 1)); OjProblemInterface::save(array('trans' => $innerTrans, 'id' => $globalId, 'solved' => $problemInfo['solved'] + 1)); } } // 如果状态从 AC -> 非AC,那么修改user表和problem表,竞赛状态下不改变 if (0 == $contestId && $result != StatusVars::ACCEPTED && $solutionInfo['result'] == StatusVars::ACCEPTED) { // 判断是否只AC过一次 $where = array(array('problem_global_id', '=', $globalId), array('user_id', '=', $userId), array('result', '=', StatusVars::ACCEPTED), array('contest_id', '=', 0)); $count = self::getCount($where); if (1 == $count) { $userInfo = UserCommonInterface::getById(array('id' => $userId)); $problemInfo = OjProblemInterface::getById(array('id' => $globalId)); $remoteStr = strtolower(StatusVars::$REMOTE_SCHOOL[$remote]); UserCommonInterface::save(array('trans' => $innerTrans, 'id' => $userId, 'solved_all' => $userInfo['solved_all'] - 1, "solved_{$remoteStr}" => $userInfo["solved_{$remoteStr}"] - 1)); OjProblemInterface::save(array('trans' => $innerTrans, 'id' => $globalId, 'solved' => $problemInfo['solved'] - 1)); } } // 更新solution $affects = $solutionModel->updateById($id, array('result' => $result)); if (null == $trans && null != $innerTrans) { // 没有外部事务,并且存在内部事务 $innerTrans->commit(); } return $affects; }