<?php
 namespace App\ReportsData; class Params extends \App\Config\DbUniAssocVarsAbstract { protected $dbVarsPrefix = ''; protected $dbTablePkCol = 'var'; protected $dbTableValueCol = 'value'; protected $dbTableLastUpdateTimeCol = 'lastUpdate'; protected $dbVarsList = [ 'status', 'wsAuthToken', 'blockedUntil', 'blockingApp' ]; protected $cacheType = 'dummy'; private $maxBlockTime = 3600; protected function getDbTableName() { global $DBTRdParams; return $DBTRdParams; } protected function save_check() { parent::save_check(); if ($this->itemsList) { foreach($this->itemsList as $var=>$value) { if (mb_strlen($value)>32) { $this->addErrorMessage("The var `{$var}` in SystemVars must not have a length greater than 32 chars."); } if ($var=='status' and !in_array($value, ['running', 'pending', 'stopped', 'blocked', 'disabled'])) { $this->addErrorMessage("The var `{$var}` in SystemVars must not have a length greater than 32 chars."); } if ($var=='wsAuthToken' and mb_strlen($value)!=32) { $this->addErrorMessage("The var `{$var}` in SystemVars must have a length of 32 chars."); } } } } public function getStatus() { if ($this->constructType!='instance') { throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `instance`."); } $currentStatus = $this->get('status'); if (!$currentStatus) { $currentStatus = 'disabled'; } if ($currentStatus=='blocked') { if ($this->get('blockedUntil') < \Engine\DateTime::create()->formatDatetimeIso()) { $currentStatus = 'pending'; } } return $currentStatus; } public function getMaxBlockTime() { if ($this->constructType!='instance') { throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `instance`."); } return $this->maxBlockTime; } public function saveStatusToRunning() { if ($this->constructType!='admin') { throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `admin`."); } $currentStatus = self::getInstance()->getStatus(); if ($currentStatus=='running') { return true; } if (!in_array($currentStatus, ['pending', 'running'])) { $this->addErrorMessage("The RD queue cannot be set to running as the current status is `{$currentStatus}`."); } $this->set('status', 'running'); $this->save(); } public function saveStatusToPending() { if ($this->constructType!='admin') { throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `admin`."); } $this->set('status', 'pending'); $this->save(); } public function saveStatusToDisabled() { if ($this->constructType!='admin') { throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `admin`."); } $this->set('status', 'disabled'); $this->save(); } public function saveStatusToStopped() { if ($this->constructType!='admin') { throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `admin`."); } $this->set('status', 'stopped'); $this->save(); } public function saveStatusToBlocked($blockTime, $blockingApp) { if ($this->constructType!='admin') { throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `admin`."); } if (!is_numeric($blockTime) or $blockTime<=0) { $this->addErrorMessage('You did not set a valid block time.'); } if ($blockTime>$this->maxBlockTime) { $this->addErrorMessage("The block time must be smaller than {$this->maxBlockTime} seconds."); } if (!$blockingApp) { $this->addErrorMessage("The blocking app is not set."); } $currentStatus = self::getInstance()->getStatus(); if ($currentStatus!='running' and $currentStatus!='pending' and $currentStatus!='blocked') { $this->addErrorMessage("The RD queue cannot be blocked as the current status is `{$currentStatus}`."); } $blockedUntil = \Engine\DateTime::create("+{$blockTime} seconds")->formatDateTimeIso(); if ($currentStatus=='blocked') { $currentBlockTime = self::getInstance()->get('blockedUntil'); if ($blockedUntil<=$currentBlockTime) { return ; } } $this->set('status', 'blocked'); $this->set('blockedUntil', $blockedUntil); $this->set('blockingApp', $blockingApp); $this->save(); } } 