1
0
Fork 0
Manager/app/cron/IarMonitor.php

104 lines
4.2 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Mcp\Cron;
use Mcp\OpenSim;
use Mcp\Opensim\RestConsole;
use Mcp\Util\Util;
class IarMonitor extends CronJob
{
2023-09-11 04:04:30 +00:00
2023-09-11 05:05:21 +00:00
private ?RestConsole $console = null;
2023-09-11 04:04:30 +00:00
private bool $consoleAvailable = true;
public function __construct(\Mcp\Mcp $app)
{
parent::__construct($app, Frequency::EACH_MINUTE);
}
public function run(): bool
{
$opensim = new OpenSim($this->app->db());
2023-09-11 04:04:30 +00:00
$dirPath = $this->app->getDataDir().DIRECTORY_SEPARATOR.'iars';
if (!is_dir($dirPath)) {
mkdir($dirPath);
}
2023-09-11 04:04:30 +00:00
$statement = $this->app->db()->prepare("SELECT userID,iarfilename,filesize,state FROM mcp_iar_state WHERE state < ?");
$statement->execute([2]);
2023-09-11 04:04:30 +00:00
if ($statement->rowCount() > 0) {
while ($row = $statement->fetch()) {
if ($row['state'] == 0) { // 0 - Request to OS pending
if ($this->console() === false) {
continue;
}
$name = explode(' ', $opensim->getUserName($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 = ?');
$statementUpdate->execute([1, $row['userID']]);
}
} elseif ($row['state'] == 1) { // 1 - IAR Creation in progress
$fullFilePath = $dirPath.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']]);
2023-09-11 06:50:33 +00:00
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).' , sie ist mit dem Passwort "password" geschützt.');
2023-09-11 04:04:30 +00:00
}
}
}
}
2023-09-11 05:05:21 +00:00
if ($this->console != null) {
2023-09-11 04:04:30 +00:00
$this->console->closeSession();
}
}
// 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()) {
2023-09-11 04:04:30 +00:00
$fullFilePath = $dirPath.DIRECTORY_SEPARATOR.$row['iarfilename'];
if (file_exists($fullFilePath) && unlink($fullFilePath)) {
$statementDeleteExpired->execute([2, $row['userID']]);
}
}
return true;
}
2023-09-11 04:04:30 +00:00
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;
}
}