Optimize IarMonitor
parent
dbcb57f71f
commit
6a4c343b03
|
@ -9,6 +9,10 @@ use Mcp\Util\Util;
|
||||||
|
|
||||||
class IarMonitor extends CronJob
|
class IarMonitor extends CronJob
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private ?RestConsole $console;
|
||||||
|
private bool $consoleAvailable = true;
|
||||||
|
|
||||||
public function __construct(\Mcp\Mcp $app)
|
public function __construct(\Mcp\Mcp $app)
|
||||||
{
|
{
|
||||||
parent::__construct($app, Frequency::EACH_MINUTE);
|
parent::__construct($app, Frequency::EACH_MINUTE);
|
||||||
|
@ -17,45 +21,57 @@ class IarMonitor extends CronJob
|
||||||
public function run(): bool
|
public function run(): bool
|
||||||
{
|
{
|
||||||
$opensim = new OpenSim($this->app->db());
|
$opensim = new OpenSim($this->app->db());
|
||||||
|
|
||||||
|
$dirPath = $this->app->getDataDir().DIRECTORY_SEPARATOR.'iars';
|
||||||
|
if (!is_dir($dirPath)) {
|
||||||
|
mkdir($dirPath);
|
||||||
|
}
|
||||||
|
|
||||||
$statement = $this->app->db()->prepare("SELECT userID,iarfilename,filesize,state,created FROM mcp_iar_state WHERE state < ?");
|
$statement = $this->app->db()->prepare("SELECT userID,iarfilename,filesize,state FROM mcp_iar_state WHERE state < ?");
|
||||||
$statement->execute([2]);
|
$statement->execute([2]);
|
||||||
|
|
||||||
while ($row = $statement->fetch()) {
|
if ($statement->rowCount() > 0) {
|
||||||
if ($row['state'] == 0) { // 0 - Request to OS pending
|
while ($row = $statement->fetch()) {
|
||||||
$name = explode(' ', $opensim->getUserName($row['userID']));
|
if ($row['state'] == 0) { // 0 - Request to OS pending
|
||||||
|
if ($this->console() === false) {
|
||||||
$restCfg = $this->app->config('restconsole');
|
continue;
|
||||||
$restConsole = new RestConsole($restCfg['host'], intval($restCfg['port']));
|
}
|
||||||
if ($restConsole->startSession($restCfg['user'], $restCfg['password']) && $restConsole->sendCommand('save iar '.$name[0].' '.$name[1].' /* password '.$restCfg['os-iar-path'].$row['iarfilename'])) {
|
|
||||||
$statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET state = ? WHERE userID = ?');
|
$name = explode(' ', $opensim->getUserName($row['userID']));
|
||||||
$statementUpdate->execute([1, $row['userID']]);
|
if ($this->console()->sendCommand('save iar '.$name[0].' '.$name[1].' /* password '.$this->app->config('iarfetcher')['os-iar-path'].$row['iarfilename'])) {
|
||||||
}
|
$statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET state = ? WHERE userID = ?');
|
||||||
} elseif ($row['state'] == 1) { // 1 - IAR Creation in progress
|
$statementUpdate->execute([1, $row['userID']]);
|
||||||
$fullFilePath = $this->app->getDataDir().DIRECTORY_SEPARATOR.'iars'.DIRECTORY_SEPARATOR.$row['iarfilename'];
|
}
|
||||||
if (file_exists($fullFilePath)) {
|
} elseif ($row['state'] == 1) { // 1 - IAR Creation in progress
|
||||||
$filesize = filesize($fullFilePath);
|
$fullFilePath = $dirPath.DIRECTORY_SEPARATOR.$row['iarfilename'];
|
||||||
|
if (file_exists($fullFilePath)) {
|
||||||
if ($filesize != $row['filesize']) {
|
$filesize = filesize($fullFilePath);
|
||||||
$statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET filesize = ? WHERE userID = ?');
|
|
||||||
$statementUpdate->execute([$filesize, $row['userID']]);
|
if ($filesize != $row['filesize']) {
|
||||||
} else {
|
$statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET filesize = ? WHERE userID = ?');
|
||||||
$statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET state = ?, created = ? WHERE userID = ?');
|
$statementUpdate->execute([$filesize, $row['userID']]);
|
||||||
$statementUpdate->execute([2, time(), $row['userID']]);
|
} else {
|
||||||
|
$statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET state = ?, created = ? WHERE userID = ?');
|
||||||
Util::sendInworldIM("00000000-0000-0000-0000-000000000000", $row['userID'], "Inventory", $this->app->config('grid')['homeurl'], "Deine IAR ist fertig zum Download: https://".$this->app->config('domain').'/iars/'.$row['iarfilename']);
|
$statementUpdate->execute([2, time(), $row['userID']]);
|
||||||
|
|
||||||
|
Util::sendInworldIM("00000000-0000-0000-0000-000000000000", $row['userID'], "Inventory", $this->app->config('grid')['homeurl'], "Deine IAR ist fertig zum Download: https://".$this->app->config('domain').'/index.php?api=downloadIar&id='.substr($row['iarfilename'], 0, strlen($row['iarfilename']) - 4));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($this->consoleAvailable) {
|
||||||
|
$this->console->closeSession();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2 - IAR creation finished; delete if expired
|
// 2 - IAR creation finished; delete if expired
|
||||||
$weekOld = time() - 604800;
|
$weekOld = time() - 604800;
|
||||||
$statementExpired = $this->app->db()->prepare('SELECT userID,iarfilename FROM mcp_iar_state WHERE state = ? AND created < ?');
|
$statementExpired = $this->app->db()->prepare('SELECT userID,iarfilename FROM mcp_iar_state WHERE state = ? AND created < ?');
|
||||||
$statementExpired->execute([2, $weekOld]);
|
$statementExpired->execute([2, $weekOld]);
|
||||||
$statementDeleteExpired = $this->app->db()->prepare('DELETE FROM mcp_iar_state WHERE state = ? AND userID = ?');
|
$statementDeleteExpired = $this->app->db()->prepare('DELETE FROM mcp_iar_state WHERE state = ? AND userID = ?');
|
||||||
while ($row = $statementExpired->fetch()) {
|
while ($row = $statementExpired->fetch()) {
|
||||||
$fullFilePath = $this->app->getDataDir().DIRECTORY_SEPARATOR.'iars'.DIRECTORY_SEPARATOR.$row['iarfilename'];
|
$fullFilePath = $dirPath.DIRECTORY_SEPARATOR.$row['iarfilename'];
|
||||||
if (file_exists($fullFilePath) && unlink($fullFilePath)) {
|
if (file_exists($fullFilePath) && unlink($fullFilePath)) {
|
||||||
$statementDeleteExpired->execute([2, $row['userID']]);
|
$statementDeleteExpired->execute([2, $row['userID']]);
|
||||||
}
|
}
|
||||||
|
@ -63,4 +79,25 @@ class IarMonitor extends CronJob
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function console(): RestConsole|bool
|
||||||
|
{
|
||||||
|
if (!$this->consoleAvailable) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->console == null) {
|
||||||
|
$restCfg = $this->app->config('iarfetcher');
|
||||||
|
$console = new RestConsole($restCfg['host'], intval($restCfg['port']));
|
||||||
|
if ($console->startSession($restCfg['user'], $restCfg['password'])) {
|
||||||
|
$this->console = $console;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$this->consoleAvailable = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->console;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,15 @@ class RestConsole
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function closeSession(): void
|
||||||
|
{
|
||||||
|
$response = $this->sendRequest('/CloseSession/', [
|
||||||
|
'ID' => $this->sessionId
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->detectError($response, '/CloseSession/');
|
||||||
|
}
|
||||||
|
|
||||||
public function readResponses(): array
|
public function readResponses(): array
|
||||||
{
|
{
|
||||||
if ($this->sessionId == null) {
|
if ($this->sessionId == null) {
|
||||||
|
|
Loading…
Reference in New Issue