6
6
7
7
use DateTimeImmutable ;
8
8
use Exception ;
9
+ use Psr \Clock \ClockInterface ;
9
10
use Psr \Log \LoggerInterface ;
10
11
use Psr \Log \NullLogger ;
11
12
use SchedulerBundle \Event \TaskExecutedEvent ;
@@ -56,7 +57,8 @@ public function __construct(
56
57
private WorkerMiddlewareStack $ middlewareStack ,
57
58
private EventDispatcherInterface $ eventDispatcher ,
58
59
private LockFactory $ lockFactory ,
59
- ?LoggerInterface $ logger = null
60
+ ?LoggerInterface $ logger = null ,
61
+ private ?ClockInterface $ clock = null
60
62
) {
61
63
$ this ->configuration = WorkerConfiguration::create ();
62
64
$ this ->logger = $ logger ?? new NullLogger ();
@@ -292,15 +294,15 @@ protected function handleTask(TaskInterface $task, TaskListInterface $taskList):
292
294
$ this ->configuration ->setCurrentlyExecutedTask (task: $ task );
293
295
$ this ->eventDispatcher ->dispatch (event: new WorkerRunningEvent (worker: $ this ));
294
296
$ 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 ());
297
299
$ this ->taskExecutionTracker ->startTracking (task: $ task );
298
300
299
301
$ output = $ runner ->run (task: $ task , worker: $ this );
300
302
301
303
$ 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 ());
304
306
305
307
$ this ->defineTaskExecutionState (task: $ task , output: $ output );
306
308
@@ -364,10 +366,19 @@ private function checkTaskState(TaskInterface $task): bool
364
366
*/
365
367
private function getSleepDuration (): int
366
368
{
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 ();
369
370
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 ();
371
382
}
372
383
373
384
private function defineTaskExecutionState (TaskInterface $ task , Output $ output ): void
@@ -378,4 +389,12 @@ private function defineTaskExecutionState(TaskInterface $task, Output $output):
378
389
379
390
$ task ->setExecutionState (executionState: Output::ERROR === $ output ->getType () ? TaskInterface::ERRORED : TaskInterface::SUCCEED );
380
391
}
392
+
393
+ private function getCurrentDateAsDatetimeImmutable (): DateTimeImmutable
394
+ {
395
+ return $ this ->clock instanceof ClockInterface
396
+ ? $ this ->clock ->now ()
397
+ : new DateTimeImmutable ()
398
+ ;
399
+ }
381
400
}
0 commit comments