summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2015-02-17 16:49:14 +0100
committerRobin Appelman <icewind@owncloud.com>2015-02-25 15:08:40 +0100
commit74ae7b8929a7fd3f539fd15efb9533424114a480 (patch)
tree4388660851beff515a0ec78a2bea199c9a057fb2 /lib
parentf5b62267325415b307cf2d47b69d11d4337536e4 (diff)
downloadnextcloud-server-74ae7b8929a7fd3f539fd15efb9533424114a480.tar.gz
nextcloud-server-74ae7b8929a7fd3f539fd15efb9533424114a480.zip
Add async command system to handle asynchronous operations
Diffstat (limited to 'lib')
-rw-r--r--lib/private/backgroundjob/queuedjob.php2
-rw-r--r--lib/private/command/asyncbus.php70
-rw-r--r--lib/private/command/callablejob.php22
-rw-r--r--lib/private/command/closurejob.php24
-rw-r--r--lib/private/command/commandjob.php26
-rw-r--r--lib/private/server.php12
-rw-r--r--lib/public/command/ibus.php18
-rw-r--r--lib/public/command/icommand.php16
-rw-r--r--lib/public/iservercontainer.php5
9 files changed, 194 insertions, 1 deletions
diff --git a/lib/private/backgroundjob/queuedjob.php b/lib/private/backgroundjob/queuedjob.php
index 884b22a40fb..93dc5a2f063 100644
--- a/lib/private/backgroundjob/queuedjob.php
+++ b/lib/private/backgroundjob/queuedjob.php
@@ -35,7 +35,7 @@ abstract class QueuedJob extends Job {
* @param \OC\Log $logger
*/
public function execute($jobList, $logger = null) {
- $jobList->remove($this);
+ $jobList->remove($this, $this->argument);
parent::execute($jobList, $logger);
}
}
diff --git a/lib/private/command/asyncbus.php b/lib/private/command/asyncbus.php
new file mode 100644
index 00000000000..fc9c85acc3f
--- /dev/null
+++ b/lib/private/command/asyncbus.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+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;
+
+ /**
+ * @param \OCP\BackgroundJob\IJobList $jobList
+ */
+ function __construct($jobList) {
+ $this->jobList = $jobList;
+ }
+
+ /**
+ * Schedule a command to be fired
+ *
+ * @param \OCP\Command\ICommand | callable $command
+ */
+ public function push($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');
+ }
+ }
+}
diff --git a/lib/private/command/callablejob.php b/lib/private/command/callablejob.php
new file mode 100644
index 00000000000..6b755d615e6
--- /dev/null
+++ b/lib/private/command/callablejob.php
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Command;
+
+use OC\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..abba120b745
--- /dev/null
+++ b/lib/private/command/closurejob.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Command;
+
+use OC\BackgroundJob\QueuedJob;
+use SuperClosure\Serializer;
+
+class ClosureJob extends QueuedJob {
+ protected function run($serializedCallable) {
+ $serializer = new Serializer();
+ $callable = $serializer->unserialize($serializedCallable);
+ 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..b2c7d30ee56
--- /dev/null
+++ b/lib/private/command/commandjob.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Command;
+
+use OC\BackgroundJob\QueuedJob;
+use OCP\Command\ICommand;
+
+/**
+ * Wrap a command in the background job interface
+ */
+class CommandJob extends QueuedJob {
+ protected function run($serializedCommand) {
+ $command = unserialize($serializedCommand);
+ if ($command instanceof ICommand) {
+ $command->handle();
+ } else {
+ throw new \InvalidArgumentException('Invalid serialized command');
+ }
+ }
+}
diff --git a/lib/private/server.php b/lib/private/server.php
index f6fa5387e49..ca3a17451ac 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -38,6 +38,7 @@ use OC\AppFramework\Http\Request;
use OC\AppFramework\Db\Db;
use OC\AppFramework\Utility\SimpleContainer;
use OC\Cache\UserCache;
+use OC\Command\AsyncBus;
use OC\Diagnostics\NullQueryLogger;
use OC\Diagnostics\EventLogger;
use OC\Diagnostics\QueryLogger;
@@ -291,6 +292,10 @@ class Server extends SimpleContainer implements IServerContainer {
$this->registerService('IniWrapper', function ($c) {
return new IniGetWrapper();
});
+ $this->registerService('AsyncCommandBus', function (Server $c) {
+ $jobList = $c->getJobList();
+ return new AsyncBus($jobList);
+ });
$this->registerService('TrustedDomainHelper', function ($c) {
return new TrustedDomainHelper($this->getConfig());
});
@@ -778,6 +783,13 @@ class Server extends SimpleContainer implements IServerContainer {
}
/**
+ * @return \OCP\Command\IBus
+ */
+ function getAsyncCommandBus(){
+ return $this->query('AsyncCommandBus');
+ }
+
+ /**
* Get the trusted domain helper
*
* @return TrustedDomainHelper
diff --git a/lib/public/command/ibus.php b/lib/public/command/ibus.php
new file mode 100644
index 00000000000..707f8fd072d
--- /dev/null
+++ b/lib/public/command/ibus.php
@@ -0,0 +1,18 @@
+<?php
+/**
+ * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCP\Command;
+
+interface IBus {
+ /**
+ * Schedule a command to be fired
+ *
+ * @param \OCP\Command\ICommand | callable $command
+ */
+ public function push($command);
+}
diff --git a/lib/public/command/icommand.php b/lib/public/command/icommand.php
new file mode 100644
index 00000000000..6de61258a41
--- /dev/null
+++ b/lib/public/command/icommand.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCP\Command;
+
+interface ICommand {
+ /**
+ * Run the command
+ */
+ public function handle();
+}
diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php
index f2806529a4c..3b73426d9fb 100644
--- a/lib/public/iservercontainer.php
+++ b/lib/public/iservercontainer.php
@@ -317,4 +317,9 @@ interface IServerContainer {
* @return \bantu\IniGetWrapper\IniGetWrapper
*/
function getIniWrapper();
+
+ /**
+ * @return \OCP\Command\IBus
+ */
+ function getAsyncCommandBus();
}