From 50ac07728c0245f07b126eb40b03f57ea10ea0e0 Mon Sep 17 00:00:00 2001 From: Florian Charlaix Date: Tue, 22 Aug 2023 15:23:17 +0200 Subject: [PATCH] Add debug/info logs --- lib/AppInfo/Application.php | 27 +++++++++++++++++++++------ lib/Migration/BackgroundJob.php | 23 +++++++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index ac55251..74c9811 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -27,9 +27,10 @@ use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IRegistrationContext; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\EventDispatcher\GenericEvent; use OCP\IUser; +use Psr\Log\LoggerInterface; use OCP\BackgroundJob\IJobList; use OCP\Notification\IManager as INotificationManager; use OCA\FirstRunMigrate\Migration\Notifier; @@ -52,35 +53,49 @@ class Application extends App implements IBootstrap { public function register(IRegistrationContext $context): void { } - public function registerHooks(EventDispatcherInterface $dispatcher, IJobList $jobList) { + public function registerHooks(IEventDispatcher $dispatcher, IJobList $jobList, LoggerInterface $logger) { + // first time login event setup - $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($jobList) { + $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($jobList, $logger) { if ($event instanceof GenericEvent) { + $logger->debug("Trigger of::firstLogin"); + /** * @var IUser */ $user = $event->getSubject(); + $uid = $user->getUID(); + $logger->debug("Subject $uid set, checking migration ID"); // If the user dont have an ID, skip all the migrations if (Utils::getUserId($user) === null) { + $logger->info("$uid don't have a migration ID"); return; } - $uid = $user->getUID(); - + $logger->debug("Checking $uid migrations"); if (Utils::isMigrationData()){ + $logger->debug("Scheduling $uid data migrations"); $jobList->add('OCA\FirstRunMigrate\Migration\BackgroundJob', ['uid' => $uid, 'type' => 'data']); Utils::setMigrationStatus('data', 'scheduled', $user); + } else { + $logger->info("$uid don't have data migrations"); } if (Utils::isMigrationGroups()) { + $logger->debug("Scheduling $uid groups migrations"); $jobList->add('OCA\FirstRunMigrate\Migration\BackgroundJob', ['uid' => $uid, 'type' => 'group']); Utils::setMigrationStatus('group', 'scheduled', $user); + } else { + $logger->info("$uid don't have groups migrations"); } if (Utils::isMigrationQuotas()) { + $logger->debug("Scheduling $uid quota migrations"); $jobList->add('OCA\FirstRunMigrate\Migration\BackgroundJob', ['uid' => $uid, 'type' => 'quota']); Utils::setMigrationStatus('quota', 'scheduled', $user); + } else { + $logger->info("$uid don't have quota migrations"); } } }); diff --git a/lib/Migration/BackgroundJob.php b/lib/Migration/BackgroundJob.php index 57b70fe..35ae821 100644 --- a/lib/Migration/BackgroundJob.php +++ b/lib/Migration/BackgroundJob.php @@ -28,11 +28,15 @@ use OCA\FirstRunMigrate\Migration\Utils; use OCP\IGroupManager; use OCP\IUser; use OCP\IUserManager; +use Psr\Log\LoggerInterface; class BackgroundJob extends QueuedJob { /** @var IConfig */ protected $config; + /** @var LoggerInterface */ + protected $logger; + /** @var IUserManager */ protected $userManager; @@ -44,8 +48,9 @@ class BackgroundJob extends QueuedJob { * * @param INotificationManager $notificationManager */ - public function __construct(IConfig $config, IUserManager $userManager, IGroupManager $groupManager) { + public function __construct(IConfig $config, IUserManager $userManager, IGroupManager $groupManager, LoggerInterface $logger) { $this->config = $config; + $this->logger = $logger; $this->userManager = $userManager; $this->groupManager = $groupManager; } @@ -54,6 +59,7 @@ class BackgroundJob extends QueuedJob { * @param array $argument */ protected function run($argument) { + $this->logger->debug("Starting job {$this->getId()} with args $argument"); $uid = $argument['uid']; $user = $this->userManager->get($uid); $type = $argument['type']; @@ -72,6 +78,7 @@ class BackgroundJob extends QueuedJob { } private function data(IUser $user) { + $this->logger->debug("{$this->getId()}: Migrating datas"); $uid = $user->getUID(); Utils::setMigrationStatus('data', 'started', $user); @@ -84,15 +91,18 @@ class BackgroundJob extends QueuedJob { $user->setQuota('none'); OC_Util::copyr($migrate_dir, $userFolder); $user->setQuota($quota); - + // update the file cache $userFolder->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE); + } else { + $this->logger->info("{$this->getId()}: No dir to migrate"); } - + Utils::setMigrationStatus('data', 'finished', $user); } private function group(IUser $user) { + $this->logger->debug("{$this->getId()}: Migrating groups"); Utils::setMigrationStatus('group', 'started', $user); if ($groups = Utils::getUserMigrationGroups($user)) { @@ -102,19 +112,24 @@ class BackgroundJob extends QueuedJob { } else { $group = $this->groupManager->createGroup($group); } - + $group->addUser($user); } + } else { + $this->logger->info("{$this->getId()}: No groups to migrate"); } Utils::setMigrationStatus('group', 'finished', $user); } private function quota(IUser $user) { + $this->logger->debug("{$this->getId()}: Migrating quota"); Utils::setMigrationStatus('quota', 'started', $user); if ($quota = Utils::getUserMigrationQuota($user)) { $user->setQuota($quota); + } else { + $this->logger->info("{$this->getId()}: No quota to migrate"); } Utils::setMigrationStatus('quota', 'finished', $user);