dominikmank / gearman

a php gearman worker/dispatcher implementation
https://github.com/dominikmank/gearman
MIT License
4 stars 4 forks source link

fix high cpu usage when gearman queue no job #9

Closed chronus282 closed 6 years ago

chronus282 commented 6 years ago

hello :D

I found that it's consume many CPU resources when no job in gearman queue. It run so fast when no any task in queue, so i add the sleep between each run (default 0.05 second).

dominikmank commented 6 years ago

heya @chronus282 ,

thanks for contributing! The idea behind this was to control it by the events, in this case in the applications which i'm using it, i use a subscriber like that

<?php

namespace AppBundle\Subscriber\Gearman;

use dmank\gearman\event\WorkerEvent;
use dmank\gearman\event\WorkerExceptionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class DefaultBehaviour implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return array(
            WorkerEvent::EVENT_ON_WORK => array('onWork'),
            WorkerExceptionEvent::EVENT_ON_FAILURE => array('onFailure')
        );
    }

    /**
     * @param WorkerEvent $event
     */
    public function onWork(WorkerEvent $event)
    {
        sleep(1);
    }

    /**
     * @param WorkerExceptionEvent $event
     */
    public function onFailure(WorkerExceptionEvent $event)
    {
        $event->getWorker()->destroyWorker();
    }
}

so, just in case i would have the possibility to control it by each job, if i want to.

chronus282 commented 6 years ago

ok, thank you for help :D