diff options
Diffstat (limited to 'lib/private/Command')
-rw-r--r-- | lib/private/Command/AsyncBus.php | 89 | ||||
-rw-r--r-- | lib/private/Command/CallableJob.php | 21 | ||||
-rw-r--r-- | lib/private/Command/ClosureJob.php | 23 | ||||
-rw-r--r-- | lib/private/Command/CommandJob.php | 25 | ||||
-rw-r--r-- | lib/private/Command/CronBus.php | 53 | ||||
-rw-r--r-- | lib/private/Command/FileAccess.php | 21 | ||||
-rw-r--r-- | lib/private/Command/QueueBus.php | 58 |
7 files changed, 290 insertions, 0 deletions
diff --git a/lib/private/Command/AsyncBus.php b/lib/private/Command/AsyncBus.php new file mode 100644 index 00000000000..fb7860c9dd8 --- /dev/null +++ b/lib/private/Command/AsyncBus.php @@ -0,0 +1,89 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Command; + +use OCP\Command\IBus; +use OCP\Command\ICommand; + +/** + * Asynchronous command bus that uses the background job system as backend + */ +abstract class AsyncBus implements IBus { + /** + * List of traits for command which require sync execution + * + * @var string[] + */ + private $syncTraits = []; + + /** + * Schedule a command to be fired + * + * @param \OCP\Command\ICommand | callable $command + */ + public function push($command) { + if ($this->canRunAsync($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 + */ + public function requireSync($trait) { + $this->syncTraits[] = trim($trait, '\\'); + } + + /** + * @param \OCP\Command\ICommand | callable $command + */ + private function runCommand($command) { + if ($command instanceof ICommand) { + $command->handle(); + } else { + $command(); + } + } + + /** + * @param \OCP\Command\ICommand | callable $command + * @return bool + */ + private function canRunAsync($command) { + $traits = $this->getTraits($command); + foreach ($traits as $trait) { + if (in_array($trait, $this->syncTraits)) { + return false; + } + } + return true; + } + + /** + * @param \OCP\Command\ICommand | callable $command + * @return string[] + */ + private function getTraits($command) { + if ($command instanceof ICommand) { + return class_uses($command); + } else { + return []; + } + } +} diff --git a/lib/private/Command/CallableJob.php b/lib/private/Command/CallableJob.php new file mode 100644 index 00000000000..8744977a81e --- /dev/null +++ b/lib/private/Command/CallableJob.php @@ -0,0 +1,21 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Command; + +use OCP\BackgroundJob\QueuedJob; + +class CallableJob extends QueuedJob { + protected function run($serializedCallable) { + $callable = unserialize($serializedCallable); + if (is_callable($callable)) { + $callable(); + } else { + throw new \InvalidArgumentException('Invalid serialized callable'); + } + } +} diff --git a/lib/private/Command/ClosureJob.php b/lib/private/Command/ClosureJob.php new file mode 100644 index 00000000000..58fe9696437 --- /dev/null +++ b/lib/private/Command/ClosureJob.php @@ -0,0 +1,23 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Command; + +use Laravel\SerializableClosure\SerializableClosure as LaravelClosure; +use OCP\BackgroundJob\QueuedJob; + +class ClosureJob extends QueuedJob { + protected function run($argument) { + $callable = unserialize($argument, [LaravelClosure::class]); + $callable = $callable->getClosure(); + if (is_callable($callable)) { + $callable(); + } else { + throw new \InvalidArgumentException('Invalid serialized callable'); + } + } +} diff --git a/lib/private/Command/CommandJob.php b/lib/private/Command/CommandJob.php new file mode 100644 index 00000000000..41ca3a9cb40 --- /dev/null +++ b/lib/private/Command/CommandJob.php @@ -0,0 +1,25 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Command; + +use OCP\BackgroundJob\QueuedJob; +use OCP\Command\ICommand; + +/** + * Wrap a command in the background job interface + */ +class CommandJob extends QueuedJob { + protected function run($argument) { + $command = unserialize($argument); + if ($command instanceof ICommand) { + $command->handle(); + } else { + throw new \InvalidArgumentException('Invalid serialized command'); + } + } +} diff --git a/lib/private/Command/CronBus.php b/lib/private/Command/CronBus.php new file mode 100644 index 00000000000..a12520469a9 --- /dev/null +++ b/lib/private/Command/CronBus.php @@ -0,0 +1,53 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Command; + +use Laravel\SerializableClosure\SerializableClosure; +use OCP\BackgroundJob\IJob; +use OCP\BackgroundJob\IJobList; +use OCP\Command\ICommand; + +class CronBus extends AsyncBus { + public function __construct( + private IJobList $jobList, + ) { + } + + protected function queueCommand($command): void { + $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command)); + } + + /** + * @param ICommand|callable $command + * @return class-string<IJob> + */ + private function getJobClass($command): string { + if ($command instanceof \Closure) { + return ClosureJob::class; + } elseif (is_callable($command)) { + return CallableJob::class; + } elseif ($command instanceof ICommand) { + return CommandJob::class; + } else { + throw new \InvalidArgumentException('Invalid command'); + } + } + + /** + * @param ICommand|callable $command + * @return string + */ + private function serializeCommand($command): string { + if ($command instanceof \Closure) { + return serialize(new SerializableClosure($command)); + } elseif (is_callable($command) or $command instanceof ICommand) { + return serialize($command); + } else { + throw new \InvalidArgumentException('Invalid command'); + } + } +} diff --git a/lib/private/Command/FileAccess.php b/lib/private/Command/FileAccess.php new file mode 100644 index 00000000000..1af1591492d --- /dev/null +++ b/lib/private/Command/FileAccess.php @@ -0,0 +1,21 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Command; + +use OCP\IUser; + +trait FileAccess { + protected function setupFS(IUser $user) { + \OC_Util::setupFS($user->getUID()); + } + + protected function getUserFolder(IUser $user) { + $this->setupFS($user); + return \OC::$server->getUserFolder($user->getUID()); + } +} diff --git a/lib/private/Command/QueueBus.php b/lib/private/Command/QueueBus.php new file mode 100644 index 00000000000..ed7d43b38b6 --- /dev/null +++ b/lib/private/Command/QueueBus.php @@ -0,0 +1,58 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Command; + +use OCP\Command\IBus; +use OCP\Command\ICommand; + +class QueueBus implements IBus { + /** + * @var ICommand[]|callable[] + */ + private $queue = []; + + /** + * Schedule a command to be fired + * + * @param \OCP\Command\ICommand | callable $command + */ + public function push($command) { + $this->queue[] = $command; + } + + /** + * Require all commands using a trait to be run synchronous + * + * @param string $trait + */ + public function requireSync($trait) { + } + + /** + * @param \OCP\Command\ICommand | callable $command + */ + private function runCommand($command) { + if ($command instanceof ICommand) { + // ensure the command can be serialized + $serialized = serialize($command); + if (strlen($serialized) > 4000) { + throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)'); + } + $unserialized = unserialize($serialized); + $unserialized->handle(); + } else { + $command(); + } + } + + public function run() { + while ($command = array_shift($this->queue)) { + $this->runCommand($command); + } + } +} |