refactor: move pkill to binext

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/888/head
Varun Patil 2023-10-20 12:42:22 -07:00
parent fd851b5e34
commit e406b74806
3 changed files with 46 additions and 47 deletions

View File

@ -6,7 +6,6 @@ namespace OCA\Memories\Migration;
use OCA\Memories\Db\AddMissingIndices; use OCA\Memories\Db\AddMissingIndices;
use OCA\Memories\Service\BinExt; use OCA\Memories\Service\BinExt;
use OCA\Memories\Util;
use OCP\IConfig; use OCP\IConfig;
use OCP\Migration\IOutput; use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep; use OCP\Migration\IRepairStep;
@ -26,8 +25,8 @@ class Repair implements IRepairStep
AddMissingIndices::run($output); AddMissingIndices::run($output);
// kill any instances of go-vod and exiftool // kill any instances of go-vod and exiftool
Util::pkill(BinExt::getName('go-vod')); BinExt::pkill(BinExt::getName('go-vod'));
Util::pkill(BinExt::getName('exiftool')); BinExt::pkill(BinExt::getName('exiftool'));
// detect exiftool // detect exiftool
if ($path = BinExt::detectExiftool()) { if ($path = BinExt::detectExiftool()) {

View File

@ -229,7 +229,7 @@ class BinExt
// Check if disabled // Check if disabled
if (Util::getSystemConfig('memories.vod.disable')) { if (Util::getSystemConfig('memories.vod.disable')) {
// Make sure it's dead, in case the user just disabled it // Make sure it's dead, in case the user just disabled it
Util::pkill(self::getName('go-vod')); self::pkill(self::getName('go-vod'));
return null; return null;
} }
@ -271,7 +271,7 @@ class BinExt
file_put_contents($configFile, json_encode($env, JSON_PRETTY_PRINT)); file_put_contents($configFile, json_encode($env, JSON_PRETTY_PRINT));
// Kill the transcoder in case it's running // Kill the transcoder in case it's running
Util::pkill(self::getName('go-vod')); self::pkill(self::getName('go-vod'));
// Start transcoder // Start transcoder
/** @psalm-suppress ForbiddenCode */ /** @psalm-suppress ForbiddenCode */
@ -451,4 +451,46 @@ class BinExt
/** @psalm-suppress ForbiddenCode */ /** @psalm-suppress ForbiddenCode */
return shell_exec("{$path} -e 'print $^V;'") ?: 'unknown version'; return shell_exec("{$path} -e 'print $^V;'") ?: 'unknown version';
} }
/**
* Kill all instances of a process by name.
* Similar to pkill, which may not be available on all systems.
*
* @param string $name Process name (only the first 12 characters are used)
*/
public static function pkill(string $name): void
{
// don't kill everything
if (empty($name)) {
return;
}
// only use the first 12 characters
$name = substr($name, 0, 12);
// check if ps or busybox is available
$ps = 'ps';
/** @psalm-suppress ForbiddenCode */
if (!shell_exec('which ps')) {
if (!shell_exec('which busybox')) {
return;
}
$ps = 'busybox ps';
}
// get pids using ps as array
/** @psalm-suppress ForbiddenCode */
$pids = shell_exec("{$ps} -eao pid,comm | grep {$name} | awk '{print $1}'");
if (null === $pids || empty($pids)) {
return;
}
$pids = array_filter(explode("\n", $pids));
// kill all pids
foreach ($pids as $pid) {
posix_kill((int) $pid, 9); // SIGKILL
}
}
} }

View File

@ -482,46 +482,4 @@ class Util
return null; return null;
} }
/**
* Kill all instances of a process by name.
* Similar to pkill, which may not be available on all systems.
*
* @param string $name Process name (only the first 12 characters are used)
*/
public static function pkill(string $name): void
{
// don't kill everything
if (empty($name)) {
return;
}
// only use the first 12 characters
$name = substr($name, 0, 12);
// check if ps or busybox is available
$ps = 'ps';
/** @psalm-suppress ForbiddenCode */
if (!shell_exec('which ps')) {
if (!shell_exec('which busybox')) {
return;
}
$ps = 'busybox ps';
}
// get pids using ps as array
/** @psalm-suppress ForbiddenCode */
$pids = shell_exec("{$ps} -eao pid,comm | grep {$name} | awk '{print $1}'");
if (null === $pids || empty($pids)) {
return;
}
$pids = array_filter(explode("\n", $pids));
// kill all pids
foreach ($pids as $pid) {
posix_kill((int) $pid, 9); // SIGKILL
}
}
} }