forked from Webador/SlmQueueRedis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added nap strategy otherweise the CPU is at 100%
- Loading branch information
Stefan Kleff
committed
Jul 16, 2015
1 parent
781f460
commit f93f74d
Showing
4 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace SlmQueueRedisStrategy; | ||
|
||
use SlmQueue\Strategy\AbstractStrategy; | ||
use SlmQueue\Worker\WorkerEvent; | ||
use SlmQueueDoctrine\Queue\DoctrineQueueInterface; | ||
use Zend\EventManager\EventManagerInterface; | ||
|
||
class IdleNapStrategy extends AbstractStrategy | ||
{ | ||
/** | ||
* How long should we sleep when the worker is idle before trying again | ||
* | ||
* @var int | ||
*/ | ||
protected $napDuration = 1; | ||
|
||
/** | ||
* @param int $napDuration | ||
*/ | ||
public function setNapDuration($napDuration) | ||
{ | ||
$this->napDuration = (int) $napDuration; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getNapDuration() | ||
{ | ||
return $this->napDuration; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function attach(EventManagerInterface $events) | ||
{ | ||
$this->listeners[] = $events->attach( | ||
WorkerEvent::EVENT_PROCESS_IDLE, | ||
array($this, 'onIdle'), | ||
1 | ||
); | ||
} | ||
|
||
/** | ||
* @param WorkerEvent $event | ||
*/ | ||
public function onIdle(WorkerEvent $event) | ||
{ | ||
$queue = $event->getQueue(); | ||
|
||
if ($queue instanceof DoctrineQueueInterface) { | ||
sleep($this->napDuration); | ||
} | ||
} | ||
} |