-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Internalize ResqueStatus, fix deprecated tests. #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: '3' | ||
services: | ||
php: | ||
container_name: php | ||
build: ./docker/php | ||
working_dir: /app | ||
volumes: | ||
- "./:/app:cached" | ||
|
||
redis: | ||
container_name: redis | ||
image: "redis:alpine" | ||
depends_on: | ||
- php |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM php:8.1-fpm | ||
|
||
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ | ||
|
||
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer | ||
|
||
RUN chmod +x /usr/local/bin/install-php-extensions && \ | ||
install-php-extensions redis posix pcntl zip |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ | |
|
||
namespace Fresque; | ||
|
||
use ResqueStatus\ResqueStatus; | ||
use ezcConsoleInput; | ||
use ezcConsoleOutput; | ||
use DateTime; | ||
|
@@ -587,7 +586,7 @@ function (&$worker) { | |
return $worker = (string) $worker; | ||
} | ||
); | ||
$pausedWorkers = call_user_func([$this->ResqueStatus, 'getPausedWorker']); | ||
$pausedWorkers = call_user_func([$this->ResqueStatus, 'getPausedWorkers']); | ||
|
||
$this->debug('Searching for active workers'); | ||
$options = new SendSignalCommandOptions(); | ||
|
@@ -622,7 +621,7 @@ public function resume() | |
$options->allOption = 'Resume all workers'; | ||
$options->selectMessage = 'Worker to resume'; | ||
$options->actionMessage = 'resuming'; | ||
$options->workers = call_user_func([$this->ResqueStatus, 'getPausedWorker']); | ||
$options->workers = call_user_func([$this->ResqueStatus, 'getPausedWorkers']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Автозамена, так бы убрал |
||
$options->signal = 'CONT'; | ||
$options->successCallback = function ($pid, $workerName) use ($ResqueStatus) { | ||
$ResqueStatus->setPausedWorker($workerName, false); | ||
|
@@ -665,7 +664,7 @@ public function sendSignal($options) | |
); | ||
}; | ||
} else { | ||
$listFormatter = $option->formatListItem; | ||
$listFormatter = $options->formatListItem; | ||
} | ||
|
||
if (empty($options->workers)) { | ||
|
@@ -930,7 +929,7 @@ public function stats() | |
|
||
if (!empty($workers)) { | ||
|
||
$pausedWorkers = call_user_func([$this->ResqueStatus, 'getPausedWorker']); | ||
$pausedWorkers = call_user_func([$this->ResqueStatus, 'getPausedWorkers']); | ||
|
||
foreach ($workers as $worker) { | ||
if ($this->runtime['Scheduler']['enabled'] === true && $this->ResqueStatus->isSchedulerWorker($worker)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
namespace Fresque; | ||
|
||
use Redis; | ||
|
||
/** | ||
* Saving the workers statuses | ||
*/ | ||
class ResqueStatus | ||
{ | ||
public const WORKER_KEY = 'ResqueWorker'; | ||
public const SCHEDULER_WORKER_KEY = 'ResqueSchedulerWorker'; | ||
public const PAUSED_WORKER_KEY = 'PausedWorker'; | ||
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут сразу и рефакторинг небольшой сделал, публичные т.к. в тестах юзаются. |
||
|
||
public function __construct(protected Redis $redis) | ||
{ | ||
} | ||
|
||
/** | ||
* Save the workers arguments | ||
* | ||
* Used when restarting the worker | ||
* | ||
* @param array $args Worker settings | ||
*/ | ||
public function addWorker(int $pid, array $args): bool | ||
{ | ||
return $this->redis->hSet(self::WORKER_KEY, $pid, serialize($args)) !== false;; | ||
} | ||
|
||
/** | ||
* Register a Scheduler Worker | ||
*/ | ||
public function registerSchedulerWorker(int $pid): bool | ||
{ | ||
return (bool) $this->redis->set(self::SCHEDULER_WORKER_KEY, $pid); | ||
} | ||
|
||
/** | ||
* Test if a given worker is a scheduler worker | ||
* | ||
* @param Worker|string $worker Worker to test | ||
* | ||
* @return boolean True if the worker is a scheduler worker | ||
* @since 0.0.1 | ||
*/ | ||
public function isSchedulerWorker(mixed $worker): bool | ||
{ | ||
[$host, $pid, $queue] = explode(':', (string) $worker); | ||
|
||
return $pid === $this->redis->get(self::SCHEDULER_WORKER_KEY); | ||
} | ||
|
||
/** | ||
* Check if the Scheduler Worker is already running | ||
* | ||
* @return boolean True if the scheduler worker is already running | ||
*/ | ||
public function isRunningSchedulerWorker(): bool | ||
{ | ||
$pids = $this->redis->hKeys(self::WORKER_KEY); | ||
$schedulerPid = $this->redis->get(self::SCHEDULER_WORKER_KEY); | ||
|
||
if ($schedulerPid !== false && is_array($pids)) { | ||
if (in_array($schedulerPid, $pids)) { | ||
return true; | ||
} | ||
// Pid is outdated, remove it | ||
$this->unregisterSchedulerWorker(); | ||
|
||
return false; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Unregister a Scheduler Worker | ||
* | ||
* @return boolean True if the scheduler worker existed and was successfully unregistered | ||
*/ | ||
public function unregisterSchedulerWorker(): bool | ||
{ | ||
return $this->redis->del(self::SCHEDULER_WORKER_KEY) > 0; | ||
} | ||
|
||
/** | ||
* Return all started workers arguments | ||
* | ||
* @return array An array of settings, by worker | ||
*/ | ||
public function getWorkers(): array | ||
{ | ||
$workers = $this->redis->hGetAll(self::WORKER_KEY); | ||
$temp = []; | ||
|
||
foreach ($workers as $name => $value) { | ||
$temp[$name] = unserialize($value); | ||
} | ||
|
||
return $temp; | ||
} | ||
|
||
public function removeWorker(int $pid): void | ||
{ | ||
$this->redis->hDel(self::WORKER_KEY, $pid); | ||
} | ||
|
||
/** | ||
* Clear all workers saved arguments | ||
*/ | ||
public function clearWorkers(): void | ||
{ | ||
$this->redis->del(self::WORKER_KEY); | ||
$this->redis->del(self::PAUSED_WORKER_KEY); | ||
} | ||
|
||
/** | ||
* Mark a worker as paused/active | ||
* | ||
* @param string $workerName Name of the paused worker | ||
* @param bool $paused Whether to mark the worker as paused or active | ||
*/ | ||
public function setPausedWorker(string $workerName, bool $paused = true): void | ||
{ | ||
if ($paused) { | ||
$this->redis->sadd(self::PAUSED_WORKER_KEY, $workerName); | ||
} else { | ||
$this->redis->srem(self::PAUSED_WORKER_KEY, $workerName); | ||
} | ||
} | ||
|
||
/** | ||
* Return a list of paused workers | ||
* | ||
* @return array An array of paused workers' name | ||
*/ | ||
public function getPausedWorkers(): array | ||
{ | ||
return (array) $this->redis->smembers(self::PAUSED_WORKER_KEY); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Через docker compose как-то проще за всем следить, и redis теперь нужен,