Skip to content

Configurable max attempt | Failed flag | Bugfix for queue #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 21 additions & 2 deletions Model/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -176,7 +195,7 @@ private function getRunnableJobs()
)
->addFieldToFilter(
['attempts', 'attempts'],
[['lt' => 10], ['null' => 'null']]
[['lt' => $this->maxAttempts], ['null' => 'null']]
);
}

Expand Down
55 changes: 55 additions & 0 deletions Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Springbot\Queue\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;

/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->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'
]
);
}

}
}
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<springbot>
<queue>
<max_jobs>1</max_jobs>
<max_attempts>10</max_attempts>
</queue>
</springbot>
</default>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Springbot_Queue" setup_version="1.1.7"/>
<module name="Springbot_Queue" setup_version="1.1.8"/>
</config>