Просмотр исходного кода

#solves 66. Added Exception for jobs with name longer than 114 chars

Marc 11 лет назад
Родитель
Сommit
d2ed483500

+ 34 - 0
Exceptions/WorkerNameTooLongException.php

@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * Gearman Bundle for Symfony2
+ *
+ * @author Marc Morera <yuhu@mmoreram.com>
+ * @since 2013
+ */
+
+namespace Mmoreram\GearmanBundle\Exceptions;
+
+use Mmoreram\GearmanBundle\Exceptions\Abstracts\AbstractGearmanException;
+use Exception;
+
+/**
+ * GearmanBundle can't find worker specified as Gearman format Exception
+ */
+class WorkerNameTooLongException extends AbstractGearmanException
+{
+
+    /**
+     * Construction method
+     * 
+     * @param string    $message  Message
+     * @param int       $code     Code
+     * @param Exception $previous Previous
+     */
+    public function __construct($message = null, $code = 0, Exception $previous = null)
+    {
+        $message = 'The function name + unique id cannot exceed 114 bytes. You can change workers name or set a shortly unique key';
+
+        parent::__construct($message, $code, $previous);
+    }
+}

+ 13 - 1
Generator/UniqueJobIdentifierGenerator.php

@@ -13,6 +13,7 @@ namespace Mmoreram\GearmanBundle\Generator;
  * Job Unique Key generator
  * Job Unique Key generator
  *
  *
  * @author Marc Morera <yuhu@mmoreram.com>
  * @author Marc Morera <yuhu@mmoreram.com>
+ * @see    https://github.com/mmoreram/GearmanBundle/issues/66
  */
  */
 class UniqueJobIdentifierGenerator
 class UniqueJobIdentifierGenerator
 {
 {
@@ -40,6 +41,8 @@ class UniqueJobIdentifierGenerator
      * Generate unique key if generateUniqueKey is enabled
      * Generate unique key if generateUniqueKey is enabled
      *
      *
      * Even some parameters are not used, are passed to allow user overwrite method
      * Even some parameters are not used, are passed to allow user overwrite method
+     * 
+     * Also, if name and unique value exceeds 114 bytes, an exception is thrown
      *
      *
      * @param string $name   A GermanBundle registered function to be executed
      * @param string $name   A GermanBundle registered function to be executed
      * @param string $params Parameters to send to task as string
      * @param string $params Parameters to send to task as string
@@ -47,11 +50,20 @@ class UniqueJobIdentifierGenerator
      * @param string $method Method to perform
      * @param string $method Method to perform
      *
      *
      * @return string Generated Unique Key
      * @return string Generated Unique Key
+     * 
+     * @throws WorkerNameTooLongException If name is too large
      */
      */
     public function generateUniqueKey($name, $params, $unique, $method)
     public function generateUniqueKey($name, $params, $unique, $method)
     {
     {
-        return  ( !$unique && $this->generateUniqueKey )
+        $unique = !$unique && $this->generateUniqueKey
                 ? md5($name . $params)
                 ? md5($name . $params)
                 : $unique;
                 : $unique;
+
+        if (strlen($name . $unique) > 114) {
+
+            throw new WorkerNameTooLongException;
+        }
+
+        return $unique;
     }
     }
 }
 }

+ 0 - 1
Service/GearmanClient.php

@@ -388,7 +388,6 @@ class GearmanClient extends AbstractGearmanService
      */
      */
     public function doLowJob($name, $params = '', $unique = null)
     public function doLowJob($name, $params = '', $unique = null)
     {
     {
-
         return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DOLOW, $unique);
         return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DOLOW, $unique);
     }
     }