1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202:
<?php
namespace Nethgui\Model;
class SystemTasks
{
const PTRACK_PATH_TEMPLATE = '/var/run/ptrack/%s.sock';
const PTRACK_DUMP_PATH = '/var/spool/ptrack/%.16s.dump';
const TY_DECLARE = 0x01;
const TY_DONE = 0x02;
const TY_QUERY = 0x03;
const TY_PROGRESS = 0x04;
const TY_ERROR = 0x40;
const TY_RESPONSE = 0x80;
protected $phpWrapper;
protected $log;
private $tasks = array();
public function __construct(\Nethgui\Log\LogInterface $log)
{
$this->log = $log;
$this->phpWrapper = new \Nethgui\Utility\PhpWrapper(__CLASS__);
}
public function getRunningTasks()
{
$running = array();
$pattern = '|' . sprintf(preg_quote(self::PTRACK_PATH_TEMPLATE, '|'), "(?P<taskId>[^.]+)") . '|';
foreach ($this->phpWrapper->glob(sprintf(self::PTRACK_PATH_TEMPLATE, '*')) as $socketPath) {
$matches = array();
if (preg_match($pattern, $socketPath, $matches) && isset($matches['taskId'])) {
try {
$task = $this->getTaskStatus($matches['taskId']);
} catch (\RuntimeException $ex) {
$this->log->exception($ex);
$this->phpWrapper->unlink($socketPath);
continue;
}
$running[$matches['taskId']] = $task;
}
}
return $running;
}
public function getStartingTasks()
{
return array_filter($this->tasks, function ($t) {
return isset($t['starting']);
});
}
public function getTaskStatus($taskId)
{
if ( ! isset($this->tasks[$taskId])) {
$this->tasks[$taskId] = $this->fetchTaskStatus($taskId);
}
return $this->tasks[$taskId];
}
public function setTaskStarting($taskId)
{
if (isset($this->tasks[$taskId])) {
throw new \LogicException(sprintf("%s: the taskId is already registered", __CLASS__), 1405928979);
}
$this->tasks[$taskId] = array(
'starting' => $taskId,
);
return $this;
}
private function fetchTaskStatus($taskId)
{
$socketPath = sprintf(self::PTRACK_PATH_TEMPLATE, $taskId);
$dumpPath = sprintf(self::PTRACK_DUMP_PATH, md5($socketPath));
$taskStatus = FALSE;
$errno = 0;
$errstr = "";
$socket = $this->phpWrapper->fsockopen('unix://' . $socketPath, -1, $errno, $errstr);
if ($socket === FALSE) {
$socketPathExists = $errno != 2;
if ($socketPathExists) {
$this->log->error(sprintf('%s: Socket %s exists, but open failed: errno %d, errstr %s', __CLASS__, $socketPath, $errno, $errstr));
}
$taskStatus = $this->fetchDumpFile($dumpPath);
} else {
$this->sendMessage($socket, self::TY_QUERY);
$taskStatus = $this->recvMessage($socket);
$this->phpWrapper->fclose($socket);
}
return $taskStatus;
}
private function fetchDumpFile($dumpPath)
{
if ( ! $this->phpWrapper->file_exists($dumpPath)) {
throw new \RuntimeException(sprintf("%s: could not open dump file %s", __CLASS__, $dumpPath), 1405613538);
}
$tmp = json_decode($this->phpWrapper->file_get_contents($dumpPath), TRUE);
if ( ! is_array($tmp)) {
throw new \RuntimeException(sprintf("%s: dump file decode error", __CLASS__), 1405613539);
}
return $tmp;
}
private function sendMessage($socket, $type, $args = array())
{
$payload = json_encode($args);
$data = pack('Cn', (int) $type, strlen($payload)) . $payload;
$written = $this->phpWrapper->fwrite($socket, $data);
if ($written !== strlen($data)) {
throw new \RuntimeException(sprintf('%s: Socket write error', __CLASS__), 1405610071);
}
}
private function recvMessage($socket)
{
$buf = $this->safeRead($socket, 3);
if ($buf === FALSE) {
throw new \RuntimeException(sprintf('%s: Socket read error', __CLASS__), 1405610072);
}
$header = unpack('Ctype/nsize', $buf);
if ( ! is_array($header)) {
throw new \RuntimeException(sprintf('%s: Socket read error', __CLASS__), 1405610073);
}
$message = NULL;
if ($header['type'] & self::TY_RESPONSE) {
$message = $this->safeRead($socket, $header['size']);
if ($message === FALSE) {
throw new \RuntimeException(sprintf('%s: Socket read error', __CLASS__), 1405610074);
}
}
return json_decode($message, TRUE);
}
private function safeRead($socket, $size)
{
$buffer = "";
$count = 0;
while ($count < $size) {
if (feof($socket)) {
return FALSE;
}
$chunk = $this->phpWrapper->fread($socket, $size - $count);
$count += strlen($chunk);
if ($chunk === FALSE) {
return FALSE;
}
$buffer .= $chunk;
}
return $buffer;
}
}