Skip to content

Commit

Permalink
Updated the bundle to support prefixes for jobs/tasks. This adds supp…
Browse files Browse the repository at this point in the history
…ort for using a single gearmand server but have different environments for dev and prod.

Now you can optionally specify a job_prefix under the default settings. By default this will be null and have no affect on anything.
If you specify one such as 'dev' it will prepend all tasks and job.  This will require zero updates between live and dev environments of your code.
Also updated the describer and list commands to show the prefix is there is one in use.
  • Loading branch information
daum committed Nov 5, 2013
1 parent 5467020 commit 5c7b117
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 16 deletions.
6 changes: 5 additions & 1 deletion Command/GearmanWorkerListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($worker['jobs'] as $job) {
$output->writeln('<comment> - #'.$it++.'</comment>');
$output->writeln('<comment> name: '.$job['methodName'].'</comment>');
$output->writeln('<comment> callablename:</comment><info> '.$job['realCallableName'].'</info>');
$output->writeln('<comment> callablename:</comment><info> '.$job['realCallableNameNoPrefix'].'</info>');

if(false === is_null($job['jobPrefix'])){
$output->writeln('<comment> job prefix:</comment><info> '.$job['jobPrefix'].'</info>');
}
}
$output->writeln('');
}
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public function getConfigTreeBuilder()
->scalarNode('callbacks')
->defaultValue(true)
->end()
->scalarNode('job_prefix')
->defaultValue(null)
->end()
->end()
->end()
->end();
Expand Down
38 changes: 28 additions & 10 deletions Module/JobClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class JobClass extends ContainerAware
*/
private $methodName;

/**
* @var string
*
* RealCallable name for this job without the job prefix
*
*/
private $realCallableNameNoPrefix;


/**
* @var string
Expand Down Expand Up @@ -87,6 +95,13 @@ class JobClass extends ContainerAware
*/
private $servers;

/**
* @var string
*
* The prefix to be prepended to all job callable names.
*/
private $jobPrefix;


/**
* Construct method
Expand All @@ -99,13 +114,15 @@ class JobClass extends ContainerAware
*/
public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $reflectionMethod, $callableNameClass, array $servers, array $defaultSettings)
{

$this->callableName = is_null($jobAnnotation->name)
? $reflectionMethod->getName()
: $jobAnnotation->name;

$this->methodName = $reflectionMethod->getName();

$this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
$this->realCallableNameNoPrefix = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
$this->jobPrefix = $defaultSettings['jobPrefix'];
$this->realCallableName = $this->jobPrefix . $this->realCallableNameNoPrefix;
$this->description = is_null($jobAnnotation->description)
? self::DEFAULT_DESCRIPTION
: $jobAnnotation->description;
Expand Down Expand Up @@ -193,14 +210,15 @@ public function toArray()
{
return array(

'callableName' => $this->callableName,
'methodName' => $this->methodName,
'realCallableName' => $this->realCallableName,
'description' => $this->description,

'iterations' => $this->iterations,
'servers' => $this->servers,
'defaultMethod' => $this->defaultMethod,
'callableName' => $this->callableName,
'methodName' => $this->methodName,
'realCallableName' => $this->realCallableName,
'jobPrefix' => $this->jobPrefix,
'realCallableNameNoPrefix' => $this->realCallableNameNoPrefix,
'description' => $this->description,
'iterations' => $this->iterations,
'servers' => $this->servers,
'defaultMethod' => $this->defaultMethod,
);
}
}
18 changes: 15 additions & 3 deletions Module/WorkerClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ class WorkerClass
private $jobCollection;


/**
* The prefix for all job names
*
* @var string $jobPrefix
*/
private $jobPrefix = null;


/**
* Retrieves all jobs available from worker
*
Expand Down Expand Up @@ -148,10 +156,15 @@ public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $ref
$this->className = $reflectionClass->getName();
$this->service = $workAnnotation->service;

if(isset($defaultSettings['job_prefix']))
$this->jobPrefix = $defaultSettings['job_prefix'];

$this->servers = $this->loadServers($workAnnotation, $servers);
$this->iterations = $this->loadIterations($workAnnotation, $defaultSettings);
$this->defaultMethod = $this->loadDefaultMethod($workAnnotation, $defaultSettings);
$this->jobCollection = $this->createJobCollection($reflectionClass, $reader);


}


Expand Down Expand Up @@ -249,15 +262,14 @@ private function createJobCollection(ReflectionClass $reflectionClass, SimpleAnn
foreach ($methodAnnotations as $methodAnnotation) {

/**
* Annotation is only laoded if is typeof JobAnnotation
* Annotation is only loaded if is typeof JobAnnotation
*/
if ($methodAnnotation instanceof JobAnnotation) {

/**
* Creates new Job
*/
$job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, array(

'jobPrefix' => $this->jobPrefix,
'iterations' => $this->iterations,
'method' => $this->defaultMethod,
));
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ services:
abstract: true
arguments:
gearman.cache.wrapper: @gearman.cache.wrapper
default_settings: %gearman.default.settings%

gearman.execute:
class: %gearman.execute.class%
Expand Down
17 changes: 16 additions & 1 deletion Service/Abstracts/AbstractGearmanService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,27 @@ abstract class AbstractGearmanService
protected $workers;


/**
* The prefix for all job names
*
* @var string $jobPrefix
*/
protected $jobPrefix = null;


/**
* Construct method
*
* @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper
* @param array $defaultSettings The default settings for the bundle.
*
*/
public function __construct(GearmanCacheWrapper $gearmanCacheWrapper)
public function __construct(GearmanCacheWrapper $gearmanCacheWrapper,array $defaultSettings)
{
$this->workers = $gearmanCacheWrapper->getWorkers();

if(isset($defaultSettings['job_prefix']))
$this->jobPrefix = $defaultSettings['job_prefix'];
}


Expand All @@ -50,6 +63,8 @@ public function __construct(GearmanCacheWrapper $gearmanCacheWrapper)
*/
public function getJob($jobName)
{
$jobName = $this->jobPrefix . $jobName;

foreach ($this->workers as $worker) {

if (is_array($worker['jobs'])) {
Expand Down
9 changes: 8 additions & 1 deletion Service/GearmanDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ public function describeWorker(OutputInterface $output, array $worker, $tinyJobD
$output->writeln('<info> @Worker\jobs</info>');
$output->writeln('');
foreach ($worker['jobs'] as $job) {
$output->writeln('<comment> # ' . $job['realCallableName'] . '</comment>');

if(false === is_null($job['jobPrefix'])){
$output->writeln('<comment> # ' . $job['realCallableNameNoPrefix'] . ' with prefix: '.$job['jobPrefix'].'</comment>');
}
else{
$output->writeln('<comment> # ' . $job['realCallableNameNoPrefix'] . ' </comment>');
}

}
}

Expand Down

0 comments on commit 5c7b117

Please sign in to comment.