瀏覽代碼

Some config editions

Marc 11 年之前
父節點
當前提交
c36f0a2367
共有 3 個文件被更改,包括 113 次插入23 次删除
  1. 26 22
      DependencyInjection/Configuration.php
  2. 1 1
      DependencyInjection/GearmanExtension.php
  3. 86 0
      Service/GearmanService.php

+ 26 - 22
DependencyInjection/Configuration.php

@@ -26,35 +26,39 @@ class Configuration implements ConfigurationInterface
             ->children()
                 ->arrayNode('bundles')
                     ->prototype('array')
-                    ->children()
-                        ->scalarNode('namespace')
-                            ->isRequired()
-                            ->cannotBeEmpty()
-                        ->end()
-                        ->scalarNode('active')
-                            ->defaultFalse()
-                        ->end()
-                        ->arrayNode('include')
-                            ->prototype('scalar')->end()
-                        ->end()
-                        ->arrayNode('exclude')
-                            ->prototype('scalar')->end()
+                        ->children()
+                            ->scalarNode('namespace')
+                                ->isRequired()
+                                ->cannotBeEmpty()
+                            ->end()
+                            ->scalarNode('active')
+                                ->defaultFalse()
+                            ->end()
+                            ->arrayNode('include')
+                                ->prototype('scalar')->end()
+                            ->end()
+                            ->arrayNode('exclude')
+                                ->prototype('scalar')->end()
+                            ->end()
                         ->end()
                     ->end()
                 ->end()
                 ->arrayNode('servers')
                     ->defaultValue(array(
-                        'hostname'  =>  '127.0.0.1',
-                        'port'      =>  4730,
+                        'localhost' =>  array(
+                            'hostname'  =>  '127.0.0.1',
+                            'port'      =>  4730,
+                        )
                     ))
                     ->prototype('array')
-                    ->children()
-                        ->scalarNode('hostname')
-                            ->isRequired()
-                            ->cannotBeEmpty()
-                        ->end()
-                        ->scalarNode('port')
-                            ->defaultValue(4730)
+                        ->children()
+                            ->scalarNode('hostname')
+                                ->isRequired()
+                                ->cannotBeEmpty()
+                            ->end()
+                            ->scalarNode('port')
+                                ->defaultValue(4730)
+                            ->end()
                         ->end()
                     ->end()
                 ->end()

+ 1 - 1
DependencyInjection/GearmanExtension.php

@@ -43,7 +43,7 @@ class GearmanExtension extends Extension
             'gearman.params',
             $config['defaults']
         );
-
+        
         $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
         $loader->load('services.yml');
     }

+ 86 - 0
Service/GearmanService.php

@@ -0,0 +1,86 @@
+<?php
+
+namespace Mmoreram\GearmanBundle\Service;
+
+use Mmoreram\GearmanBundle\Service\GearmanCache as Cache;
+use Mmoreram\GearmanBundle\Exceptions\JobDoesNotExistException;
+use Mmoreram\GearmanBundle\Exceptions\WorkerDoesNotExistException;
+
+/**
+ * Gearman execute methods. All Worker methods
+ *
+ * @author Marc Morera <yuhu@mmoreram.com>
+ */
+abstract class GearmanService
+{
+
+    /**
+     * All workers
+     *
+     * @var type
+     */
+    protected $workers;
+
+
+    /**
+     * Construct method
+     *
+     * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper
+     */
+    public function __construct(GearmanCacheWrapper $gearmanCacheWrapper)
+    {
+        $this->workers = $gearmanCacheWrapper->getWorkerCollection();
+    }
+
+
+    /**
+     * Return worker containing a job with $jobName as name
+     * If is not found, throws JobDoesNotExistException Exception
+     *
+     * @param string $jobName Name of job
+     *
+     * @return Array
+     */
+    public function getJob($jobName)
+    {
+        foreach ($this->workers as $worker) {
+
+            if (is_array($worker['jobs'])) {
+
+                foreach ($worker['jobs'] as $job) {
+
+                    if ($jobName === $job['realCallableName']) {
+
+                        $worker['job'] = $job;
+
+                        return $worker;
+                    }
+                }
+            }
+        }
+
+        throw new JobDoesNotExistException($jobName);
+    }
+
+
+    /**
+     * Return worker with $workerName as name and all its jobs
+     * If is not found, throws WorkerDoesNotExistException Exception
+     *
+     * @param string $workerName Name of worker
+     *
+     * @return Array
+     */
+    public function getWorker($workerName)
+    {
+        foreach ($this->workers as $worker) {
+
+            if ($workerName === $worker['callableName']) {
+
+                return $worker;
+            }
+        }
+
+        throw new WorkerDoesNotExistException($workerName);
+    }
+}