fix: issues when worker is stopped, allow null pid and user in worker class

pull/7939/head
Luciano Righetti 2021-11-10 16:10:42 +01:00
parent 766f559852
commit 89c39270e4
2 changed files with 15 additions and 12 deletions

View File

@ -6,7 +6,7 @@ App::uses('Model', 'Model');
class Worker implements JsonSerializable
{
/** @var integer */
/** @var integer|null */
private $pid;
/** @var string */
@ -15,7 +15,7 @@ class Worker implements JsonSerializable
/**
* OS user
*
* @var string
* @var string|null
*/
private $user;
@ -67,7 +67,7 @@ class Worker implements JsonSerializable
];
}
public function pid(): int
public function pid(): ?int
{
return $this->pid;
}
@ -77,7 +77,7 @@ class Worker implements JsonSerializable
return $this->queue;
}
public function user(): string
public function user(): ?string
{
return $this->user;
}

View File

@ -295,14 +295,17 @@ class BackgroundJobsTool
foreach ($procs as $proc) {
if ($proc->offsetGet('group') === self::MISP_WORKERS_PROCESS_GROUP) {
$workers[] = new Worker([
'pid' => $proc->offsetGet('pid'),
'queue' => explode("_", $proc->offsetGet('name'))[0],
'user' => trim(shell_exec(sprintf("ps -o uname= -p %s", (int) $proc->offsetGet('pid')))),
'createdAt' => $proc->offsetGet('start'),
'updatedAt' => $proc->offsetGet('now'),
'status' => $this->convertProcessStatus($proc->offsetGet('state'))
]);
if ($proc->offsetGet('pid') > 0) {
$user = trim(shell_exec(sprintf("ps -o uname='' -p %s", (int) $proc->offsetGet('pid'))) ?? '');
$workers[] = new Worker([
'pid' => $proc->offsetGet('pid'),
'queue' => explode("_", $proc->offsetGet('name'))[0],
'user' => $user,
'createdAt' => $proc->offsetGet('start'),
'updatedAt' => $proc->offsetGet('now'),
'status' => $this->convertProcessStatus($proc->offsetGet('state'))
]);
}
}
}