diff options
author | Robin Appelman <robin@icewind.nl> | 2017-08-24 16:06:37 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-08-24 16:06:37 +0200 |
commit | 9731350acef75931ccbeafa054b40afe8189653f (patch) | |
tree | 3da1e286c8ab55882daa05223f9987fae86c8c50 /lib | |
parent | ae0789ef433adad73bdf77c57b8ead5616ed99fe (diff) | |
download | nextcloud-server-9731350acef75931ccbeafa054b40afe8189653f.tar.gz nextcloud-server-9731350acef75931ccbeafa054b40afe8189653f.zip |
split async test bus for easier subclassing
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Command/AsyncBus.php | 55 | ||||
-rw-r--r-- | lib/private/Command/CronBus.php | 75 | ||||
-rw-r--r-- | lib/private/Server.php | 4 |
3 files changed, 86 insertions, 48 deletions
diff --git a/lib/private/Command/AsyncBus.php b/lib/private/Command/AsyncBus.php index fb3cbee7240..2dffc9c784d 100644 --- a/lib/private/Command/AsyncBus.php +++ b/lib/private/Command/AsyncBus.php @@ -24,17 +24,11 @@ namespace OC\Command; use OCP\Command\IBus; use OCP\Command\ICommand; -use SuperClosure\Serializer; /** * Asynchronous command bus that uses the background job system as backend */ -class AsyncBus implements IBus { - /** - * @var \OCP\BackgroundJob\IJobList - */ - private $jobList; - +abstract class AsyncBus implements IBus { /** * List of traits for command which require sync execution * @@ -43,26 +37,26 @@ class AsyncBus implements IBus { private $syncTraits = []; /** - * @param \OCP\BackgroundJob\IJobList $jobList - */ - public function __construct($jobList) { - $this->jobList = $jobList; - } - - /** * Schedule a command to be fired * * @param \OCP\Command\ICommand | callable $command */ public function push($command) { if ($this->canRunAsync($command)) { - $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command)); + $this->queueCommand($command); } else { $this->runCommand($command); } } /** + * Queue a command in the bus + * + * @param \OCP\Command\ICommand | callable $command + */ + abstract protected function queueCommand($command); + + /** * Require all commands using a trait to be run synchronous * * @param string $trait @@ -84,37 +78,6 @@ class AsyncBus implements IBus { /** * @param \OCP\Command\ICommand | callable $command - * @return string - */ - private function getJobClass($command) { - if ($command instanceof \Closure) { - return 'OC\Command\ClosureJob'; - } else if (is_callable($command)) { - return 'OC\Command\CallableJob'; - } else if ($command instanceof ICommand) { - return 'OC\Command\CommandJob'; - } else { - throw new \InvalidArgumentException('Invalid command'); - } - } - - /** - * @param \OCP\Command\ICommand | callable $command - * @return string - */ - private function serializeCommand($command) { - if ($command instanceof \Closure) { - $serializer = new Serializer(); - return $serializer->serialize($command); - } else if (is_callable($command) or $command instanceof ICommand) { - return serialize($command); - } else { - throw new \InvalidArgumentException('Invalid command'); - } - } - - /** - * @param \OCP\Command\ICommand | callable $command * @return bool */ private function canRunAsync($command) { diff --git a/lib/private/Command/CronBus.php b/lib/private/Command/CronBus.php new file mode 100644 index 00000000000..a0ad9b3c860 --- /dev/null +++ b/lib/private/Command/CronBus.php @@ -0,0 +1,75 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Command; + +use OCP\Command\ICommand; +use SuperClosure\Serializer; + +class CronBus extends AsyncBus { + /** + * @var \OCP\BackgroundJob\IJobList + */ + private $jobList; + + + /** + * @param \OCP\BackgroundJob\IJobList $jobList + */ + public function __construct($jobList) { + $this->jobList = $jobList; + } + + protected function queueCommand($command) { + $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command)); + } + + /** + * @param \OCP\Command\ICommand | callable $command + * @return string + */ + private function getJobClass($command) { + if ($command instanceof \Closure) { + return 'OC\Command\ClosureJob'; + } else if (is_callable($command)) { + return 'OC\Command\CallableJob'; + } else if ($command instanceof ICommand) { + return 'OC\Command\CommandJob'; + } else { + throw new \InvalidArgumentException('Invalid command'); + } + } + + /** + * @param \OCP\Command\ICommand | callable $command + * @return string + */ + private function serializeCommand($command) { + if ($command instanceof \Closure) { + $serializer = new Serializer(); + return $serializer->serialize($command); + } else if (is_callable($command) or $command instanceof ICommand) { + return serialize($command); + } else { + throw new \InvalidArgumentException('Invalid command'); + } + } +}
\ No newline at end of file diff --git a/lib/private/Server.php b/lib/private/Server.php index 60a5de97bbf..d49e782ea2c 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -51,7 +51,7 @@ use OC\AppFramework\Http\Request; use OC\AppFramework\Utility\SimpleContainer; use OC\AppFramework\Utility\TimeFactory; use OC\Authentication\LoginCredentials\Store; -use OC\Command\AsyncBus; +use OC\Command\CronBus; use OC\Contacts\ContactsMenu\ActionFactory; use OC\Diagnostics\EventLogger; use OC\Diagnostics\NullEventLogger; @@ -695,7 +695,7 @@ class Server extends ServerContainer implements IServerContainer { }); $this->registerService('AsyncCommandBus', function (Server $c) { $jobList = $c->getJobList(); - return new AsyncBus($jobList); + return new CronBus($jobList); }); $this->registerService('TrustedDomainHelper', function ($c) { return new TrustedDomainHelper($this->getConfig()); |