Skip to content

Commit

Permalink
Updated sys:cron:history
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Aug 13, 2024
1 parent 3d7c002 commit df447e4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 37 deletions.
93 changes: 58 additions & 35 deletions src/N98/Magento/Command/System/Cron/HistoryCommand.php
Original file line number Diff line number Diff line change
@@ -1,77 +1,100 @@
<?php

declare(strict_types=1);

namespace N98\Magento\Command\System\Cron;

use Mage;
use Mage_Core_Exception;
use Mage_Core_Model_Store_Exception;
use Mage_Cron_Model_Schedule;
use N98\Magento\Command\AbstractMagentoCommand;
use N98\Magento\Command\CommandFormatable;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Varien_Data_Collection_Db;

/**
* List cronjob history command
*
* @package N98\Magento\Command\System\Cron
*/
class HistoryCommand extends AbstractMagentoCommand
class HistoryCommand extends AbstractMagentoCommand implements CommandFormatable
{
public const COMMAND_OPTION_TIMEZONE = 'timezone';

/**
* @var string
*/
protected static $defaultName = 'sys:cron:history';

/**
* @var array
* @var string
*/
protected $infos;
protected static $defaultDescription = 'Last executed cronjobs with status.';

protected function configure()
/**
* {@inheritDoc}
*/
protected function configure(): void
{
$this
->setName('sys:cron:history')
->setDescription('Last executed cronjobs with status.')
->addOption(
'timezone',
null,
InputOption::VALUE_OPTIONAL,
'Timezone to show finished at in'
)
->addFormatOption()
;
$this->addOption(
self::COMMAND_OPTION_TIMEZONE,
null,
InputOption::VALUE_OPTIONAL,
'Timezone to show finished at in'
);

parent::configure();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
public function getSectionTitle(InputInterface $input, OutputInterface $output): string
{
$this->detectMagento($output, true);
return 'Last executed jobs';
}

if ($input->getOption('format') === null) {
$this->writeSection($output, 'Last executed jobs');
}
$this->initMagento();
/**
* {@inheritDoc}
*/
public function getListHeader(InputInterface $input, OutputInterface $output): array
{
return ['Job', 'Status', 'Finished'];
}

$timezone = $input->getOption('timezone') ?: Mage::app()->getStore()->getConfig('general/locale/timezone');
/**
* {@inheritDoc}
* @throws Mage_Core_Model_Store_Exception|Mage_Core_Exception
*/
public function getListData(InputInterface $input, OutputInterface $output): array
{
$timezone = $input->getOption(self::COMMAND_OPTION_TIMEZONE)
?: Mage::app()->getStore()->getConfig('general/locale/timezone');

$output->writeln('<info>Times shown in <comment>' . $timezone . '</comment></info>');
$output->writeln(sprintf('<info>Times shown in <comment>%s</comment></info>', $timezone));

$date = Mage::getSingleton('core/date');
$offset = $date->calculateOffset($timezone);
$collection = Mage::getModel('cron/schedule')->getCollection();
$collection
->addFieldToFilter('status', ['neq' => Mage_Cron_Model_Schedule::STATUS_PENDING])
->addOrder('finished_at', Varien_Data_Collection_Db::SORT_ORDER_DESC);
->addOrder('finished_at');

$table = [];
/** @var Mage_Cron_Model_Schedule $job */
foreach ($collection as $job) {
$table[] = [$job->getJobCode(), $job->getStatus(), $job->getFinishedAt() ? $date->gmtDate(null, $date->timestamp($job->getFinishedAt()) + $offset) : ''];
$table[] = [
$job->getJobCode(),
$job->getStatus(),
$job->getFinishedAt() ? $date->gmtDate(
null,
$date->timestamp($job->getFinishedAt()) + $offset
) : ''
];
}

$tableHelper = $this->getTableHelper();
$tableHelper
->setHeaders(['Job', 'Status', 'Finished'])
->renderByFormat($output, $table, $input->getOption('format'));
return 0;
return $table;
}
}
4 changes: 2 additions & 2 deletions src/N98/Magento/Command/System/Cron/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class ListCommand extends AbstractCronCommand implements CommandFormatable
/**
* @var string
*/
public static $defaultName = 'sys:cron:list';
protected static $defaultName = 'sys:cron:list';

/**
* @var string
*/
public static $defaultDescription = 'Lists all cronjobs.';
protected static $defaultDescription = 'Lists all cronjobs.';

/**
* @var array|null
Expand Down

0 comments on commit df447e4

Please sign in to comment.