From 76f07c9ff17219cce3d8f1eb78c60c74a5b3b818 Mon Sep 17 00:00:00 2001 From: Menno Hoekstra Date: Mon, 21 Aug 2017 12:29:15 +0200 Subject: [PATCH 1/3] Update table with a new datetime column 'failed' so the user can check when the job was failed --- Console/Command/ListCommand.php | 2 +- Setup/UpgradeData.php | 55 +++++++++++++++++++++++++++++++++ etc/module.xml | 2 +- 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 Setup/UpgradeData.php 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/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/module.xml b/etc/module.xml index b5c2c761..a2b3e370 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -2,5 +2,5 @@ - + From cfca7a338230b02982829e5933d7b608e9146992 Mon Sep 17 00:00:00 2001 From: Menno Hoekstra Date: Mon, 21 Aug 2017 12:30:15 +0200 Subject: [PATCH 2/3] Configurable Max attempts for jobs | when max attempts is reached, update 'failed' with the current time --- Model/Queue.php | 15 ++++++++++++++- etc/config.xml | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Model/Queue.php b/Model/Queue.php index 377235c9..eb364408 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); } @@ -121,6 +129,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 +189,7 @@ private function getRunnableJobs() ) ->addFieldToFilter( ['attempts', 'attempts'], - [['lt' => 10], ['null' => 'null']] + [['lt' => $this->maxAttempts], ['null' => 'null']] ); } 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 From 785edd8760b64b1ec241fb4bf793bb1e89dfb982 Mon Sep 17 00:00:00 2001 From: Menno Hoekstra Date: Mon, 21 Aug 2017 12:32:26 +0200 Subject: [PATCH 3/3] Bugfix: user can now add a class/method to the queue when the previous job with the same class/method was failed --- Model/Queue.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Model/Queue.php b/Model/Queue.php index eb364408..70a3cf98 100644 --- a/Model/Queue.php +++ b/Model/Queue.php @@ -95,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()); }