1
0
Fork 0

Add new cronjob for IAR request queue processing

master
Anonymous Contributor 2023-09-10 12:07:45 +02:00
parent 2ed8285c93
commit 93d8aafd6f
4 changed files with 84 additions and 59 deletions

View File

@ -11,6 +11,7 @@ class Mcp implements ConnectionProvider
private ?PDO $db = null;
private array $config;
private string $dataDir;
private string $templateDir;
const ROUTES = [
@ -42,6 +43,7 @@ class Mcp implements ConnectionProvider
public function __construct($basedir)
{
$this->templateDir = $basedir.DIRECTORY_SEPARATOR.'templates';
$this->dataDir = $basedir.DIRECTORY_SEPARATOR.'data';
$this->config = array();
try {
$config = parse_ini_file($basedir.DIRECTORY_SEPARATOR.'config.ini', true);
@ -95,6 +97,11 @@ class Mcp implements ConnectionProvider
])->unsafeVar('csrf', $this->csrfField());
}
public function getDataDir(): string
{
return $this->dataDir;
}
public function handleRequest()
{
$reqClass = 'Mcp\\Page\\Error';

66
app/cron/IarMonitor.php Normal file
View File

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Mcp\Cron;
use Mcp\OpenSim;
use Mcp\Opensim\RestConsole;
use Mcp\Util\Util;
class IarMonitor extends CronJob
{
public function __construct(\Mcp\Mcp $app)
{
parent::__construct($app, Frequency::EACH_MINUTE);
}
public function run(): bool
{
$opensim = new OpenSim($this->app->db());
$statement = $this->app->db()->prepare("SELECT userID,iarfilename,filesize,state,created FROM mcp_iar_state WHERE state < ?");
$statement->execute([2]);
while ($row = $statement->fetch()) {
if ($row['state'] == 0) { // 0 - Request to OS pending
$name = explode(' ', $opensim->getUserName($row['userID']));
$restCfg = $this->app->config('restconsole');
$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 = ?');
$statementUpdate->execute([1, $row['userID']]);
}
} elseif ($row['state'] == 1) { // 1 - IAR Creation in progress
$fullFilePath = $this->app->getDataDir().DIRECTORY_SEPARATOR.'iars'.DIRECTORY_SEPARATOR.$row['iarfilename'];
if (file_exists($fullFilePath)) {
$filesize = filesize($fullFilePath);
if ($filesize != $row['filesize']) {
$statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET filesize = ? WHERE userID = ?');
$statementUpdate->execute([$filesize, $row['userID']]);
} else {
$statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET state = ?, created = ? WHERE userID = ?');
$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').'/iars/'.$row['iarfilename']);
}
}
}
}
// 2 - IAR creation finished; delete if expired
$weekOld = time() - 604800;
$statementExpired = $this->app->db()->prepare('SELECT userID,iarfilename FROM mcp_iar_state WHERE state = ? AND created < ?');
$statementExpired->execute([2, $weekOld]);
$statementDeleteExpired = $this->app->db()->prepare('DELETE FROM mcp_iar_state WHERE state = ? AND userID = ?');
while ($row = $statementExpired->fetch()) {
$fullFilePath = $this->app->getDataDir().DIRECTORY_SEPARATOR.'iars'.DIRECTORY_SEPARATOR.$row['iarfilename'];
if (file_exists($fullFilePath) && unlink($fullFilePath)) {
$statementDeleteExpired->execute([2, $row['userID']]);
}
}
return true;
}
}

View File

@ -35,4 +35,14 @@ password = secret
[grid]
name = OpenSim
main-news = Yet another OpenSim Grid.
homeurl = http://...:8002
homeurl = http://...:8002
; Benötigt für die Anforderung von IARs
[iarfetcher]
; Zugangsdaten der REST-Konsole von OpenSimulator
host = example.com
port = 9001
user = mcp
password = secret
; IAR-Verzeichnis aus Sicht der ROBUST-Instanz
os-iar-path = /opt/opensim/iars/

View File

@ -1,58 +0,0 @@
<?php
include_once 'app/OpenSim.php';
$opensim = new OpenSim();
$statement = $RUNTIME['PDO']->prepare("CREATE TABLE IF NOT EXISTS `iarstates` (`userID` VARCHAR(36) NOT NULL COLLATE 'utf8_unicode_ci', `filesize` BIGINT(20) NOT NULL DEFAULT '0', `iarfilename` VARCHAR(64) NOT NULL COLLATE 'utf8_unicode_ci', `running` INT(1) NOT NULL DEFAULT '0', PRIMARY KEY (`userID`) USING BTREE) COLLATE='utf8_unicode_ci' ENGINE=InnoDB;");
$statement->execute();
$statement = $RUNTIME['PDO']->prepare("SELECT userID,iarfilename,filesize FROM iarstates WHERE running = 1 LIMIT 1");
$statement->execute();
if ($row = $statement->fetch()) {
$email = $opensim->getUserMail($row['userID']);
$fullFilePath = "/var/www/html/data/".$row['iarfilename'];
echo "Aktive IAR für ".$opensim->getUserName($row['userID'])." gefunden. File: ".$fullFilePath."\n";
if (file_exists($fullFilePath)) {
$filesize = filesize($fullFilePath);
if ($filesize != $row['filesize']) {
$statementUpdate = $RUNTIME['PDO']->prepare('UPDATE iarstates SET filesize = :filesize WHERE userID = :userID');
$statementUpdate->execute(['filesize' => $filesize, 'userID' => $row['userID']]);
echo "Status der IAR für ".$opensim->getUserName($row['userID']).": Speichert...\n";
} else {
$APIURL = $RUNTIME['SIDOMAN']['URL']."api.php?CONTAINER=".$RUNTIME['SIDOMAN']['CONTAINER']."&KEY=".$RUNTIME['SIDOMAN']['PASSWORD']."&METODE=RESTART";
$APIResult = file_get_contents($APIURL);
echo "Status der IAR für ".$opensim->getUserName($row['userID']).": Sende Mail...\n";
$statementUpdate = $RUNTIME['PDO']->prepare('DELETE FROM iarstates WHERE userID = :userID');
$statementUpdate->execute(['userID' => $row['userID']]);
sendInworldIM("00000000-0000-0000-0000-000000000000", $row['userID'], "Inventory", $RUNTIME['GRID']['HOMEURL'], "Deine IAR ist fertig zum Download: ".$RUNTIME['IAR']['BASEURL'].$row['iarfilename']);
}
} else {
$name = explode(" ", $opensim->getUserName($row['userID']));
$APIURL = $RUNTIME['SIDOMAN']['URL']."api.php?CONTAINER=".$RUNTIME['SIDOMAN']['CONTAINER']."&KEY=".$RUNTIME['SIDOMAN']['PASSWORD']."&METODE=COMMAND&COMMAND=".urlencode("save iar ".$name[0]." ".$name[1]." /* PASSWORD /downloads/".$row['iarfilename']);
$APIResult = file_get_contents($APIURL);
echo "IAR für ".$name[0]." ".$name[1]." wurde gestartet: Status: ".$APIResult."\n";
}
} else {
$statement = $RUNTIME['PDO']->prepare("SELECT userID,iarfilename FROM iarstates WHERE running = 0 LIMIT 1");
$statement->execute();
while ($row = $statement->fetch()) {
$statementUpdate = $RUNTIME['PDO']->prepare('UPDATE iarstates SET running = :running WHERE userID = :userID');
$statementUpdate->execute(['running' => 1, 'userID' => $row['userID']]);
$name = explode(" ", $opensim->getUserName($row['userID']));
$APIURL = $RUNTIME['SIDOMAN']['URL']."api.php?CONTAINER=".$RUNTIME['SIDOMAN']['CONTAINER']."&KEY=".$RUNTIME['SIDOMAN']['PASSWORD']."&METODE=COMMAND&COMMAND=".urlencode("save iar ".$name[0]." ".$name[1]." /* PASSWORD /downloads/".$row['iarfilename']);
$APIResult = file_get_contents($APIURL);
echo "IAR für ".$name[0]." ".$name[1]." wurde gestartet: Status: ".$APIResult."\n";
}
}