Esempio n. 1
0
 /**
  * 
  * @param \Sooh\Base\Log\Data $logData
  */
 public function write($logData)
 {
     $resChg = $logData->resChanged;
     $arr = $logData->toArray();
     unset($arr['resChanged']);
     unset($arr['logGuid']);
     \Sooh\DB\Cases\LogStorage::$__YMD = \Sooh\Base\Time::getInstance()->YmdFull;
     \Sooh\DB\Cases\LogStorage::$__id_in_dbByObj = 'dbgrpForLog';
     \Sooh\DB\Cases\LogStorage::$__type = 'a';
     \Sooh\DB\Cases\LogStorage::$__nSplitedBy = $this->tbSplit;
     //\Sooh\DB\Cases\LogStorage::$__fields=array(.....);
     $tmp = \Sooh\DB\Cases\LogStorage::getCopy($logData->logGuid);
     foreach ($arr as $k => $v) {
         $tmp->setField($k, $v);
     }
     $ret = $tmp->writeLog();
     if ($ret) {
         $tbSub = str_replace('_a_', '_b_', $tmp->tbname());
         foreach ($resChg as $r) {
             $r['logGuid'] = $logData->logGuid;
             try {
                 \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::tableNotExists);
                 $tmp->db()->addRecord($tbSub, $r);
             } catch (\ErrorException $e) {
                 if (\Sooh\DB\Broker::errorIs($e, \Sooh\DB\Error::tableNotExists)) {
                     $tmp->db()->ensureObj($tbSub, array('logGuid' => 'bigint unsigned not null  default 0', 'resName' => "varchar(36) not null default ''", 'resChg' => "int not null default 0", 'resNew' => "int not null default 0"));
                     $tmp->db()->addRecord($tbSub, $r);
                 } else {
                     error_log("write log failed:" . $e->getMessage() . "\n" . \Sooh\DB\Broker::lastCmd());
                 }
             }
         }
     }
 }
Esempio n. 2
0
 /**
  * 记录报表数据
  * | mainType | subType | ymd | rptdata | flg1 | flg2 | flg3 |
  * @param \Sooh\DB\Interfaces\All $db
  * @param int $ymd yyyymmdd
  * @param array $rptData
  * @param string $mainType
  * @param string $subType
  * @param array $flgs 标志位字段,不参与任何统计
  * @param array $conditionCanOverwrite 满足条件的才可以覆盖,null表示始终不能覆盖,空数组表示始终可以覆盖
  * @return boolean
  */
 public function save($db, $ymd, $rptData, $mainType, $subType, $flgs = array(), $conditionCanOverwrite = array())
 {
     $pkey = array('mainType' => $mainType, 'subType' => $subType, 'ymd' => $ymd);
     $fields = $flgs;
     if (!isset($fields['flg1'])) {
         $fields['flg1'] = 0;
     }
     if (!isset($fields['flg2'])) {
         $fields['flg2'] = 0;
     }
     if (!isset($fields['flg3'])) {
         $fields['flg3'] = 0;
     }
     $fields['rptdata'] = json_encode($rptData);
     $exists = $db->getOne($this->tbName, 'ymd', $pkey);
     if ($exists) {
         if (is_array($conditionCanOverwrite)) {
             if (empty($conditionCanOverwrite)) {
                 foreach ($pkey as $k => $v) {
                     $conditionCanOverwrite[$k] = $v;
                 }
                 $exists = $db->getOne($this->tbName, 'ymd', $conditionCanOverwrite);
                 if (!$exists) {
                     return false;
                 }
             }
             $db->updRecords($this->tbName, $fields, $pkey);
         } else {
             return false;
         }
     } else {
         try {
             \Sooh\DB\Broker::errorMarkSkip($v);
             foreach ($pkey as $k => $v) {
                 $fields[$k] = $v;
             }
             $db->addRecord($this->tbName, $fields);
         } catch (ErrorException $e) {
             if (\Sooh\DB\Broker::errorIs($e)) {
                 $this->save($db, $ymd, $mainType, $subType, $rptData);
             } else {
                 error_log($e->getMessage() . '#' . \Sooh\DB\Broker::lastCmd() . "\n" . $e->getTraceAsString());
                 return false;
             }
         }
     }
 }
Esempio n. 3
0
 public function writeLog()
 {
     try {
         \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::tableNotExists);
         $this->update();
         return true;
     } catch (\Sooh\DB\Error $e) {
         if (\Sooh\DB\Broker::errorIs($e, \Sooh\DB\Error::tableNotExists)) {
             $this->createTable();
             $this->update();
             return true;
         } else {
             error_log("ErrorOnWriteLog:" . $e->getMessage() . "\n" . \Sooh\DB\Broker::lastCmd() . "\n" . $e->getTraceAsString());
             return false;
         }
     }
 }
Esempio n. 4
0
 /**
  * 更新状态
  * @param int $ymd yyyymmdd
  * @param int $hour
  * @param string $taskid  哪个任务
  * @param string $lastStatus  本轮最后执行结果
  * @param int $isOkFinal 是否正常结束(预定的跳过也算正常)
  * @param int $isManual  是自动还是手动
  * @throws \ErrorException
  */
 public function updCrondStatus($ymd, $hour, $taskid, $lastStatus, $isOkFinal, $isManual = 0)
 {
     try {
         if (strlen($lastStatus) > 250) {
             error_log('updCrondStatus_msgTooLong:' . $lastStatus);
             $lastStatus = substr($lastStatus, 0, 250) . "...";
         }
         \Sooh\DB\Broker::errorMarkSkip();
         \Sooh\DB\Broker::getInstance($this->dbConfID)->addRecord($this->tbName, array('ymdh' => $ymd * 100 + $hour, 'taskid' => $taskid, 'lastStatus' => $lastStatus, 'ymdhis' => date('YmdHis'), 'lastRet' => $isOkFinal, 'isManual' => $isManual));
     } catch (\ErrorException $e) {
         if (\Sooh\DB\Broker::errorIs($e)) {
             \Sooh\DB\Broker::getInstance($this->dbConfID)->updRecords($this->tbName, array('lastStatus' => $lastStatus, 'ymdhis' => date('YmdHis'), 'lastRet' => $isOkFinal, 'isManual' => $isManual), array('ymdh' => $ymd * 100 + $hour, 'taskid' => $taskid));
         } else {
             throw $e;
         }
     }
 }
Esempio n. 5
0
 /**
  * 更新db里记录的每个小时执行的状态
  * @param type $taskid
  * @param type $msg
  */
 public function updCrondStatus($ymd, $hour, $taskid, $lastStatus, $isOkFinal, $isManual = 0)
 {
     if ($this->rpc !== null) {
         return $this->rpc->initArgs(array('ymd' => $ymd, 'hour' => $hour, 'taskid' => $taskid, 'lastStatus' => $lastStatus, 'isOkFinal' => $isOkFinal, 'isManual' => $isManual))->send(__FUNCTION__);
     } else {
         $db = $this->getDB();
         try {
             if (strlen($lastStatus) > 250) {
                 error_log('updCrondStatus_msgTooLong:' . $lastStatus);
                 $lastStatus = substr($lastStatus, 0, 250) . "...";
             }
             \Sooh\DB\Broker::errorMarkSkip();
             $db->addRecord($this->tbname, array('ymdh' => $ymd * 100 + $hour, 'taskid' => $taskid, 'lastStatus' => $lastStatus, 'ymdhis' => date('YmdHis'), 'lastRet' => $isOkFinal, 'isManual' => $isManual));
         } catch (\ErrorException $e) {
             if (\Sooh\DB\Broker::errorIs($e)) {
                 $db->updRecords($this->tbname, array('lastStatus' => $lastStatus, 'ymdhis' => date('YmdHis'), 'lastRet' => $isOkFinal, 'isManual' => $isManual), array('ymdh' => $ymd * 100 + $hour, 'taskid' => $taskid));
             } else {
                 throw $e;
             }
         }
     }
 }
Esempio n. 6
0
 /**
  * 
  * @param string $type [idfa|imei|md5|mac]
  * @param string $sn
  * @param string $phone
  * @param string $userIdentifier
  * @param string $contractId
  * @param array $extraData
  * @return \PrjLib\DataDig\Log\Device
  */
 public static function ensureOne($type, $sn, $phone = null, $userIdentifier = null, $contractId = null, $extraData = null)
 {
     $dt = \Sooh\Base\Time::getInstance();
     $deviceId = self::guidMaker($type, $sn);
     $ddd = \Yaf_Dispatcher::getInstance()->getRequest();
     error_log("trace device->ensure(" . $ddd->getModuleName() . '/' . $ddd->getControllerName() . '/' . $ddd->getActionName() . ") " . $deviceId . "  phone:{$phone}  user:{$userIdentifier}");
     $sys = parent::getCopy(array('deviceId' => $deviceId));
     try {
         \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::tableNotExists);
         $sys->load();
     } catch (\ErrorException $e) {
         if (\Sooh\DB\Broker::errorIs($e, \Sooh\DB\Error::tableNotExists)) {
             $sys->createTable();
         }
     }
     $fields = array('ip' => \Sooh\Base\Tools::remoteIP(), 'ymd' => $dt->YmdFull, 'hhiiss' => $dt->his);
     try {
         if ($sys->exists() === false) {
             foreach ($fields as $k => $v) {
                 $sys->setField($k, $v);
             }
             $sys->setField('phone', empty($phone) ? '0' : $phone);
             $sys->setField('userIdentifier', empty($userIdentifier) ? '' : $userIdentifier);
             $sys->setField('extraData', empty($extraData) ? '' : json_encode($extraData));
             $sys->setField('extraRet', '');
             $sys->setField('contractId', empty($contractId) ? '0' : $contractId);
             try {
                 \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::duplicateKey);
                 $sys->update();
                 $sys->flgNewCreate = true;
                 return $sys;
             } catch (\ErrorException $e) {
                 $sys->reload();
                 if ($sys->exists() === false) {
                     error_log('error create new device log:' . $e->getMessage() . "\n" . $e->getTraceAsString());
                     return $sys;
                 }
             }
         }
         $oldPhone = $sys->getField('phone', true);
         $oldUser = $sys->getField('userIdentifier', true);
         $oldContractId = $sys->getField('contractId', true);
         if (!empty($phone) && !empty($oldPhone) && $phone != $oldPhone || !empty($oldUser) && !empty($userIdentifier) && $oldUser != $userIdentifier || !empty($oldContractId) && !empty($contractId) && $oldContractId != $contractId) {
             $extraDataOld = $sys->getField('extraData', true);
             $extraRetOld = $sys->getField('extraRet', true);
             $extraRetOld = is_scalar($extraRetOld) === false ? json_encode($extraRetOld) : $extraRetOld;
             \Sooh\DB\Broker::getInstance(\PrjLib\Tbname::db_rpt)->addRecord(\PrjLib\Tbname::tb_device_log, array('deviceId' => $deviceId, 'dtChange' => $dt->YmdFull . sprintf('%06d', $dt->his), 'phoneOld' => $oldPhone, 'userIdentifierOld' => $oldUser, 'extraDataOld' => is_scalar($extraDataOld) === false ? json_encode($extraDataOld) : $extraDataOld, 'extraRetOld' => $extraRetOld, 'contractIdOld' => $oldContractId, 'phoneNew' => $phone, 'userIdentifierNew' => $userIdentifier, 'extraDataNew' => empty($extraData) ? '' : json_encode($extraData), 'extraRetNew' => $extraRetOld, 'contractIdNew' => empty($contractId) ? '0' : $contractId, 'ipOld' => $sys->getField('ip', true), 'ipNew' => $fields['ip']));
         }
         foreach ($fields as $k => $v) {
             $sys->setField($k, $v);
         }
         if (!empty($extraData)) {
             $sys->setField('extraData', empty($extraData) ? '' : json_encode($extraData));
         }
         if (!empty($phone)) {
             $sys->setField('phone', $phone);
         }
         if (!empty($userIdentifier)) {
             $sys->setField('userIdentifier', $userIdentifier);
         }
         //$sys->setField('extraRet', '');
         if (!empty($contractId)) {
             $sys->setField('contractId', $contractId);
         }
         $sys->update();
         //var_log($sys->dump(),'======================log->filled for '.$sys->tbname());
         \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::tableNotExists);
     } catch (\ErrorException $e) {
         error_log("error: on ensure-device:" . $e->getMessage() . "\n" . $e->getTraceAsString());
     }
     return $sys;
 }
Esempio n. 7
0
 /**
  * 
  * @param string $imei
  * @return int64 contractId
  */
 public static function getContractIdOfAndroid($imei)
 {
     $tmp = self::startAndroid($imei);
     try {
         \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::tableNotExists);
         $pkey = $tmp->load();
         if ($pkey) {
             $dt = \Sooh\Base\Time::getInstance()->timestamp();
             if ($dt < $tmp->getField('expired')) {
                 $contractId = $tmp->getField('contractId');
                 try {
                     $tmp->update(array($tmp, 'markInstalled'));
                     return $contractId;
                 } catch (\ErrorException $e) {
                     error_log($e->getMessage() . "\n" . $e->getTraceAsString());
                     return 0;
                 }
             } else {
                 return 0;
             }
         } else {
             return 0;
         }
     } catch (\ErrorException $e) {
         if (\Sooh\DB\Broker::errorIs($e, \Sooh\DB\Error::tableNotExists)) {
             return 0;
         } else {
             error_log($e->getMessage() . "\n" . $e->getTraceAsString());
             return 0;
         }
     }
 }