-
-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters