Skip to content

Commit eb3317c

Browse files
committed
Added "scheduler" command for show scheduled task list
1 parent 60333d2 commit eb3317c

File tree

8 files changed

+106
-7
lines changed

8 files changed

+106
-7
lines changed

src/Internal/Scheduler/Scheduler.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Luzrain\PHPStreamServer\Internal\Scheduler\Trigger\TriggerInterface;
1212
use Luzrain\PHPStreamServer\Internal\SIGCHLDHandler;
1313
use Luzrain\PHPStreamServer\Internal\Status;
14+
use Luzrain\PHPStreamServer\MasterProcess;
15+
use Luzrain\PHPStreamServer\Message\ProcessScheduledEvent;
1416
use Luzrain\PHPStreamServer\PeriodicProcessInterface;
1517
use Psr\Log\LoggerInterface;
1618
use Revolt\EventLoop;
@@ -27,8 +29,10 @@ final class Scheduler
2729
private Suspension $suspension;
2830
private DeferredFuture|null $stopFuture = null;
2931

30-
public function __construct(private Status &$status)
31-
{
32+
public function __construct(
33+
private readonly MasterProcess $masterProcess,
34+
private Status &$status,
35+
) {
3236
$this->pool = new WorkerPool();
3337
}
3438

@@ -59,7 +63,7 @@ public function start(Suspension $suspension, LoggerInterface $logger): void
5963

6064
private function scheduleWorker(PeriodicProcessInterface $worker, TriggerInterface $trigger): bool
6165
{
62-
if ($this->status !== Status::RUNNING) {
66+
if ($this->status === Status::SHUTDOWN) {
6367
return false;
6468
}
6569

@@ -68,9 +72,15 @@ private function scheduleWorker(PeriodicProcessInterface $worker, TriggerInterfa
6872

6973
if ($nextRunDate !== null) {
7074
$delay = $nextRunDate->getTimestamp() - $currentDate->getTimestamp();
71-
EventLoop::delay($delay, fn() => $this->startWorker($worker, $trigger));
75+
EventLoop::delay($delay, function () use($worker, $trigger): void {
76+
$this->startWorker($worker, $trigger);
77+
});
7278
}
7379

80+
EventLoop::defer(function () use ($worker, $nextRunDate): void {
81+
$this->masterProcess->dispatch(new ProcessScheduledEvent($worker->getId(), $nextRunDate));
82+
});
83+
7484
return true;
7585
}
7686

src/Internal/Supervisor/Supervisor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ final class Supervisor
3232

3333
public function __construct(
3434
private readonly MasterProcess $masterProcess,
35-
private readonly int $stopTimeout,
3635
private Status &$status,
36+
private readonly int $stopTimeout,
3737
) {
3838
$this->workerPool = new WorkerPool();
3939
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Luzrain\PHPStreamServer\Internal\SystemPlugin\Command;
6+
7+
use Luzrain\PHPStreamServer\Internal\Console\Command;
8+
use Luzrain\PHPStreamServer\Internal\Console\Options;
9+
use Luzrain\PHPStreamServer\Internal\Console\Table;
10+
use Luzrain\PHPStreamServer\Internal\Scheduler\Trigger\TriggerFactory;
11+
use Luzrain\PHPStreamServer\Internal\SystemPlugin\ServerStatus\PeriodicWorkerInfo;
12+
use Luzrain\PHPStreamServer\Internal\SystemPlugin\ServerStatus\ServerStatus;
13+
14+
/**
15+
* @internal
16+
*/
17+
final class SchedulerCommand extends Command
18+
{
19+
protected const COMMAND = 'scheduler';
20+
protected const DESCRIPTION = 'Show scheduler map';
21+
22+
public function execute(Options $options): int
23+
{
24+
echo "❯ Scheduler\n";
25+
26+
if(!$this->masterProcess->isRunning()) {
27+
echo " <color;bg=yellow> ! </> <color;fg=yellow>Server is not running</>\n";
28+
29+
return 0;
30+
}
31+
32+
$status = $this->masterProcess->get(ServerStatus::class);
33+
\assert($status instanceof ServerStatus);
34+
35+
if ($status->getPeriodicTasksCount() > 0) {
36+
echo (new Table(indent: 1))
37+
->setHeaderRow([
38+
'User',
39+
'Worker',
40+
'Schedule',
41+
'Next run',
42+
'Status',
43+
])
44+
->addRows(\array_map(array: $status->getPeriodicWorkers(), callback: static fn (PeriodicWorkerInfo $w) => [
45+
$w->user === 'root' ? $w->user : "<color;fg=gray>{$w->user}</>",
46+
$w->name,
47+
$w->schedule ?: '-',
48+
$w->nextRunDate?->format('Y-m-d H:i:s') ?? '<color;fg=gray>-</>',
49+
match(true) {
50+
$w->nextRunDate !== null => '[<color;fg=green>OK</>]',
51+
default => '[<color;fg=red>ERROR</>]',
52+
},
53+
]));
54+
} else {
55+
echo " <color;bg=yellow> ! </> <color;fg=yellow>There are no scheduled tasks</>\n";
56+
}
57+
58+
return 0;
59+
}
60+
}

src/Internal/SystemPlugin/ServerStatus/PeriodicWorkerInfo.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ final class PeriodicWorkerInfo
99
public function __construct(
1010
public string $user,
1111
public string $name,
12+
public string $schedule,
13+
public \DateTimeInterface|null $nextRunDate = null,
1214
) {
1315
}
1416
}

src/Internal/SystemPlugin/ServerStatus/ServerStatus.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Luzrain\PHPStreamServer\Message\ProcessDetachedEvent;
1313
use Luzrain\PHPStreamServer\Message\ProcessExitEvent;
1414
use Luzrain\PHPStreamServer\Message\ProcessHeartbeatEvent;
15+
use Luzrain\PHPStreamServer\Message\ProcessScheduledEvent;
1516
use Luzrain\PHPStreamServer\Message\ProcessSpawnedEvent;
1617
use Luzrain\PHPStreamServer\Message\RequestCounterIncreaseEvent;
1718
use Luzrain\PHPStreamServer\Message\RxCounterIncreaseEvent;
@@ -129,6 +130,10 @@ public function subscribeToWorkerMessages(MessageHandler $handler): void
129130
$handler->subscribe(ConnectionClosedEvent::class, weakClosure(function (ConnectionClosedEvent $message): void {
130131
unset($this->processes[$message->pid]->connections[$message->connectionId]);
131132
}));
133+
134+
$handler->subscribe(ProcessScheduledEvent::class, weakClosure(function (ProcessScheduledEvent $message): void {
135+
$this->periodicWorkers[$message->id]->nextRunDate = $message->nextRunDate;
136+
}));
132137
}
133138

134139
public function addWorker(ProcessInterface $worker): void
@@ -143,6 +148,7 @@ public function addWorker(ProcessInterface $worker): void
143148
$this->periodicWorkers[$worker->getId()] = new PeriodicWorkerInfo(
144149
user: $worker->getUser(),
145150
name: $worker->getName(),
151+
schedule: $worker->getSchedule(),
146152
);
147153
}
148154
}

src/Internal/SystemPlugin/System.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Luzrain\PHPStreamServer\Internal\SystemPlugin\Command\ConnectionsCommand;
88
use Luzrain\PHPStreamServer\Internal\SystemPlugin\Command\ProcessesCommand;
99
use Luzrain\PHPStreamServer\Internal\SystemPlugin\Command\ReloadCommand;
10+
use Luzrain\PHPStreamServer\Internal\SystemPlugin\Command\SchedulerCommand;
1011
use Luzrain\PHPStreamServer\Internal\SystemPlugin\Command\StartCommand;
1112
use Luzrain\PHPStreamServer\Internal\SystemPlugin\Command\StatusCommand;
1213
use Luzrain\PHPStreamServer\Internal\SystemPlugin\Command\StopCommand;
@@ -53,6 +54,7 @@ public function commands(): array
5354
new WorkersCommand($this->masterProcess),
5455
new ProcessesCommand($this->masterProcess),
5556
new ConnectionsCommand($this->masterProcess),
57+
new SchedulerCommand($this->masterProcess),
5658
];
5759
}
5860
}

src/MasterProcess.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public function __construct(
8484
$this->pidFile = $pidFile ?? \sprintf('%s/phpss%s.pid', $runDirectory, \hash('xxh32', $this->startFile));
8585
$this->socketFile = \sprintf('%s/phpss%s.socket', $runDirectory, \hash('xxh32', $this->startFile . 'rx'));
8686

87-
$this->supervisor = new Supervisor($this, $stopTimeout, $this->status);
88-
$this->scheduler = new Scheduler($this->status);
87+
$this->supervisor = new Supervisor($this, $this->status, $stopTimeout);
88+
$this->scheduler = new Scheduler($this, $this->status);
8989
$this->container = new ArrayContainer();
9090
}
9191

src/Message/ProcessScheduledEvent.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Luzrain\PHPStreamServer\Message;
6+
7+
use Luzrain\PHPStreamServer\Message;
8+
9+
/**
10+
* @implements Message<void>
11+
*/
12+
final readonly class ProcessScheduledEvent implements Message
13+
{
14+
public function __construct(
15+
public int $id,
16+
public \DateTimeInterface|null $nextRunDate,
17+
) {
18+
}
19+
}

0 commit comments

Comments
 (0)