Skip to content

Commit

Permalink
Lets test if we can hook the first cron sync to the app register event
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Nov 2, 2024
1 parent e6f3c99 commit 3d45dcc
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,51 @@
use OCA\OpenCatalogi\Dashboard\CatalogWidget;
use OCA\OpenCatalogi\Dashboard\UnpublishedPublicationsWidget;
use OCA\OpenCatalogi\Dashboard\UnpublishedAttachmentsWidget;
use OCP\IConfig;

/**
* Main Application class for OpenCatalogi
*/
class Application extends App implements IBootstrap {
public const APP_ID = 'opencatalogi';

/** @psalm-suppress PossiblyUnusedMethod */
public function __construct() {
parent::__construct(self::APP_ID);

// Listen for the post installation event
$container = $this->getContainer();
$this->listen($container);
}

/**
* Register event listeners
*/
private function listen($container): void {
$dispatcher = $container->getServer()->getEventDispatcher();

// Listen for app installation
$dispatcher->addListener('app.register', function() use ($container) {
// Get app config to check if initial sync has been done
$config = $container->get(IConfig::class);
$initialSyncDone = $config->getAppValue(self::APP_ID, 'initial_sync_done', 'false');

// Only run if initial sync hasn't been done
if ($initialSyncDone === 'false') {
try {
// Get DirectoryService and run sync
$directoryService = $container->get(\OCA\OpenCatalogi\Service\DirectoryService::class);
$directoryService->doCronSync();

// Mark initial sync as done
$config->setAppValue(self::APP_ID, 'initial_sync_done', 'true');
} catch (\Exception $e) {
\OC::$server->getLogger()->error('Failed to run initial directory sync: ' . $e->getMessage(), [
'app' => self::APP_ID
]);
}
}
});
}

public function register(IRegistrationContext $context): void {
Expand Down

0 comments on commit 3d45dcc

Please sign in to comment.