From f729a4ac6f32e10920855ca5da8b1b8b3a5abeb3 Mon Sep 17 00:00:00 2001 From: Roman Zhuravlev Date: Sat, 29 Jul 2017 19:16:29 +0300 Subject: [PATCH 1/9] #87: Stoppable behavior --- src/behaviors/StopBehavior.php | 97 ++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/behaviors/StopBehavior.php diff --git a/src/behaviors/StopBehavior.php b/src/behaviors/StopBehavior.php new file mode 100644 index 0000000000..1cba34f162 --- /dev/null +++ b/src/behaviors/StopBehavior.php @@ -0,0 +1,97 @@ + + */ +class StopBehavior extends Behavior +{ + /** + * @var Cache|array|string + */ + public $cache = 'cache'; + /** + * @var bool + */ + public $checkWaiting = true; + /** + * @var Queue + */ + public $owner; + + /** + * @inheritdoc + */ + public function init() + { + parent::init(); + $this->cache = Instance::ensure($this->cache, Cache::class); + } + + /** + * @inheritdoc + */ + public function events() + { + return [ + Queue::EVENT_BEFORE_EXEC => function (ExecEvent $event) { + $event->handled = $this->isStopped($event->id); + }, + ]; + } + + /** + * Sets stop flag for a job which + * + * @param string $id of a job + * @return bool + */ + public function stop($id) + { + if (!$this->checkWaiting || $this->owner->isWaiting($id)) { + $this->setStopping($id); + return true; + } else { + return false; + } + } + + /** + * @param string $id of a job + * @return bool + */ + protected function setStopping($id) + { + $this->cache->set(__CLASS__ . $id, true); + } + + /** + * @param string $id of a job + * @return bool + */ + protected function isStopped($id) + { + if ($this->cache->exists(__CLASS__ . $id)) { + $this->cache->delete(__CLASS__ . $id); + return true; + } else { + return false; + } + } +} \ No newline at end of file From 008a36bb72000f303d7cebf6d2926775ab5cadb6 Mon Sep 17 00:00:00 2001 From: Roman Zhuravlev Date: Sun, 30 Jul 2017 00:08:20 +0300 Subject: [PATCH 2/9] Fixed typo --- src/behaviors/StopBehavior.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/behaviors/StopBehavior.php b/src/behaviors/StopBehavior.php index 1cba34f162..a2c93c878f 100644 --- a/src/behaviors/StopBehavior.php +++ b/src/behaviors/StopBehavior.php @@ -16,8 +16,6 @@ /** * Stoppable behavior * - * - * * @author Roman Zhuravlev */ class StopBehavior extends Behavior @@ -57,7 +55,7 @@ public function events() } /** - * Sets stop flag for a job which + * Sets stop flag. * * @param string $id of a job * @return bool From 62a7cf4f8a485210e8bf4e282a47282c7328a7eb Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 31 Jul 2017 01:03:43 +0200 Subject: [PATCH 3/9] Update StopBehavior.php added documentation --- src/behaviors/StopBehavior.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/behaviors/StopBehavior.php b/src/behaviors/StopBehavior.php index a2c93c878f..cfed41eff2 100644 --- a/src/behaviors/StopBehavior.php +++ b/src/behaviors/StopBehavior.php @@ -14,14 +14,20 @@ use yii\queue\Queue; /** - * Stoppable behavior + * Stoppable behavior allow stopping scheduled jobs in a queue. + * + * This behavior provides a [[stop()]] method, which allows to mark scheduled jobs as "stopped", which + * will prevent their execution. + * + * This behavior should be attached to the [[Queue]] component. * * @author Roman Zhuravlev + * @since 2.0.1 */ class StopBehavior extends Behavior { /** - * @var Cache|array|string + * @var Cache|array|string the cache instance used to store stopped status. */ public $cache = 'cache'; /** @@ -30,6 +36,7 @@ class StopBehavior extends Behavior public $checkWaiting = true; /** * @var Queue + * @inheritdoc */ public $owner; @@ -92,4 +99,4 @@ protected function isStopped($id) return false; } } -} \ No newline at end of file +} From 53b1565bf86c14b81e0ad3c3ba12b5dbda2ca4ea Mon Sep 17 00:00:00 2001 From: Roman Zhuravlev Date: Mon, 31 Jul 2017 20:48:30 +0300 Subject: [PATCH 4/9] Fixed naming --- src/behaviors/StopBehavior.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/behaviors/StopBehavior.php b/src/behaviors/StopBehavior.php index cfed41eff2..a1ca70f663 100644 --- a/src/behaviors/StopBehavior.php +++ b/src/behaviors/StopBehavior.php @@ -55,12 +55,18 @@ public function init() public function events() { return [ - Queue::EVENT_BEFORE_EXEC => function (ExecEvent $event) { - $event->handled = $this->isStopped($event->id); - }, + Queue::EVENT_BEFORE_EXEC => 'beforeExec', ]; } + /** + * @param ExecEvent $event + */ + public function beforeExec(ExecEvent $event) + { + $event->handled = $this->isStopped($event->id); + } + /** * Sets stop flag. * @@ -70,7 +76,7 @@ public function events() public function stop($id) { if (!$this->checkWaiting || $this->owner->isWaiting($id)) { - $this->setStopping($id); + $this->markAsStopped($id); return true; } else { return false; @@ -81,7 +87,7 @@ public function stop($id) * @param string $id of a job * @return bool */ - protected function setStopping($id) + protected function markAsStopped($id) { $this->cache->set(__CLASS__ . $id, true); } From ffb0db34a5799c81d00611835483fdc418f09517 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Mon, 14 Aug 2017 12:27:12 +0300 Subject: [PATCH 5/9] Adjusted wording [skip ci] --- src/behaviors/StopBehavior.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/behaviors/StopBehavior.php b/src/behaviors/StopBehavior.php index a1ca70f663..9059a036a0 100644 --- a/src/behaviors/StopBehavior.php +++ b/src/behaviors/StopBehavior.php @@ -14,9 +14,9 @@ use yii\queue\Queue; /** - * Stoppable behavior allow stopping scheduled jobs in a queue. + * StopBehavior allows stopping scheduled jobs in a queue. * - * This behavior provides a [[stop()]] method, which allows to mark scheduled jobs as "stopped", which + * It provides a [[stop()]] method to mark scheduled jobs as "stopped", that * will prevent their execution. * * This behavior should be attached to the [[Queue]] component. From 8cf8c690a73cd62d89e0c79e6afbaa08bd03798c Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Mon, 14 Aug 2017 12:29:05 +0300 Subject: [PATCH 6/9] Adjusted code style [skip ci] --- src/behaviors/StopBehavior.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/behaviors/StopBehavior.php b/src/behaviors/StopBehavior.php index 9059a036a0..5624505829 100644 --- a/src/behaviors/StopBehavior.php +++ b/src/behaviors/StopBehavior.php @@ -78,9 +78,9 @@ public function stop($id) if (!$this->checkWaiting || $this->owner->isWaiting($id)) { $this->markAsStopped($id); return true; - } else { - return false; } + + return false; } /** @@ -101,8 +101,8 @@ protected function isStopped($id) if ($this->cache->exists(__CLASS__ . $id)) { $this->cache->delete(__CLASS__ . $id); return true; - } else { - return false; } + + return false; } } From 80ac6b3b910155767d0e7e8364e9bd23210f9ad5 Mon Sep 17 00:00:00 2001 From: Roman Zhuravlev Date: Sun, 27 Aug 2017 12:35:41 +0300 Subject: [PATCH 7/9] Description of checkWaiting option --- src/behaviors/StopBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/behaviors/StopBehavior.php b/src/behaviors/StopBehavior.php index 5624505829..a082360bbc 100644 --- a/src/behaviors/StopBehavior.php +++ b/src/behaviors/StopBehavior.php @@ -31,7 +31,7 @@ class StopBehavior extends Behavior */ public $cache = 'cache'; /** - * @var bool + * @var bool option allows to turn status checking off in case a driver does not support it. */ public $checkWaiting = true; /** From 34f07255f5521fb5ad2a940ad70cd4b9b6dad3ef Mon Sep 17 00:00:00 2001 From: Roman Zhuravlev Date: Sun, 27 Aug 2017 15:33:33 +0300 Subject: [PATCH 8/9] Tests --- CHANGELOG.md | 1 + .../Behavior.php} | 7 +- tests/stoppable/StoppableTest.php | 70 +++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) rename src/{behaviors/StopBehavior.php => stoppable/Behavior.php} (93%) create mode 100644 tests/stoppable/StoppableTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 475acebde7..9a34d8f19f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Yii2 Queue Extension Change Log ## 2.0.1 under development +- Enh #102: Stoppable behavior (zhuravljov) - Bug #118: Synchronized moving of delayed and reserved jobs to waiting list (zhuravljov) - Bug #112: Queue command inside module (tsingsun) - Enh #116: Add Chinese Guide (kids-return) diff --git a/src/behaviors/StopBehavior.php b/src/stoppable/Behavior.php similarity index 93% rename from src/behaviors/StopBehavior.php rename to src/stoppable/Behavior.php index a082360bbc..ff5679fe71 100644 --- a/src/behaviors/StopBehavior.php +++ b/src/stoppable/Behavior.php @@ -5,16 +5,15 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\queue\behaviors; +namespace yii\queue\stoppable; -use yii\base\Behavior; use yii\caching\Cache; use yii\di\Instance; use yii\queue\ExecEvent; use yii\queue\Queue; /** - * StopBehavior allows stopping scheduled jobs in a queue. + * Stoppable Behavior allows stopping scheduled jobs in a queue. * * It provides a [[stop()]] method to mark scheduled jobs as "stopped", that * will prevent their execution. @@ -24,7 +23,7 @@ * @author Roman Zhuravlev * @since 2.0.1 */ -class StopBehavior extends Behavior +class Behavior extends \yii\base\Behavior { /** * @var Cache|array|string the cache instance used to store stopped status. diff --git a/tests/stoppable/StoppableTest.php b/tests/stoppable/StoppableTest.php new file mode 100644 index 0000000000..f9841424ac --- /dev/null +++ b/tests/stoppable/StoppableTest.php @@ -0,0 +1,70 @@ + + */ +class StoppableTest extends TestCase +{ + public function testStop() + { + $job = new SimpleJob(['uid' => uniqid()]); + $id = $this->getQueue()->push($job); + $this->getQueue()->stop($id); + $this->getQueue()->run(); + $this->assertFileNotExists($job->getFileName()); + } + + public function testNotStop() + { + $job = new SimpleJob(['uid' => uniqid()]); + $this->getQueue()->push($job); + $this->getQueue()->run(); + $this->assertFileExists($job->getFileName()); + } + + /** + * @return SyncQueue|Stoppable + */ + protected function getQueue() + { + if (!$this->_queue) { + $this->_queue = new SyncQueue([ + 'handle' => false, + 'as stoppable' => [ + 'class' => Stoppable::class, + 'cache' => ArrayCache::class, + ], + ]); + } + return $this->_queue; + } + + private $_queue; + + /** + * @inheritdoc + */ + protected function tearDown() + { + foreach (glob(Yii::getAlias("@runtime/job-*.lock")) as $fileName) { + unlink($fileName); + } + parent::tearDown(); + } +} \ No newline at end of file From c710f309ad6fd9b8a0ef5b7dd30728cecf41008f Mon Sep 17 00:00:00 2001 From: Roman Zhuravlev Date: Sun, 15 Oct 2017 18:23:38 +0300 Subject: [PATCH 9/9] Base layer of stoppable behavior --- src/stoppable/BaseBehavior.php | 81 ++++++++++++++++++++++++++++++++++ src/stoppable/Behavior.php | 55 +---------------------- 2 files changed, 83 insertions(+), 53 deletions(-) create mode 100644 src/stoppable/BaseBehavior.php diff --git a/src/stoppable/BaseBehavior.php b/src/stoppable/BaseBehavior.php new file mode 100644 index 0000000000..e0f10da87a --- /dev/null +++ b/src/stoppable/BaseBehavior.php @@ -0,0 +1,81 @@ + + * @since 2.0.1 + */ +abstract class BaseBehavior extends \yii\base\Behavior +{ + /** + * @var bool option allows to turn status checking off in case a driver does not support it. + */ + public $checkWaiting = true; + /** + * @var Queue + * @inheritdoc + */ + public $owner; + + /** + * @inheritdoc + */ + public function events() + { + return [ + Queue::EVENT_BEFORE_EXEC => 'beforeExec', + ]; + } + + /** + * @param ExecEvent $event + */ + public function beforeExec(ExecEvent $event) + { + $event->handled = $this->isStopped($event->id); + } + + /** + * Sets stop flag. + * + * @param string $id of a job + * @return bool + */ + public function stop($id) + { + if (!$this->checkWaiting || $this->owner->isWaiting($id)) { + $this->markAsStopped($id); + return true; + } + + return false; + } + + /** + * @param string $id of a job + * @return bool + */ + abstract protected function markAsStopped($id); + + /** + * @param string $id of a job + * @return bool + */ + abstract protected function isStopped($id); +} diff --git a/src/stoppable/Behavior.php b/src/stoppable/Behavior.php index ff5679fe71..049001a010 100644 --- a/src/stoppable/Behavior.php +++ b/src/stoppable/Behavior.php @@ -9,35 +9,19 @@ use yii\caching\Cache; use yii\di\Instance; -use yii\queue\ExecEvent; -use yii\queue\Queue; /** - * Stoppable Behavior allows stopping scheduled jobs in a queue. - * - * It provides a [[stop()]] method to mark scheduled jobs as "stopped", that - * will prevent their execution. - * - * This behavior should be attached to the [[Queue]] component. + * Stoppable Behavior * * @author Roman Zhuravlev * @since 2.0.1 */ -class Behavior extends \yii\base\Behavior +class Behavior extends BaseBehavior { /** * @var Cache|array|string the cache instance used to store stopped status. */ public $cache = 'cache'; - /** - * @var bool option allows to turn status checking off in case a driver does not support it. - */ - public $checkWaiting = true; - /** - * @var Queue - * @inheritdoc - */ - public $owner; /** * @inheritdoc @@ -47,41 +31,6 @@ public function init() parent::init(); $this->cache = Instance::ensure($this->cache, Cache::class); } - - /** - * @inheritdoc - */ - public function events() - { - return [ - Queue::EVENT_BEFORE_EXEC => 'beforeExec', - ]; - } - - /** - * @param ExecEvent $event - */ - public function beforeExec(ExecEvent $event) - { - $event->handled = $this->isStopped($event->id); - } - - /** - * Sets stop flag. - * - * @param string $id of a job - * @return bool - */ - public function stop($id) - { - if (!$this->checkWaiting || $this->owner->isWaiting($id)) { - $this->markAsStopped($id); - return true; - } - - return false; - } - /** * @param string $id of a job * @return bool