You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CronBus.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Command;
  27. use Laravel\SerializableClosure\SerializableClosure;
  28. use OCP\BackgroundJob\IJob;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\Command\ICommand;
  31. class CronBus extends AsyncBus {
  32. public function __construct(
  33. private IJobList $jobList,
  34. ) {
  35. }
  36. protected function queueCommand($command): void {
  37. $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
  38. }
  39. /**
  40. * @param ICommand|callable $command
  41. * @return class-string<IJob>
  42. */
  43. private function getJobClass($command): string {
  44. if ($command instanceof \Closure) {
  45. return ClosureJob::class;
  46. } elseif (is_callable($command)) {
  47. return CallableJob::class;
  48. } elseif ($command instanceof ICommand) {
  49. return CommandJob::class;
  50. } else {
  51. throw new \InvalidArgumentException('Invalid command');
  52. }
  53. }
  54. /**
  55. * @param ICommand|callable $command
  56. * @return string
  57. */
  58. private function serializeCommand($command): string {
  59. if ($command instanceof \Closure) {
  60. return serialize(new SerializableClosure($command));
  61. } elseif (is_callable($command) or $command instanceof ICommand) {
  62. return serialize($command);
  63. } else {
  64. throw new \InvalidArgumentException('Invalid command');
  65. }
  66. }
  67. }