Skip to content

Commit f0b26e9

Browse files
committed
feat(core): ClockInterface support started
1 parent f5ee3f2 commit f0b26e9

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
"phpstan/phpstan-symfony": "^1.2.19",
144144
"phpunit/phpunit": "^9.5.26",
145145
"psr/cache": "^1.0 || ^2.0 || ^3.0",
146+
"psr/clock": "^1.0",
146147
"rector/rector": "0.15.10",
147148
"symfony/cache": "^5.4 || ^6.0",
148149
"symfony/dependency-injection": "^5.4 || ^6.0",

src/Probe/Probe.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SchedulerBundle\Probe;
66

77
use DateTimeImmutable;
8+
use Psr\Clock\ClockInterface;
89
use SchedulerBundle\SchedulerInterface;
910
use SchedulerBundle\Task\TaskInterface;
1011
use SchedulerBundle\Worker\WorkerInterface;
@@ -17,7 +18,8 @@ final class Probe implements ProbeInterface
1718
{
1819
public function __construct(
1920
private SchedulerInterface $scheduler,
20-
private WorkerInterface $worker
21+
private WorkerInterface $worker,
22+
private ?ClockInterface $clock = null,
2123
) {
2224
}
2325

@@ -26,29 +28,42 @@ public function __construct(
2628
*/
2729
public function getExecutedTasks(): int
2830
{
29-
return $this->scheduler->getTasks()->filter(filter: static function (TaskInterface $task): bool {
31+
$tasks = $this->scheduler->getTasks();
32+
33+
$filteredTasks = $tasks->filter(filter: function (TaskInterface $task): bool {
3034
$lastExecutionDate = $task->getLastExecution();
3135
if (!$lastExecutionDate instanceof DateTimeImmutable) {
3236
return false;
3337
}
3438

35-
return $lastExecutionDate->format(format: 'Y-m-d h:i') === (new DateTimeImmutable())->format(format: 'Y-m-d h:i');
36-
})->count();
39+
$currentDate = $this->clock instanceof ClockInterface
40+
? $this->clock->now()->format(format: 'Y-m-d h:i')
41+
: (new DateTimeImmutable())->format(format: 'Y-m-d h:i')
42+
;
43+
44+
return $lastExecutionDate->format(format: 'Y-m-d h:i') === $currentDate;
45+
});
46+
47+
return $filteredTasks->count();
3748
}
3849

3950
/**
4051
* {@inheritdoc}
4152
*/
4253
public function getFailedTasks(): int
4354
{
44-
return $this->worker->getFailedTasks()->count();
55+
$failedTasks = $this->worker->getFailedTasks();
56+
57+
return $failedTasks->count();
4558
}
4659

4760
/**
4861
* {@inheritdoc}
4962
*/
5063
public function getScheduledTasks(): int
5164
{
52-
return $this->scheduler->getTasks()->filter(filter: static fn (TaskInterface $task): bool => null !== $task->getScheduledAt())->count();
65+
$tasks = $this->scheduler->getTasks();
66+
67+
return $tasks->filter(filter: static fn (TaskInterface $task): bool => null !== $task->getScheduledAt())->count();
5368
}
5469
}

src/Scheduler.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use DateTimeImmutable;
1111
use DateTimeZone;
1212
use Exception;
13+
use Psr\Clock\ClockInterface;
1314
use SchedulerBundle\Event\TaskExecutingEvent;
1415
use SchedulerBundle\Exception\InvalidArgumentException;
1516
use SchedulerBundle\Exception\TransportException;
@@ -56,10 +57,14 @@ public function __construct(
5657
private TransportInterface $transport,
5758
private SchedulerMiddlewareStack $middlewareStack,
5859
private EventDispatcherInterface $eventDispatcher,
59-
private ?MessageBusInterface $bus = null
60+
private ?MessageBusInterface $bus = null,
61+
private ?ClockInterface $clock = null
6062
) {
6163
$this->timezone = new DateTimeZone(timezone: $timezone);
62-
$this->initializationDate = new DateTimeImmutable(datetime: 'now', timezone: $this->timezone);
64+
$this->initializationDate = $clock instanceof ClockInterface
65+
? $clock->now()->setTimezone(timezone: $this->timezone)
66+
: new DateTimeImmutable(timezone: $this->timezone)
67+
;
6368

6469
$this->minSynchronizationDelay = new DateInterval(duration: 'PT1S');
6570
$this->maxSynchronizationDelay = new DateInterval(duration: 'P1D');
@@ -311,7 +316,11 @@ public function getPoolConfiguration(): SchedulerConfiguration
311316
*/
312317
private function getSynchronizedCurrentDate(): DateTimeImmutable
313318
{
314-
$currentDate = new DateTimeImmutable(datetime: 'now', timezone: $this->timezone);
319+
$currentDate = $this->clock instanceof ClockInterface
320+
? $this->clock->now()->setTimezone(timezone: $this->timezone)
321+
: new DateTimeImmutable(datetime: 'now', timezone: $this->timezone)
322+
;
323+
315324
$currentDateIntervalWithInitialization = $this->initializationDate->diff(targetObject: $currentDate);
316325

317326
$currentDateWithMinInterval = $currentDate->add(interval: $this->minSynchronizationDelay);

src/Worker/Worker.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use DateTimeImmutable;
88
use Exception;
9+
use Psr\Clock\ClockInterface;
910
use Psr\Log\LoggerInterface;
1011
use Psr\Log\NullLogger;
1112
use SchedulerBundle\Event\TaskExecutedEvent;
@@ -56,7 +57,8 @@ public function __construct(
5657
private WorkerMiddlewareStack $middlewareStack,
5758
private EventDispatcherInterface $eventDispatcher,
5859
private LockFactory $lockFactory,
59-
?LoggerInterface $logger = null
60+
?LoggerInterface $logger = null,
61+
private ?ClockInterface $clock = null
6062
) {
6163
$this->configuration = WorkerConfiguration::create();
6264
$this->logger = $logger ?? new NullLogger();
@@ -292,15 +294,15 @@ protected function handleTask(TaskInterface $task, TaskListInterface $taskList):
292294
$this->configuration->setCurrentlyExecutedTask(task: $task);
293295
$this->eventDispatcher->dispatch(event: new WorkerRunningEvent(worker: $this));
294296
$this->eventDispatcher->dispatch(event: new TaskExecutingEvent(task: $task, worker: $this, currentTasks: $taskList));
295-
$task->setArrivalTime(dateTimeImmutable: new DateTimeImmutable());
296-
$task->setExecutionStartTime(dateTimeImmutable: new DateTimeImmutable());
297+
$task->setArrivalTime(dateTimeImmutable: $this->getCurrentDateAsDatetimeImmutable());
298+
$task->setExecutionStartTime(dateTimeImmutable: $this->getCurrentDateAsDatetimeImmutable());
297299
$this->taskExecutionTracker->startTracking(task: $task);
298300

299301
$output = $runner->run(task: $task, worker: $this);
300302

301303
$this->taskExecutionTracker->endTracking(task: $task);
302-
$task->setExecutionEndTime(dateTimeImmutable: new DateTimeImmutable());
303-
$task->setLastExecution(dateTimeImmutable: new DateTimeImmutable());
304+
$task->setExecutionEndTime(dateTimeImmutable: $this->getCurrentDateAsDatetimeImmutable());
305+
$task->setLastExecution(dateTimeImmutable: $this->getCurrentDateAsDatetimeImmutable());
304306

305307
$this->defineTaskExecutionState(task: $task, output: $output);
306308

@@ -364,10 +366,19 @@ private function checkTaskState(TaskInterface $task): bool
364366
*/
365367
private function getSleepDuration(): int
366368
{
367-
$nextMinute = new DateTimeImmutable(datetime: '+ 1 minute', timezone: $this->scheduler->getTimezone());
368-
$updatedNextExecutionDate = $nextMinute->setTime(hour: (int) $nextMinute->format('H'), minute: (int) $nextMinute->format('i'));
369+
$schedulerTimezone = $this->scheduler->getTimezone();
369370

370-
return (new DateTimeImmutable(datetime: 'now', timezone: $this->scheduler->getTimezone()))->diff(targetObject: $updatedNextExecutionDate)->s + $this->configuration->getSleepDurationDelay();
371+
$currentDatetimeMinusOneMinute = $this->clock instanceof ClockInterface
372+
? $this->clock->now()->setTimezone(timezone: $schedulerTimezone)->modify('-1 minute')
373+
: new DateTimeImmutable(datetime: '+ 1 minute', timezone: $schedulerTimezone)
374+
;
375+
376+
$updatedNextExecutionDate = $currentDatetimeMinusOneMinute->setTime(
377+
hour: (int) $currentDatetimeMinusOneMinute->format('H'),
378+
minute: (int) $currentDatetimeMinusOneMinute->format('i')
379+
);
380+
381+
return (new DateTimeImmutable(datetime: 'now', timezone: $schedulerTimezone))->diff(targetObject: $updatedNextExecutionDate)->s + $this->configuration->getSleepDurationDelay();
371382
}
372383

373384
private function defineTaskExecutionState(TaskInterface $task, Output $output): void
@@ -378,4 +389,12 @@ private function defineTaskExecutionState(TaskInterface $task, Output $output):
378389

379390
$task->setExecutionState(executionState: Output::ERROR === $output->getType() ? TaskInterface::ERRORED : TaskInterface::SUCCEED);
380391
}
392+
393+
private function getCurrentDateAsDatetimeImmutable(): DateTimeImmutable
394+
{
395+
return $this->clock instanceof ClockInterface
396+
? $this->clock->now()
397+
: new DateTimeImmutable()
398+
;
399+
}
381400
}

0 commit comments

Comments
 (0)