2017-08-24 10:06:37 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-08-24 10:06:37 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Command;
|
|
|
|
|
|
2022-06-14 04:58:24 -04:00
|
|
|
use Laravel\SerializableClosure\SerializableClosure;
|
2024-02-08 05:52:40 -05:00
|
|
|
use OCP\BackgroundJob\IJob;
|
|
|
|
|
use OCP\BackgroundJob\IJobList;
|
2017-08-24 10:06:37 -04:00
|
|
|
use OCP\Command\ICommand;
|
|
|
|
|
|
|
|
|
|
class CronBus extends AsyncBus {
|
2024-02-08 05:52:40 -05:00
|
|
|
public function __construct(
|
|
|
|
|
private IJobList $jobList,
|
|
|
|
|
) {
|
2017-08-24 10:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-08 05:52:40 -05:00
|
|
|
protected function queueCommand($command): void {
|
2017-08-24 10:06:37 -04:00
|
|
|
$this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-08 05:52:40 -05:00
|
|
|
* @param ICommand|callable $command
|
|
|
|
|
* @return class-string<IJob>
|
2017-08-24 10:06:37 -04:00
|
|
|
*/
|
2024-02-08 05:52:40 -05:00
|
|
|
private function getJobClass($command): string {
|
2017-08-24 10:06:37 -04:00
|
|
|
if ($command instanceof \Closure) {
|
2018-01-25 17:16:13 -05:00
|
|
|
return ClosureJob::class;
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif (is_callable($command)) {
|
2018-01-25 17:16:13 -05:00
|
|
|
return CallableJob::class;
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($command instanceof ICommand) {
|
2018-01-25 17:16:13 -05:00
|
|
|
return CommandJob::class;
|
2017-08-24 10:06:37 -04:00
|
|
|
} else {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid command');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-08 05:52:40 -05:00
|
|
|
* @param ICommand|callable $command
|
2017-08-24 10:06:37 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2024-02-08 05:52:40 -05:00
|
|
|
private function serializeCommand($command): string {
|
2017-08-24 10:06:37 -04:00
|
|
|
if ($command instanceof \Closure) {
|
2022-06-14 04:58:24 -04:00
|
|
|
return serialize(new SerializableClosure($command));
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif (is_callable($command) or $command instanceof ICommand) {
|
2022-06-14 04:58:24 -04:00
|
|
|
return serialize($command);
|
2017-08-24 10:06:37 -04:00
|
|
|
} else {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid command');
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-04 08:21:44 -04:00
|
|
|
}
|