112 lines
3.1 KiB
PHP
112 lines
3.1 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 19/feb/2016
|
|
*/
|
|
|
|
class FDN2CBS_DataStorage extends CBSClient_DataStorageBase{
|
|
/**
|
|
* Restituisce un mongoID (stringa)
|
|
* @see CBSClient_DataStorageBase::getNextOperationId()
|
|
*/
|
|
public function getNextOperationId(){
|
|
$rval = 1;
|
|
$conf = GlobalVariables::get("dao")->getFirst("ConfigModel",array("name"=>"cbsQueue"));
|
|
if (!is_null($conf)){
|
|
$rval = $conf->lastOperationId+1;
|
|
}
|
|
return $rval;
|
|
|
|
// $cur = GlobalVariables::get("dao")->getFirst("CBSQueueModel",array(),array("sort"=>"operationId"));
|
|
// if (is_null($cur)){
|
|
// return 1;
|
|
// }
|
|
// else {
|
|
// return $cur->operationId+1;
|
|
// }
|
|
}
|
|
/**
|
|
* (non-PHPdoc)
|
|
* @see CBSClient_DataStorageBase::setMaxOperationId()
|
|
*/
|
|
public function setMaxOperationId($id){
|
|
$conf = GlobalVariables::get("dao")->getFirst("ConfigModel",array("name"=>"cbsQueue"));
|
|
if (is_null($conf)){
|
|
$obj = new stdClass();
|
|
$obj->name="cbsQueue";
|
|
$conf = new ConfigModel($obj);
|
|
}
|
|
$conf->lastOperationId = $id;
|
|
GlobalVariables::get("dao")->save($conf);
|
|
}
|
|
|
|
/**
|
|
* @see CBSClient_DataStorageBase::queueUncompletedRequest()
|
|
*/
|
|
public function queueUncompletedRequest($id, RAPI_RequestDetails $req){
|
|
$queueFolder = GlobalVariables::get("config")->paths->cbsQueue;
|
|
$obj = new stdClass();
|
|
$obj->operationId = $id;
|
|
$obj->retry = 0;
|
|
$queueObj = new CBSQueueModel($obj);
|
|
GlobalVariables::get("dao")->save($queueObj);
|
|
|
|
if (!SFSManager::fileExists($queueFolder)){
|
|
SFSManager::createDirectory($queueFolder);
|
|
}
|
|
$path = $queueFolder."/".$queueObj->id;
|
|
|
|
SFSManager::writeFile(serialize($req), $path);
|
|
}
|
|
|
|
/**
|
|
* @see CBSClient_DataStorageBase::getUncompletedRequests()
|
|
*/
|
|
public function getUncompletedRequests($limit = null){
|
|
$rval = array();
|
|
|
|
$basePath = GlobalVariables::get("config")->paths->cbsQueue;
|
|
|
|
$opt = array("sort"=>array("retry"=>1));
|
|
if (!is_null($limit)){
|
|
$opt["limit"] = $limit;
|
|
}
|
|
|
|
$requests = GlobalVariables::get("dao")->query("CBSQueueModel", array(), $opt);
|
|
if (sizeof($requests)>0){
|
|
foreach ($requests as $request){
|
|
$path = $basePath."/".$queueObj->id;
|
|
$cont = implode("",SFSManager::readFile($path));
|
|
$rval[] = unserialize($cont);
|
|
}
|
|
}
|
|
return $rval;
|
|
}
|
|
|
|
/**
|
|
* @see CBSClient_DataStorageBase::onRequestCompletedFromQueue()
|
|
*/
|
|
public function onRequestCompletedFromQueue($id){
|
|
$queueObj = GlobalVariables::get("dao")->getFirst("CBSQueueModel", array("operationId"=>$id));
|
|
|
|
$path = GlobalVariables::get("config")->paths->cbsQueue."/".$queueObj->id;
|
|
SFSManager::deleteFile($path);
|
|
GlobalVariables::get("dao")->delete($queueObj);
|
|
}
|
|
|
|
/**
|
|
* @see CBSClient_DataStorageBase::onRequestFailedFromQueue()
|
|
*/
|
|
public function onRequestFailedFromQueue($id){
|
|
$queueObj = GlobalVariables::get("dao")->getFirst("CBSQueueModel", array("operationId"=>$id));
|
|
$queueObj->retry = $queueObj->retry+1;
|
|
GlobalVariables::get("dao")->save($queueObj);
|
|
}
|
|
|
|
// private function getOperationIdFile(){
|
|
// $config = GlobalVariables::get("config");
|
|
// return $config->paths->common."/RAPI_maxOperationId";
|
|
// }
|
|
}
|
|
|
|
?>
|