1
0

CronBus.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 OCP\Command\ICommand;
  28. use Laravel\SerializableClosure\SerializableClosure;
  29. class CronBus extends AsyncBus {
  30. /**
  31. * @var \OCP\BackgroundJob\IJobList
  32. */
  33. private $jobList;
  34. /**
  35. * @param \OCP\BackgroundJob\IJobList $jobList
  36. */
  37. public function __construct($jobList) {
  38. $this->jobList = $jobList;
  39. }
  40. protected function queueCommand($command) {
  41. $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
  42. }
  43. /**
  44. * @param \OCP\Command\ICommand | callable $command
  45. * @return string
  46. */
  47. private function getJobClass($command) {
  48. if ($command instanceof \Closure) {
  49. return ClosureJob::class;
  50. } elseif (is_callable($command)) {
  51. return CallableJob::class;
  52. } elseif ($command instanceof ICommand) {
  53. return CommandJob::class;
  54. } else {
  55. throw new \InvalidArgumentException('Invalid command');
  56. }
  57. }
  58. /**
  59. * @param \OCP\Command\ICommand | callable $command
  60. * @return string
  61. */
  62. private function serializeCommand($command) {
  63. if ($command instanceof \Closure) {
  64. return serialize(new SerializableClosure($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. }