Skip to content

Commit

Permalink
Added nap strategy otherweise the CPU is at 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kleff committed Jul 16, 2015
1 parent 781f460 commit f93f74d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
7 changes: 2 additions & 5 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
*/
'worker_strategies' => array(
'default' => array(
// 'SlmQueueDoctrine\Strategy\IdleNapStrategy' => array('nap_duration' => 1),
// 'SlmQueueDoctrine\Strategy\ClearObjectManagerStrategy'
'SlmQueueQueue\Strategy\IdleNapStrategy' => array('nap_duration' => 1),
),
'queues' => array(
),
Expand All @@ -57,9 +56,7 @@
*/
'strategy_manager' => array(
'invokables' => array(
// 'SlmQueueDoctrine\Strategy\IdleNapStrategy' => 'SlmQueueDoctrine\Strategy\IdleNapStrategy',
// 'SlmQueueDoctrine\Strategy\ClearObjectManagerStrategy'
// => 'SlmQueueDoctrine\Strategy\ClearObjectManagerStrategy'
'SlmQueueRedis\Strategy\IdleNapStrategy' => 'SlmQueueRedis\Strategy\IdleNapStrategy',
)
),
)
Expand Down
3 changes: 2 additions & 1 deletion src/SlmQueueRedis/Paginator/Adapter/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public function getItems($offset, $itemCountPerPage)
$ids = $this->queue->getAdapter()->slice(
$this->queue->getName(),
$offset,
$itemCountPerPage
$offset + $itemCountPerPage
);


$jobs = array();
foreach($ids as $id) {
$jobs[$id] = $this->queue->peek($id);
Expand Down
57 changes: 56 additions & 1 deletion src/SlmQueueRedis/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function getAdapter() {
*/
public function push(JobInterface $job, array $options = array())
{

$id = $this->adapter->push(
$this->getName(),
$this->serializeJob($job)
Expand Down Expand Up @@ -141,4 +140,60 @@ public function peek($id) {
public function purge() {
$this->getAdapter()->purge($this->getName());
}


// /**
// * Create a job instance based on serialized input
// *
// * Instantiate a job based on a serialized data string. The string
// * is a JSON string containing job name, content and metadata. Use
// * the decoded JSON value to create a job instance, configure it
// * and return it.
// *
// * @param string $string
// * @param array $metadata
// * @return \SlmQueue\Job\JobInterface
// */
// public function unserializeJob($string, array $metadata = array())
// {
// $data = $string; //json_decode($string, true);
// $name = $data['metadata']['__name__'];
// $metadata += $data['metadata'];
// $content = $data['content']; //unserialize($data['content']);
//
// /** @var $job \SlmQueue\Job\JobInterface */
// $job = $this->getJobPluginManager()->get($name);
//
// $job->setContent($content);
// $job->setMetadata($metadata);
//
// if ($job instanceof QueueAwareInterface) {
// $job->setQueue($this);
// }
//
// return $job;
// }
//
// /**
// * Serialize job to allow persistence
// *
// * The serialization format is a JSON object with keys "content",
// * "metadata" and "__name__". When a job is fetched from the SL, a job name
// * will be set and be available as metadata. An invokable job has no service
// * name and therefore the FQCN will be used.
// *
// * @param JobInterface $job The job to persist
// * @return string
// */
// public function serializeJob(JobInterface $job)
// {
// $job->setMetadata('__name__', $job->getMetadata('__name__', get_class($job)));
//
// $data = array(
// 'content' => $job->getContent(),
// 'metadata' => $job->getMetadata()
// );
//
// return $data;
// }
}
58 changes: 58 additions & 0 deletions src/SlmQueueRedis/Strategy/IdleNapStrategy.php
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);
}
}
}

0 comments on commit f93f74d

Please sign in to comment.