Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from fieg/default-ttr
Browse files Browse the repository at this point in the history
Added default ttr configuration
  • Loading branch information
fieg committed Oct 28, 2015
2 parents b8f97d9 + 7e7198e commit 0c09b34
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/TreeHouse/WorkerBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function getConfigTreeBuilder()
->info('The id of a pheanstalk service. The service must implement Pheanstalk\\PheanstalkInterface')
;

// beanstalk settings
$queue = $rootNode->arrayNode('queue')->children();
$queue
->scalarNode('server')
Expand All @@ -42,6 +43,22 @@ public function getConfigTreeBuilder()
->info('Connection timeout')
;

// queue manager settings

$qm = $rootNode->arrayNode('queue_manager');
$qm
->addDefaultsIfNotSet()
->children()
->scalarNode('default_ttr')
->cannotBeEmpty()
->defaultValue(1200)
->isRequired()
->info('Default time-to-run for a job')
->end()
->end()
;


return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ public function configurePheanstalk(array $config, ContainerBuilder $container)

$pheanstalk = new Definition(Pheanstalk::class, $pheanstalkConfig);
$queueManager->replaceArgument(0, $pheanstalk);

// set default ttr
$queueManager->addMethodCall('setDefaultTtr', [$config['queue_manager']['default_ttr']]);
}
}
19 changes: 18 additions & 1 deletion src/TreeHouse/WorkerBundle/QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class QueueManager
*/
protected $resolvers = [];

/**
* @var int
*/
protected $defaultTtr = PheanstalkInterface::DEFAULT_TTR;

/**
* @param PheanstalkInterface $pheanstalk
* @param EventDispatcherInterface $dispatcher
Expand All @@ -73,6 +78,18 @@ public function __construct(PheanstalkInterface $pheanstalk, EventDispatcherInte
$this->logger = $logger ?: new NullLogger();
}

/**
* @param int $defaultTtr
*
* @return $this
*/
public function setDefaultTtr($defaultTtr)
{
$this->defaultTtr = $defaultTtr;

return $this;
}

/**
* @return PheanstalkInterface
*/
Expand Down Expand Up @@ -203,7 +220,7 @@ public function add($action, array $payload, $delay = null, $priority = null, $t
}

if (null === $ttr) {
$ttr = PheanstalkInterface::DEFAULT_TTR;
$ttr = $this->defaultTtr;
}

if (!is_numeric($delay)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,53 @@ public function getInvalidConfigurations()
],
];
}

public function testDefaultTtr()
{
$config = [
'tree_house.worker' => [
'queue' => [
'server' => 'localhost',
'port' => 1234,
'timeout' => 60,
],
'queue_manager' => [
'default_ttr' => 600,
],
],
];

$this->extension->load($config, $this->container);
$this->container->compile();

$this->assertTrue($this->container->hasDefinition('tree_house.worker.queue_manager'));

$definition = $this->container->getDefinition('tree_house.worker.queue_manager');
$methodCalls = $definition->getMethodCalls();

$this->assertContains(['setDefaultTtr', [600]], $methodCalls);
}

public function testDefaultDefaultTtrIs1200()
{
$config = [
'tree_house.worker' => [
'queue' => [
'server' => 'localhost',
'port' => 1234,
'timeout' => 60,
]
],
];

$this->extension->load($config, $this->container);
$this->container->compile();

$this->assertTrue($this->container->hasDefinition('tree_house.worker.queue_manager'));

$definition = $this->container->getDefinition('tree_house.worker.queue_manager');
$methodCalls = $definition->getMethodCalls();

$this->assertContains(['setDefaultTtr', [1200]], $methodCalls);
}
}

0 comments on commit 0c09b34

Please sign in to comment.