diff --git a/Console/Command/ListCommand.php b/Console/Command/ListCommand.php index 0c01201d..feb54b18 100644 --- a/Console/Command/ListCommand.php +++ b/Console/Command/ListCommand.php @@ -56,7 +56,7 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $headers = ['queue', 'class', 'method', 'args', 'priority', 'attempts', 'error']; + $headers = ['queue', 'class', 'method', 'args', 'priority', 'attempts', 'error', 'failed']; $this->_jobCollection->setCurPage($input->getArgument(self::PAGE_ARGUMENT)); $this->_jobCollection->setPageSize($input->getArgument(self::PER_PAGE_ARGUMENT)); $this->_jobCollection->addOrder('priority', JobCollection::SORT_ORDER_ASC); diff --git a/Model/Queue.php b/Model/Queue.php index 377235c9..70a3cf98 100644 --- a/Model/Queue.php +++ b/Model/Queue.php @@ -16,6 +16,12 @@ class Queue extends AbstractModel protected $jobCollection; protected $scopeConfig; + /** + * Max attempts for a job + * @var integer + */ + protected $maxAttempts; + /** * @param JobFactory $jobFactory * @param JobCollection $jobCollection @@ -34,6 +40,8 @@ public function __construct( $this->jobFactory = $jobFactory; $this->jobCollection = $jobCollection; $this->scopeConfig = $scopeConfigInterface; + $this->maxAttempts = $this->scopeConfig->getValue('springbot/queue/max_attempts'); + parent::__construct($context, $registry); } @@ -87,7 +95,13 @@ public function scheduleJob( public function jobExists($hash) { - $collection = $this->jobCollection->addFieldToFilter('hash', $hash); + $collection = $this->jobCollection + ->addFieldToFilter('hash', $hash) + ->addFieldToFilter( + ['attempts', 'attempts'], + [['lt' => $this->maxAttempts], ['null' => 'null']] + ); + $item = $collection->getFirstItem(); return (!$item->isEmpty()); } @@ -121,6 +135,11 @@ public function runNextJob() $attempts = $nextJob->getData('attempts'); $attempts = (!$attempts) ? 0 : $attempts; $attempts++; + + if ($attempts >= $this->maxAttempts) { + $nextJob->setData('failed', date('Y-m-d H:i:s')); + } + $nextJob->setData('error', $e->getMessage()); $nextJob->setData('attempts', $attempts); $nextJob->setNextRunAt(); @@ -176,7 +195,7 @@ private function getRunnableJobs() ) ->addFieldToFilter( ['attempts', 'attempts'], - [['lt' => 10], ['null' => 'null']] + [['lt' => $this->maxAttempts], ['null' => 'null']] ); } diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php new file mode 100644 index 00000000..59c7aaaa --- /dev/null +++ b/Setup/UpgradeData.php @@ -0,0 +1,55 @@ +eavSetupFactory = $eavSetupFactory; + } + + /** + * Upgrades data for a module + * + * @param ModuleDataSetupInterface $setup + * @param ModuleContextInterface $context + * @return void + */ + public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $installer = $setup; + $installer->startSetup(); + + if ($context->getVersion() && version_compare($context->getVersion(), '1.1.8') < 0) { + $installer->getConnection()->addColumn( + $installer->getTable('springbot_queue'), + 'failed', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME, + 'nullable' => true, + 'comment' => 'Date of failed job' + ] + ); + } + + } +} \ No newline at end of file diff --git a/etc/config.xml b/etc/config.xml index f702822c..158c66f6 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -6,6 +6,7 @@ 1 + 10 diff --git a/etc/module.xml b/etc/module.xml index b5c2c761..a2b3e370 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -2,5 +2,5 @@ - +