diff --git a/lib/Controller/TaskProcessingController.php b/lib/Controller/TaskProcessingController.php index 8a2305b31..9a116bf53 100644 --- a/lib/Controller/TaskProcessingController.php +++ b/lib/Controller/TaskProcessingController.php @@ -11,6 +11,8 @@ use OCA\AppAPI\AppInfo\Application; use OCA\AppAPI\Attribute\AppAPIAuth; +use OCA\AppAPI\Service\AppAPIService; +use OCA\AppAPI\Service\ExAppService; use OCA\AppAPI\Service\ProvidersAI\TaskProcessingService; use OCP\AppFramework\Http; use OCP\AppFramework\Http\Attribute\NoCSRFRequired; @@ -26,10 +28,14 @@ class TaskProcessingController extends OCSController { public function __construct( IRequest $request, private readonly TaskProcessingService $taskProcessingService, + private readonly AppAPIService $appAPIService, + private readonly ExAppService $exAppService, ) { parent::__construct(Application::APP_ID, $request); $this->request = $request; + $this->taskProcessingService->setAppAPIService($this->appAPIService); + $this->taskProcessingService->setExAppService($this->exAppService); } #[NoCSRFRequired] diff --git a/lib/Listener/GetTaskProcessingProvidersListener.php b/lib/Listener/GetTaskProcessingProvidersListener.php index 2709cb829..bf08b7b9a 100644 --- a/lib/Listener/GetTaskProcessingProvidersListener.php +++ b/lib/Listener/GetTaskProcessingProvidersListener.php @@ -10,6 +10,8 @@ namespace OCA\AppAPI\Listener; use JsonException; +use OCA\AppAPI\Service\AppAPIService; +use OCA\AppAPI\Service\ExAppService; use OCA\AppAPI\Service\ProvidersAI\TaskProcessingService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; @@ -19,6 +21,8 @@ class GetTaskProcessingProvidersListener implements IEventListener { public function __construct( private readonly TaskProcessingService $taskProcessingService, + private readonly ExAppService $exAppService, + private readonly AppAPIService $appAPIService, private readonly LoggerInterface $logger, ) { } @@ -32,13 +36,15 @@ public function handle(Event $event): void { return; } + $this->taskProcessingService->setExAppService($this->exAppService); + $this->taskProcessingService->setAppAPIService($this->appAPIService); $exAppsProviders = $this->taskProcessingService->getRegisteredTaskProcessingProviders(); foreach ($exAppsProviders as $exAppProvider) { try { // Decode provider data $providerData = json_decode($exAppProvider->getProvider(), true, 512, JSON_THROW_ON_ERROR); - $providerInstance = $this->taskProcessingService->getAnonymousExAppProvider($providerData); + $providerInstance = $this->taskProcessingService->getAnonymousExAppProvider($providerData, $exAppProvider->getAppId()); $event->addProvider($providerInstance); // Decode and add custom task type if it exists diff --git a/lib/Service/AppAPIService.php b/lib/Service/AppAPIService.php index c42c50788..2a31345f7 100644 --- a/lib/Service/AppAPIService.php +++ b/lib/Service/AppAPIService.php @@ -52,6 +52,7 @@ public function __construct( private readonly HarpService $harpService, ) { $this->client = $clientService->newClient(); + $this->exAppService->setAppAPIService($this); } /** diff --git a/lib/Service/ExAppService.php b/lib/Service/ExAppService.php index ef9272f02..53ac5aaaf 100644 --- a/lib/Service/ExAppService.php +++ b/lib/Service/ExAppService.php @@ -36,6 +36,7 @@ class ExAppService { private ?ICache $cache = null; + private AppAPIService $appAPIService; public function __construct( private readonly LoggerInterface $logger, @@ -66,6 +67,7 @@ public function __construct( $this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/service'); } } + $this->taskProcessingService->setExAppService($this); } public function getExApp(string $appId): ?ExApp { @@ -446,4 +448,9 @@ private function unregisterExAppWebhooks(string $appId): void { $this->logger->debug(sprintf('Error while unregistering ExApp %s webhooks: %s', $appId, $e->getMessage())); } } + + public function setAppAPIService(AppAPIService $appAPIService): void { + $this->appAPIService = $appAPIService; + $this->taskProcessingService->setAppAPIService($this->appAPIService); + } } diff --git a/lib/Service/ProvidersAI/TaskProcessingService.php b/lib/Service/ProvidersAI/TaskProcessingService.php index 64adac9e9..36cb15931 100644 --- a/lib/Service/ProvidersAI/TaskProcessingService.php +++ b/lib/Service/ProvidersAI/TaskProcessingService.php @@ -13,6 +13,8 @@ use OCA\AppAPI\AppInfo\Application; use OCA\AppAPI\Db\TaskProcessing\TaskProcessingProvider; use OCA\AppAPI\Db\TaskProcessing\TaskProcessingProviderMapper; +use OCA\AppAPI\Service\AppAPIService; +use OCA\AppAPI\Service\ExAppService; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; @@ -23,6 +25,7 @@ use OCP\TaskProcessing\EShapeType; use OCP\TaskProcessing\IProvider; use OCP\TaskProcessing\ITaskType; +use OCP\TaskProcessing\ITriggerableProvider; use OCP\TaskProcessing\ShapeDescriptor; use OCP\TaskProcessing\ShapeEnumValue; use Psr\Log\LoggerInterface; @@ -31,6 +34,9 @@ class TaskProcessingService { private ?ICache $cache = null; private ?array $registeredProviders = null; + private AppAPIService $appAPIService; + private ExAppService $exAppService; + public function __construct( ICacheFactory $cacheFactory, private readonly TaskProcessingProviderMapper $mapper, @@ -41,6 +47,14 @@ public function __construct( } } + public function setAppAPIService(AppAPIService $appAPIService): void { + $this->appAPIService = $appAPIService; + } + + public function setExAppService(ExAppService $exAppService): void { + $this->exAppService = $exAppService; + } + /** * Get list of registered TaskProcessing providers (only for enabled ExApps) * @@ -241,7 +255,7 @@ public function registerExAppTaskProcessingProviders(IRegistrationContext $conte $className = '\\OCA\\AppAPI\\' . $exAppProvider->getAppId() . '\\' . $exAppProvider->getName(); try { - $provider = $this->getAnonymousExAppProvider(json_decode($exAppProvider->getProvider(), true, flags: JSON_THROW_ON_ERROR)); + $provider = $this->getAnonymousExAppProvider(json_decode($exAppProvider->getProvider(), true, flags: JSON_THROW_ON_ERROR), $exAppProvider->getAppId()); } catch (JsonException $e) { $this->logger->debug('Failed to register ExApp TaskProcessing provider', ['exAppId' => $exAppProvider->getAppId(), 'taskType' => $exAppProvider->getName(), 'exception' => $e]); continue; @@ -261,10 +275,14 @@ public function registerExAppTaskProcessingProviders(IRegistrationContext $conte */ public function getAnonymousExAppProvider( array $provider, + string $appId, ): IProvider { - return new class($provider) implements IProvider { + return new class($provider, $appId, $this->exAppService, $this->appAPIService) implements IProvider, ITriggerableProvider { public function __construct( private readonly array $provider, + private readonly string $appId, + private readonly ExAppService $exAppService, + private readonly AppAPiService $appAPIService ) { } @@ -280,6 +298,14 @@ public function getTaskTypeId(): string { return $this->provider['task_type']; } + public function trigger(): void { + $exApp = $this->exAppService->getExApp($this->appId); + if ($exApp === null) { + return; + } + $this->appAPIService->requestToExApp($exApp, '/trigger?' . http_build_query(['providerId' => $this->provider['id']])); + } + public function getExpectedRuntime(): int { return $this->provider['expected_runtime']; }