Skip to content
Open
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
19 changes: 16 additions & 3 deletions Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class ListCommand extends Command
{
private ObjectManager $em;

public function __construct(ManagerRegistry $managerRegistry, private readonly DateTimeFormatter $dateTimeFormatter, string $managerName)
public function __construct(ManagerRegistry $managerRegistry,
private readonly DateTimeFormatter|null $dateTimeFormatter,
string $managerName)
{
$this->em = $managerRegistry->getManager($managerName);
parent::__construct();
Expand All @@ -38,6 +40,17 @@ protected function configure(): void
->setHelp('This class is for listing all active commands.');
}

// https://github.com/Dukecity/CommandSchedulerBundle/issues/99
private function formatDiff(\DateTime $dateTime): string
{
if($this->dateTimeFormatter)
{
return $this->dateTimeFormatter->formatDiff($dateTime);
}

return $dateTime->format('Y-m-d H:i');
}

/**
* @throws \Exception
*/
Expand All @@ -64,11 +77,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
};

if($nextRunDate = $command->getNextRunDate())
{$nextRunDateText = $this->dateTimeFormatter->formatDiff($nextRunDate);}
{$nextRunDateText = $this->formatDiff($nextRunDate);}
else {$nextRunDateText = "";}

if($lastRunDate = $command->getLastExecution())
{$lastRunDateText = $this->dateTimeFormatter->formatDiff($lastRunDate);}
{$lastRunDateText = $this->formatDiff($lastRunDate);}
else {$lastRunDateText = "";}

$table->addRow([
Expand Down
17 changes: 14 additions & 3 deletions Command/MonitorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MonitorCommand extends Command
public function __construct(
private EventDispatcherInterface $eventDispatcher,
ManagerRegistry $managerRegistry,
private readonly DateTimeFormatter $dateTimeFormatter,
private readonly DateTimeFormatter|null $dateTimeFormatter,
string $managerName,
private int | bool $lockTimeout,
private array $receiver,
Expand Down Expand Up @@ -103,6 +103,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

// https://github.com/Dukecity/CommandSchedulerBundle/issues/99
private function formatDiff(\DateTime $dateTime): string
{
if($this->dateTimeFormatter)
{
return $this->dateTimeFormatter->formatDiff($dateTime);
}

return '';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why not returning a date string in this case ?

$datetime->format('Y-m-d')

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That would be duplicate the info in this format twice or not?

}

/**
* Print a table of locked Commands to console.
*
Expand Down Expand Up @@ -133,7 +144,7 @@ private function dump(OutputInterface $output, array $failedCommands): void
if($lastRunDate)
{
$lastRunDateText = $lastRunDate->format('Y-m-d H:i').' ('
.$this->dateTimeFormatter->formatDiff($command->getLastExecution()).')';
.$this->formatDiff($command->getLastExecution()).')';
}
else {
$lastRunDateText = '';
Expand All @@ -143,7 +154,7 @@ private function dump(OutputInterface $output, array $failedCommands): void
if($nextRunDate)
{
$nextRunDateText = $nextRunDate->format('Y-m-d H:i').' ('
.$this->dateTimeFormatter->formatDiff($command->getLastExecution()).')';
.$this->formatDiff($command->getLastExecution()).')';
}
else {
$nextRunDateText = '';
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
[
service('event_dispatcher'),
service('doctrine'),
service('time.datetime_formatter'),
service('time.datetime_formatter')->nullOnInvalid(),
'%dukecity_command_scheduler.doctrine_manager%',
'%dukecity_command_scheduler.lock_timeout%',
'%dukecity_command_scheduler.monitor_mail%',
Expand All @@ -115,7 +115,7 @@
->args(
[
service('doctrine'),
service('time.datetime_formatter'),
service('time.datetime_formatter')->nullOnInvalid(),
'%dukecity_command_scheduler.doctrine_manager%',
]
)
Expand Down