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.

79 lines
2.2 KiB

3 years ago
  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. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Command;
  26. use OCP\Command\ICommand;
  27. use SuperClosure\Serializer;
  28. class CronBus extends AsyncBus {
  29. /**
  30. * @var \OCP\BackgroundJob\IJobList
  31. */
  32. private $jobList;
  33. /**
  34. * @param \OCP\BackgroundJob\IJobList $jobList
  35. */
  36. public function __construct($jobList) {
  37. $this->jobList = $jobList;
  38. }
  39. protected function queueCommand($command) {
  40. $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
  41. }
  42. /**
  43. * @param \OCP\Command\ICommand | callable $command
  44. * @return string
  45. */
  46. private function getJobClass($command) {
  47. if ($command instanceof \Closure) {
  48. return ClosureJob::class;
  49. } elseif (is_callable($command)) {
  50. return CallableJob::class;
  51. } elseif ($command instanceof ICommand) {
  52. return CommandJob::class;
  53. } else {
  54. throw new \InvalidArgumentException('Invalid command');
  55. }
  56. }
  57. /**
  58. * @param \OCP\Command\ICommand | callable $command
  59. * @return string
  60. */
  61. private function serializeCommand($command) {
  62. if ($command instanceof \Closure) {
  63. $serializer = new Serializer();
  64. return $serializer->serialize($command);
  65. } elseif (is_callable($command) or $command instanceof ICommand) {
  66. return serialize($command);
  67. } else {
  68. throw new \InvalidArgumentException('Invalid command');
  69. }
  70. }
  71. }