diff --git a/composer.json b/composer.json index fe35393b15..8d066fa88a 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "nqxcode/zendsearch": "^2.0", "xj/yii2-jplayer-widget": "*", "zendframework/zend-ldap": "^2.5", - "zhuravljov/yii2-queue": "^0.7", + "zhuravljov/yii2-queue": "^0.11", "bower-asset/jquery-timeago": "1.4.*", "bower-asset/jquery-nicescroll": "3.6.*", "bower-asset/jquery-knob": "1.2.*", diff --git a/protected/humhub/components/queue/Queue.php b/protected/humhub/components/queue/Queue.php deleted file mode 100644 index 0111da498b..0000000000 --- a/protected/humhub/components/queue/Queue.php +++ /dev/null @@ -1,39 +0,0 @@ -error; - Yii::error('Could not execute queued job! Message: ' . $exception->getMessage() . ' Trace:' . $exception->getTraceAsString(), 'queue'); - }); - } - -} diff --git a/protected/humhub/components/queue/driver/Instant.php b/protected/humhub/components/queue/driver/Instant.php index 53116af31f..217ae7513a 100644 --- a/protected/humhub/components/queue/driver/Instant.php +++ b/protected/humhub/components/queue/driver/Instant.php @@ -8,7 +8,10 @@ namespace humhub\components\queue\driver; -use zhuravljov\yii\queue\sync\Driver; +use Yii; +use yii\base\Event; +use zhuravljov\yii\queue\ErrorEvent; +use humhub\components\queue\Queue; /** * Instant queue driver, mainly used for testing purposes @@ -16,30 +19,29 @@ use zhuravljov\yii\queue\sync\Driver; * @since 1.2 * @author buddha */ -class Instant extends Driver +class Instant extends Queue { /** * @inheritdoc */ - public $handle = true; - - /** - * @var array - */ - private $_messages = []; - - /** - * Executes the jobs immediatly, serialization is done for testing purpose - */ - public function push($job) + public function init() { - $this->_messages[] = $this->serialize($job); + parent::init(); - while (($message = array_shift($this->_messages)) !== null) { - $job = $this->unserialize($message); - $this->getQueue()->run($job); - } + Event::on(Queue::class, Queue::EVENT_AFTER_ERROR, function(ErrorEvent $errorEvent) { + /* @var $exception \Expection */ + $exception = $errorEvent->error; + Yii::error('Could not execute queued job! Message: ' . $exception->getMessage() . ' Trace:' . $exception->getTraceAsString(), 'queue'); + }); + } + + /** + * @inheritdoc + */ + protected function sendMessage($message, $timeout) + { + $this->handleMessage($message); } } diff --git a/protected/humhub/components/queue/driver/MySQL.php b/protected/humhub/components/queue/driver/MySQL.php index 37fdfb7487..14d9694128 100644 --- a/protected/humhub/components/queue/driver/MySQL.php +++ b/protected/humhub/components/queue/driver/MySQL.php @@ -8,7 +8,10 @@ namespace humhub\components\queue\driver; -use zhuravljov\yii\queue\db\Driver; +use Yii; +use yii\base\Event; +use zhuravljov\yii\queue\ErrorEvent; +use zhuravljov\yii\queue\db\Queue; /** * MySQL queue driver @@ -16,9 +19,26 @@ use zhuravljov\yii\queue\db\Driver; * @since 1.2 * @author Luke */ -class MySQL extends Driver +class MySQL extends Queue { + /** + * @inheritdoc + */ public $mutex = 'yii\mutex\MysqlMutex'; + /** + * @inheritdoc + */ + public function init() + { + parent::init(); + + Event::on(Queue::class, Queue::EVENT_AFTER_ERROR, function(ErrorEvent $errorEvent) { + /* @var $exception \Expection */ + $exception = $errorEvent->error; + Yii::error('Could not execute queued job! Message: ' . $exception->getMessage() . ' Trace:' . $exception->getTraceAsString(), 'queue'); + }); + } + } diff --git a/protected/humhub/components/queue/driver/Sync.php b/protected/humhub/components/queue/driver/Sync.php index 36bf606082..dcdcb4ae37 100644 --- a/protected/humhub/components/queue/driver/Sync.php +++ b/protected/humhub/components/queue/driver/Sync.php @@ -8,7 +8,12 @@ namespace humhub\components\queue\driver; -use zhuravljov\yii\queue\sync\Driver; +use Yii; +use yii\base\Event; +use yii\base\Application; +use yii\base\NotSupportedException; +use zhuravljov\yii\queue\ErrorEvent; +use zhuravljov\yii\queue\Queue; /** * Sync queue driver @@ -16,9 +21,64 @@ use zhuravljov\yii\queue\sync\Driver; * @since 1.2 * @author Luke */ -class Sync extends Driver +class Sync extends Queue { + /** + * @var boolean + */ public $handle = true; + /** + * @var array + */ + private $messages = []; + + /** + * @inheritdoc + */ + public function init() + { + + parent::init(); + + if ($this->handle) { + Yii::$app->on(Application::EVENT_AFTER_REQUEST, function () { + ob_start(); + $this->run(); + + // Important, breaks downloads + ob_end_clean(); + }); + } + + Event::on(Queue::class, Queue::EVENT_AFTER_ERROR, function(ErrorEvent $errorEvent) { + /* @var $exception \Expection */ + $exception = $errorEvent->error; + Yii::error('Could not execute queued job! Message: ' . $exception->getMessage() . ' Trace:' . $exception->getTraceAsString(), 'queue'); + }); + } + + /** + * Runs all jobs from queue. + */ + public function run() + { + while (($message = array_shift($this->messages)) !== null) { + $this->handleMessage($message); + } + } + + /** + * @inheritdoc + */ + protected function sendMessage($message, $timeout) + { + if ($timeout) { + throw new NotSupportedException('Delayed work is not supported in the driver.'); + } + + $this->messages[] = $message; + } + } diff --git a/protected/humhub/config/common.php b/protected/humhub/config/common.php index da818169ae..c31cd85c5a 100644 --- a/protected/humhub/config/common.php +++ b/protected/humhub/config/common.php @@ -126,11 +126,7 @@ $config = [ 'clients' => [], ], 'queue' => [ - 'class' => 'humhub\components\queue\Queue', - 'driver' => [ - //'class' => 'humhub\components\queue\driver\MySQL', - 'class' => 'humhub\components\queue\driver\Sync', - ], + 'class' => 'humhub\components\queue\driver\Sync', ], 'live' => [ 'class' => 'humhub\modules\live\components\Sender', diff --git a/protected/humhub/migrations/m170307_170300_queuelater.php b/protected/humhub/migrations/m170307_170300_queuelater.php new file mode 100644 index 0000000000..2c0130f16a --- /dev/null +++ b/protected/humhub/migrations/m170307_170300_queuelater.php @@ -0,0 +1,19 @@ +addColumn('queue', 'timeout', $this->integer()->defaultValue(0)->notNull()->after('created_at')); + } + + public function down() + { + echo "m170307_170300_queuelater cannot be reverted.\n"; + return false; + } + +}