mwillbanks / MwGearman

Gearman Module for ZF2
20 stars 7 forks source link

How to run it ? #6

Open seyfer opened 9 years ago

seyfer commented 9 years ago
 php public/index.php gearmantest --action=worker --execute=1
======================================================================
   The application has thrown an exception!
======================================================================
 mwGearman\Exception\InvalidArgumentException
 Function `%s` is not callable
----------------------------------------------------------------------
/var/www/host.com/vendor/mwillbanks/mw-gearman/src/mwGearman/Worker/Pecl.php:174
#0 /var/www/host.com/module/Learning/src/Learning/Controller/Console/GearmanController.php(68): mwGearman\Worker\Pecl->register('myJob', 'handleJob')
#1 /var/www/host.com/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): Learning\Controller\Console\GearmanController->workerAction()
#2 /var/www/host.com/module/Learning/src/Learning/Controller/Console/GearmanController.php(28): Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#3 [internal function]: Learning\Controller\Console\GearmanController->onDispatch(Object(Zend\Mvc\MvcEvent))
#4 /var/www/host.com/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#5 /var/www/host.com/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 /var/www/host.com/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(116): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 /var/www/host.com/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(113): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Console\Request), Object(Zend\Console\Response))
#8 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 /var/www/host.com/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 /var/www/host.com/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /var/www/host.com/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 /var/www/host.com/public/index.php(36): Zend\Mvc\Application->run()
#13 {main}
======================================================================
   Previous Exception(s):

There is my error. I'm trying run it from Console controller.

class GearmanController extends AbstractActionController
{

public function clientAction()
    {
        $serviceMananger = $this->getServiceLocator();
        $gearman         = $serviceMananger->get('mwGearman\Client\Pecl');
        $gearman->connect();

        $workload = 'some-string';

        $task = new \mwGearman\Task\Task();
        $task->setBackground(true)
             ->setFunction('myJob')
             ->setWorkload($workload)
             ->setUnique(crc32($workload));

        $handle = $gearman->doTask($task);
    }

    public function workerAction()
    {
        $serviceMananger = $this->getServiceLocator();
        $gearman         = $serviceMananger->get('mwGearman\Worker\Pecl');
        /* @var $gearman \mwGearman\Worker\Pecl */
        $gearman->register('myJob', 'handleJob');
        $gearman->connect();
        while ($gearman->work())
            ;
    }
}

Also I add function in this file (not in class)

function handleJob($job)
    {
        $workload = $job->workload();
        echo $workload;
    }

What I'm doing wrong? Can you please provide full example?

seyfer commented 9 years ago

If I should put worker just in .php file - how to load $serviceManager to this file?

seyfer commented 9 years ago

Now i'm add function just to public/index.php

and have this error

hp public/index.php gearmantest --action=worker --execute=1
PHP Warning:  GearmanWorker::register() expects parameter 1 to be string, array given in /var/www/host.com/vendor/mwillbanks/mw-gearman/src/mwGearman/Worker/Pecl.php on line 123

but i'm pass string as parameter, not array

seyfer commented 9 years ago

Solved only with this syntax

/* @var $gearman \mwGearman\Worker\Pecl */
        $gearman->getGearmanWorker()->addFunction('myJob', function ($job) {
            $workload = $job->workload();
            echo $workload . "\n";
        });

So feature to register job from class or Closure should be implemented in module.

jackdpeterson commented 9 years ago

I suppose I could say I'm in a similar boat here trying to get things working. That being said I think I understand why the author of this module wrote things this way -- the Gearman extension appears to take in a string as a globally accessible function. Given that it seems that passing in an anonymous function, though intuitive and certainly my preference as well, it doesn't seem like it would work out given the way that the Gearman extension is written.

--> From the php docs: public bool GearmanWorker::register ( string $function_name [, int $timeout ] )

Registers a function name with the job server with an optional timeout. The timeout specifies how many seconds the server will wait before marking a job as failed. If the timeout is set to zero, there is no timeout. <-- end of documentation quote.

Now, all that being said -- what is the suggested way to register the globally accessible functions without polluting the rest of the application's namespace?