Skip to content
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

Merged
merged 1 commit into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: "ramsey/composer-install@v2"
with:
composer-options: "--ignore-platform-reqs --optimize-autoloader"
- name: Build the stack
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Через docker compose как-то проще за всем следить, и redis теперь нужен,

run: docker-compose up -d

- name: PHPUnit Tests
uses: php-actions/phpunit@v3
with:
version: 9.5.20
configuration: phpunit.xml.dist
php_version: 8.1
php_extensions: posix
- name: Install dependencies
run: docker-compose run php composer install

- name: Run PHPUnit Tests
run: docker-compose run php vendor/bin/phpunit
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"cli",
"redis"
],
"homepage": "http://kamisama.github.com/Fresque/",
"authors": [
{
"name": "Oleg Topchiy",
Expand All @@ -27,9 +26,10 @@
"require": {
"php": ">=8.1",
"ext-posix": "*",
"ext-pcntl": "*",
"ext-redis": "*",
"zetacomponents/console-tools": "^1.7",
"zetacomponents/base": "^1.9",
"kamisama/resque-status": ">=0.0.1",
"kamisama/php-resque-ex": ">=1.2.5",
"kamisama/php-resque-ex-scheduler": "~1.2.2"
},
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yml
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
8 changes: 8 additions & 0 deletions docker/php/Dockerfile
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
9 changes: 4 additions & 5 deletions lib/Fresque.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

namespace Fresque;

use ResqueStatus\ResqueStatus;
use ezcConsoleInput;
use ezcConsoleOutput;
use DateTime;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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']);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Автозамена, так бы убрал call_user_func, не знаю зачем он его везде использовал.

$options->signal = 'CONT';
$options->successCallback = function ($pid, $workerName) use ($ResqueStatus) {
$ResqueStatus->setPausedWorker($workerName, false);
Expand Down Expand Up @@ -665,7 +664,7 @@ public function sendSignal($options)
);
};
} else {
$listFormatter = $option->formatListItem;
$listFormatter = $options->formatListItem;
}

if (empty($options->workers)) {
Expand Down Expand Up @@ -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)) {
Expand Down
143 changes: 143 additions & 0 deletions lib/ResqueStatus.php
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
Copy link
Member Author

Choose a reason for hiding this comment

The 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);
}
}
Loading