Skip to content

Commit

Permalink
Added features. Worker execution and all files affected
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc committed Dec 13, 2011
1 parent c3f512d commit 5823dcd
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 92 deletions.
45 changes: 0 additions & 45 deletions Command/GearmanClientExecuteCommand.php
Original file line number Diff line number Diff line change
@@ -1,45 +0,0 @@
<?php

namespace Mmoreramerino\GearmanBundle\Command;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

/**
* Gearman Client Execute Command class
*
* @author Marc Morera <marc@ulabox.com>
*/
class GearmanClientExecuteCommand extends ContainerAwareCommand
{
/**
* Console Command configuration
*/
protected function configure()
{
parent::configure();
$this->setName('gearman:client:execute')
->setDescription('Test')
->addArgument('job', InputArgument::REQUIRED, 'job to execute');
}

/**
* Executes the current command.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return integer 0 if everything went fine, or an error code
*
* @throws \LogicException When this abstract class is not implemented
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$job = $input->getArgument('job');
$this->getContainer()->get('gearman')->callJob($job);
}
}
6 changes: 3 additions & 3 deletions Command/GearmanJobDescribeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function configure()
parent::configure();
$this->setName('gearman:job:describe')
->setDescription('Describe given job')
->addArgument('job', InputArgument::REQUIRED, 'job to execute');
->addArgument('job', InputArgument::REQUIRED, 'job to describe');
}

/**
Expand All @@ -40,7 +40,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$job = $input->getArgument('job');
$worker = $this->getContainer()->get('gearman')->getWorker($job);
$this->getContainer()->get('gearman.describer')->describeJob($output, $worker);
$job = $this->getContainer()->get('gearman')->getJob($job);
$this->getContainer()->get('gearman.describer')->describeJob($output, $job);
}
}
46 changes: 46 additions & 0 deletions Command/GearmanWorkerDescribeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Mmoreramerino\GearmanBundle\Command;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

/**
* Gearman Job Describe Command class
*
* @author Marc Morera <marc@ulabox.com>
*/
class GearmanWorkerDescribeCommand extends ContainerAwareCommand
{
/**
* Console Command configuration
*/
protected function configure()
{
parent::configure();
$this->setName('gearman:worker:describe')
->setDescription('Describe given worker')
->addArgument('worker', InputArgument::REQUIRED, 'worker to describe');
}

/**
* Executes the current command.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return integer 0 if everything went fine, or an error code
*
* @throws \LogicException When this abstract class is not implemented
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$worker = $input->getArgument('worker');
$worker = $this->getContainer()->get('gearman')->getWorker($worker);
$this->getContainer()->get('gearman.describer')->describeWorker($output, $worker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
*
* @author Marc Morera <marc@ulabox.com>
*/
class GearmanJobExecuteCommand extends ContainerAwareCommand
class GearmanWorkerExecuteCommand extends ContainerAwareCommand
{
/**
* Console Command configuration
*/
protected function configure()
{
parent::configure();
$this->setName('gearman:job:execute')
->setDescription('Execute one job of worker')
->addArgument('job', InputArgument::REQUIRED, 'job to execute');
$this->setName('gearman:worker:execute')
->setDescription('Execute one worker with all contained Jobs')
->addArgument('worker', InputArgument::REQUIRED, 'work to execute');
}

/**
Expand All @@ -45,10 +45,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
$output->writeln('<info>loading...</info>');

$job = $input->getArgument('job');
$worker = $this->getContainer()->get('gearman')->getWorker($job);
$this->getContainer()->get('gearman.describer')->describeJob($output, $worker);
$worker = $input->getArgument('worker');
$workerStruct = $this->getContainer()->get('gearman')->getWorker($worker);
$this->getContainer()->get('gearman.describer')->describeWorker($output, $workerStruct, true);
$output->writeln('<info>loaded. Ctrl+C to break</info>');
$this->getContainer()->get('gearman.execute.job')->executeJob($job);
$this->getContainer()->get('gearman.execute')->executeWorker($worker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
*
* @author Marc Morera <marc@ulabox.com>
*/
class GearmanJobListCommand extends ContainerAwareCommand
class GearmanWorkerListCommand extends ContainerAwareCommand
{
/**
* Console Command configuration
*/
protected function configure()
{
parent::configure();
$this->setName('gearman:job:list')
->setDescription('List all Gearman Jobs');
$this->setName('gearman:worker:list')
->setDescription('List all Gearman Workers and their Jobs');
}

/**
Expand All @@ -43,8 +43,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (is_array($workers)) {

foreach ($workers as $worker) {
$output->writeln('<info> @'.$worker['className'].'</info>');
$output->writeln('<info></info>');
$output->writeln('<comment> @Worker: </comment><info>'.$worker['className'].'</info>');
$output->writeln('<comment> callablename: </comment><info>'.$worker['callableName'].'</info>');
$output->writeln('<comment> Jobs:</comment>');
foreach ($worker['jobs'] as $job) {
$output->writeln('<comment> - #'.$it++.'</comment>');
$output->writeln('<comment> name: '.$job['methodName'].'</comment>');
Expand Down
25 changes: 25 additions & 0 deletions Exceptions/WorkerDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Mmoreramerino\GearmanBundle\Exceptions;

/**
* GearmanBundle can't find worker specified as Gearman format Exception
*
* @author Marc Morera <marc@ulabox.com>
*/
class WorkerDoesNotExistException extends \Exception
{

/**
* Construct method for Exception
*
* @param string $worker Worker name to be shown in Exception
* @param integer $code Code of exception
* @param \Exception $previous Previos Exception
*/
public function __construct($worker, $code = 0, \Exception $previous = null)
{
$message = 'GearmanBundle can\'t find worker with name ' . $worker . PHP_EOL;
parent::__construct($message, $code, $previous);
}
}
53 changes: 48 additions & 5 deletions Module/WorkerClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Mmoreramerino\GearmanBundle\Driver\Gearman\Work;
use Mmoreramerino\GearmanBundle\Module\JobCollection;
use Mmoreramerino\GearmanBundle\Module\JobClass as Job;
use Mmoreramerino\GearmanBundle\Exceptions\SettingValueMissingException;
use Mmoreramerino\GearmanBundle\Exceptions\SettingValueBadFormatException;

/**
* Worker class
Expand All @@ -22,8 +24,8 @@ class WorkerClass
private $jobCollection;

/**
* Callable name for this job.
* If is setted on annotations, this value will be used.
* Callable name for this job.
* If is setted on annotations, this value will be used.
* Otherwise, natural method name will be used.
*
* @var string
Expand All @@ -49,9 +51,9 @@ public function __construct( Work $classAnnotation, \ReflectionClass $reflection
{
$this->namespace = $reflectionClass->getNamespaceName();

$this->callableName = (null !== $classAnnotation->name) ?
$classAnnotation->name :
$this->namespace;
$this->callableName = str_replace('\\', '', ((null !== $classAnnotation->name) ?
($this->namespace .'\\' .$classAnnotation->name) :
$reflectionClass->getName()));

$this->description = (null !== $classAnnotation->description) ?
$classAnnotation->description :
Expand All @@ -61,6 +63,45 @@ public function __construct( Work $classAnnotation, \ReflectionClass $reflection
$this->className = $reflectionClass->getName();
$this->service = $classAnnotation->service;

if (null !== $settings['defaults']['iterations']) {
$iter = (int) ($settings['defaults']['iterations']);

if (null !== $classAnnotation->iterations) {
$iter = (int) ($classAnnotation->iterations);
}
} else {
throw new SettingValueMissingException('defaults/iterations');
}
$this->iterations = $iter;

/**
* Servers definition for worker
*/
$servers = array();
if (null !== $settings['defaults']['servers']) {
if (is_array($settings['defaults']['servers'])) {

foreach ($settings['defaults']['servers'] as $name => $server) {
$servername = $server['hostname'].':'.(int) ($server['port']);
$servers[$name] = $servername;
}
} else {

throw new SettingValueBadFormatException('servers');
}

if (null !== $classAnnotation->servers) {
if (is_array($classAnnotation->servers)) {
$servers = $classAnnotation->servers;
} else {
$servers = array($classAnnotation->servers);
}
}
} else {
throw new SettingValueMissingException('defaults/servers');
}
$this->servers = $servers;

$this->jobCollection = new JobCollection;

foreach ($reflectionClass->getMethods() as $method) {
Expand Down Expand Up @@ -88,6 +129,8 @@ public function __toCache()
'callableName' => $this->callableName,
'description' => $this->description,
'service' => $this->service,
'servers' => $this->servers,
'iterations' => $this->iterations,
'jobs' => array(),
);

Expand Down
6 changes: 3 additions & 3 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ parameters:
gearman.client.class: Mmoreramerino\GearmanBundle\Service\GearmanClient
gearman.cache.class: Mmoreramerino\GearmanBundle\Service\GearmanCache
gearman.cache.loader.class: Mmoreramerino\GearmanBundle\Service\GearmanCacheLoader
gearman.execute.job.class: Mmoreramerino\GearmanBundle\Service\GearmanExecute
gearman.execute.class: Mmoreramerino\GearmanBundle\Service\GearmanExecute
gearman.settings.class: Mmoreramerino\GearmanBundle\Service\GearmanSettings
gearman.describer.class: Mmoreramerino\GearmanBundle\Service\GearmanDescriber
services:
Expand All @@ -26,8 +26,8 @@ services:
class: %gearman.cache.loader.class%
calls:
- [setContainer, [@service_container]]
gearman.execute.job:
class: %gearman.execute.job.class%
gearman.execute:
class: %gearman.execute.class%
calls:
- [setContainer, [@service_container]]
gearman.describer:
Expand Down
4 changes: 2 additions & 2 deletions Service/GearmanClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getWorkers()
*/
public function callJob($name, $params = array())
{
$worker = $this->getWorker($name);
$worker = $this->getJob($name);
$methodCallable = $worker['job']['defaultMethod'] . 'Job';

if (!method_exists($this, $methodCallable)) {
Expand Down Expand Up @@ -160,7 +160,7 @@ public function doLowBackgroundJob($name, $params = array())
*/
private function enqueue($jobName, $params, $method)
{
$worker = $this->getWorker($jobName);
$worker = $this->getJob($jobName);
if (false !== $worker) {
return $this->doEnqueue($worker, $params, $method);
}
Expand Down
Loading

0 comments on commit 5823dcd

Please sign in to comment.