Commit a6e1e072 authored by Carsten  Rose's avatar Carsten Rose
Browse files

recordLocking

Dirty.php: add comments. Add timeout checking
parent ecce620b
......@@ -213,7 +213,7 @@ const ERROR_UPLOAD = 1500;
const ERROR_UPLOAD_TOO_BIG = 1501;
const ERROR_UPLOAD_FILE_TYPE = 1502;
const ERROR_UPLOAD_GET_MIME_TYPE = 1503;
const ERROR_UNKNOWN_ACTION = 1504;
const ERROR_UPLOAD_UNKNOWN_ACTION = 1504;
const ERROR_NO_TARGET_PATH_FILE_NAME = 1505;
// LDAP
......@@ -255,8 +255,9 @@ const ERROR_DB_MULTI_QUERY_FAILED = 2016;
const ERROR_SUBSTITUTE_FOUND = 2100;
// Dirty
const ERROR_MISSING_FORM_IN_SIP = 2200;
const ERROR_DELETE_DIRTY_RECORD = 2201;
const ERROR_DIRTY_MISSING_FORM_IN_SIP = 2200;
const ERROR_DIRTY_DELETE_RECORD = 2201;
const ERROR_DIRTY_UNKNOWN_ACTION = 2202;
//
// Store Names: Identifier
......
......@@ -67,7 +67,7 @@ class File {
$this->doDelete($sipUpload, $statusUpload);
break;
default:
throw new UserFormException("Unknown FILE_ACTION: $action", ERROR_UNKNOWN_ACTION);
throw new UserFormException("Unknown FILE_ACTION: $action", ERROR_UPLOAD_UNKNOWN_ACTION);
}
}
......
......@@ -15,7 +15,14 @@ require_once(__DIR__ . '/../Constants.php');
require_once(__DIR__ . '/../database/Database.php');
require_once(__DIR__ . '/../../qfq/store/Client.php');
/**
* Class Dirty
* Process Record locking in mode DIRTY_MODE_ADVISORY, DIRTY_MODE_EXCLUSIVE or DIRTY_MODE_NONE.
* Two entry points: process() and checkDirtyAndRelease().
* Check doc/diagram/*.png for detailed workflow.
*
* @package qfq
*/
class Dirty {
/**
......@@ -39,7 +46,7 @@ class Dirty {
private $lockTimeout = false;
/**
*
* Init class
*/
public function __construct($lockTimeout = false, $phpUnit = false) {
......@@ -56,13 +63,14 @@ class Dirty {
}
/**
* Handle any lock requests submitted via api/dirty.php.
*
* @return array|int
* @throws CodeException
* @throws DbException
* @throws UserFormException
*/
public function process() {
$answer = array();
$tableVars = array();
$sipClass = new Sip();
......@@ -70,7 +78,7 @@ class Dirty {
$sipVars = $sipClass->getVarsFromSip($this->client[SIP_SIP]);
if (empty($sipVars[SIP_FORM])) {
throw new CodeException("Missing 'form' in SIP. There might be something broken.", ERROR_MISSING_FORM_IN_SIP);
throw new CodeException("Missing 'form' in SIP. There might be something broken.", ERROR_DIRTY_MISSING_FORM_IN_SIP);
}
$recordId = empty($sipVars[SIP_RECORD_ID]) ? 0 : $sipVars[SIP_RECORD_ID];
......@@ -86,6 +94,7 @@ class Dirty {
$answer = $this->checkDirtyAndRelease(FORM_SAVE, $tableVars[F_DIRTY_MODE], $tableVars[F_TABLE_NAME], $recordId);
break;
default;
throw new CodeException("Unknown action: " . $this->client[API_LOCK_ACTION], ERROR_DIRTY_UNKNOWN_ACTION);
}
return $answer;
......@@ -104,8 +113,6 @@ class Dirty {
*/
private function acquireDirty($recordId, array $tableVars) {
$answer = array();
// For r=0 (new) , 'dirty' will always succeed.
if ($recordId == 0) {
return [API_STATUS => 'success', API_MESSAGE => ''];
......@@ -119,6 +126,15 @@ class Dirty {
// Look for already existing dirty record.
$recordDirty = $this->getRecordDirty($tableName, $recordId);
// Check if the record is timed out
if (count($recordDirty) != 0 && $this->lockTimeout > 0) {
$datetimeExpire = date_add(date_create($recordDirty[COLUMN_MODIFIED]), date_interval_create_from_date_string($this->lockTimeout . " second"));
if ($datetimeExpire <= date_create("now")) {
$this->deleteDirtyRecord($recordDirty[COLUMN_ID]);
$recordDirty=array();
}
}
if (count($recordDirty) == 0) {
// No dirty record found.
$answer = $this->writeDirty($this->client[SIP_SIP], $recordId, $tableName, $formDirtyMode, $feUser);
......@@ -148,6 +164,7 @@ class Dirty {
}
/**
*
* @param array $recordDirty
* @param $currentFormDirtyMode
* @return array
......@@ -290,7 +307,7 @@ class Dirty {
private function deleteDirtyRecord($recordDirtyId) {
$cnt = $this->db->sql('DELETE FROM Dirty WHERE id=? LIMIT 1', ROW_REGULAR, [$recordDirtyId]);
if ($cnt != 1) {
throw new CodeException("Failed to delete dirty record id=" . $recordDirtyId, ERROR_DELETE_DIRTY_RECORD);
throw new CodeException("Failed to delete dirty record id=" . $recordDirtyId, ERROR_DIRTY_DELETE_RECORD);
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment