Skip to content

Commit

Permalink
Merge pull request #29 from ostark/reserved
Browse files Browse the repository at this point in the history
Limit the number of concurrent processes by counting reserved jobs
  • Loading branch information
Oliver Stark authored Mar 26, 2020
2 parents ba6ef52 + 232056f commit f53f835
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 352 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [2.1.0] - 2020-03-26
### Changed
- Replaced `ProcessPool` with `Ratelimiter` to limit the number of concurrent queue runners
- Clean up: Removed unnecessary doc blocks in favour of type hints

## [2.0.0] - 2019-01-30
### Changed
- Decoupled `QueueCommand` form `QueueHandler`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ostark/craft-async-queue",
"description": "A queue handler that moves queue execution to a non-blocking background process",
"type": "craft-plugin",
"version": "2.0.0",
"version": "2.1.0",
"keywords": [
"craft",
"cms",
Expand Down
9 changes: 0 additions & 9 deletions src/BackgroundProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
use ostark\AsyncQueue\Exceptions\RuntimeException;
use Symfony\Component\Process\Process;


/**
* BackgroundProcess
*
* @author Oliver Stark
* @package AsyncQueue
* @since 2.0.0
*
*/
class BackgroundProcess
{

Expand Down
1 change: 0 additions & 1 deletion src/Events/QueueCommandEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php namespace ostark\AsyncQueue\Events;


use yii\base\Event;

class QueueCommandEvent extends Event
Expand Down
5 changes: 0 additions & 5 deletions src/Exceptions/LogicException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

use Symfony\Component\Process\Process;

/**
* Class LogicException
*
* @package ostark\AsyncQueue\Exceptions
*/
class LogicException extends \LogicException implements ProcessException
{
protected $process;
Expand Down
5 changes: 0 additions & 5 deletions src/Exceptions/PhpExecutableNotFound.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php namespace ostark\AsyncQueue\Exceptions;

/**
* Class PhpExecutableNotFound
*
* @package ostark\AsyncQueue\Exceptions
*/
class PhpExecutableNotFound extends \Exception
{

Expand Down
5 changes: 0 additions & 5 deletions src/Exceptions/ProcessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

use Symfony\Component\Process\Process;

/**
* Interface ProcessException
*
* @package ostark\AsyncQueue\Exceptions
*/
interface ProcessException
{
public function setProcess(Process $process);
Expand Down
5 changes: 0 additions & 5 deletions src/Exceptions/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

use Symfony\Component\Process\Process;

/**
* Class RuntimeException
*
* @package ostark\AsyncQueue\Exceptions
*/
class RuntimeException extends \RuntimeException implements ProcessException
{
protected $process;
Expand Down
11 changes: 4 additions & 7 deletions src/Handlers/BackgroundQueueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
use ostark\AsyncQueue\Plugin;
use yii\queue\PushEvent;

/**
* Class BackgroundQueueHandler
*
* @package ostark\AsyncQueue\Handlers
*/
class BackgroundQueueHandler
{
/**
Expand All @@ -38,11 +33,12 @@ public function __invoke(PushEvent $event)
: 'Not instanceof craft\queue\JobInterface';

// Run queue in the background
if ($this->plugin->getPool()->canIUse($context)) {
if ($this->plugin->getRateLimiter()->canIUse($context)) {
try {
$this->plugin->getProcess()->start();
$this->plugin->getPool()->increment($context);
$this->plugin->getRateLimiter()->increment();
$handled = true;

} catch (PhpExecutableNotFound $e) {
Craft::debug(
'QueueHandler::startBackgroundProcess() (PhpExecutableNotFound)',
Expand All @@ -60,6 +56,7 @@ public function __invoke(PushEvent $event)
'async-queue'
);
}

}

// Log what's going on
Expand Down
38 changes: 0 additions & 38 deletions src/Handlers/ProcessPoolCleanupHandler.php

This file was deleted.

42 changes: 8 additions & 34 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,18 @@

use Craft;
use craft\base\Plugin as BasePlugin;
use craft\events\RegisterTemplateRootsEvent;
use craft\queue\BaseJob;
use craft\queue\Command;
use craft\queue\JobInterface;
use craft\queue\Queue;
use ostark\AsyncQueue\Exceptions\LogicException;
use ostark\AsyncQueue\Exceptions\PhpExecutableNotFound;
use ostark\AsyncQueue\Exceptions\RuntimeException;
use ostark\AsyncQueue\Handlers\BackgroundQueueHandler;
use ostark\AsyncQueue\Handlers\ProcessPoolCleanupHandler;
use ostark\AsyncQueue\TestUtility\Utility;
use yii\base\ActionEvent;
use yii\base\Event;
use yii\caching\CacheInterface;
use yii\queue\PushEvent;


/**
* AsyncQueue
*
* @author Oliver Stark
* @package AsyncQueue
* @since 1.0.0
* Main plugin class
*
* @property \ostark\AsyncQueue\BackgroundProcess $process
* @property \ostark\AsyncQueue\RateLimiter $rateLimiter
* @method \ostark\AsyncQueue\Settings getSettings()
*
*/
class Plugin extends BasePlugin
{
Expand All @@ -58,16 +44,12 @@ public function init()

// Register plugin components
$this->setComponents([
'async_process' => BackgroundProcess::class,
'async_pool' => ProcessPool::class,
'async_process' => BackgroundProcess::class,
'async_rate_limiter' => RateLimiter::class
]);

// Tell yii about the concrete implementation of CacheInterface
Craft::$container->set(CacheInterface::class, Craft::$app->getCache());

// Register event handlers
PushEvent::on(Queue::class, Queue::EVENT_AFTER_PUSH, new BackgroundQueueHandler($this));
Event::on(Command::class, Command::EVENT_AFTER_ACTION, new ProcessPoolCleanupHandler($this));

// Register CP Utility
Utility::setup($this);
Expand All @@ -78,29 +60,21 @@ public function init()
// ServiceLocators
// =========================================================================

/**
* @return \ostark\AsyncQueue\BackgroundProcess
*/
public function getProcess(): BackgroundProcess
{
return $this->get('async_process');
}

/**
* @return \ostark\AsyncQueue\ProcessPool
*/
public function getPool(): ProcessPool
public function getRateLimiter(): RateLimiter
{
return $this->get('async_pool');
return $this->get('async_rate_limiter');
}


/**
* Creates and returns the model used to store the plugin’s settings.
*
* @return \craft\base\Model|null
*/
protected function createSettingsModel()
protected function createSettingsModel() : Settings
{
return new Settings();
}
Expand Down
115 changes: 0 additions & 115 deletions src/ProcessPool.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/QueueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
use Symfony\Component\Process\PhpExecutableFinder;
use yii\base\Component;

/**
* Class QueueCommand
*
* @author Oliver Stark
* @package AsyncQueue
* @since 2.0.0
*/
class QueueCommand extends Component
{
const DEFAULT_SCRIPT = "craft";
Expand Down
Loading

0 comments on commit f53f835

Please sign in to comment.