Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Controller/TaskProcessingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion lib/Listener/GetTaskProcessingProvidersListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/Service/AppAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct(
private readonly HarpService $harpService,
) {
$this->client = $clientService->newClient();
$this->exAppService->setAppAPIService($this);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions lib/Service/ExAppService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

class ExAppService {
private ?ICache $cache = null;
private AppAPIService $appAPIService;

public function __construct(
private readonly LoggerInterface $logger,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
30 changes: 28 additions & 2 deletions lib/Service/ProvidersAI/TaskProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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)
*
Expand Down Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand All @@ -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'];
}
Expand Down