From 174d2625e1987fbc89baa952c7bdffea168f17b8 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Wed, 21 Dec 2022 15:16:33 +0100 Subject: [PATCH] refs #35 #115 #116 add a 1h timeout after which a job is not considered running anymore so another import can start Signed-off-by: Julien Veyssier --- lib/AppInfo/Application.php | 7 ++----- lib/Service/GoogleDriveAPIService.php | 17 ++++++++++++++--- lib/Service/GooglePhotosAPIService.php | 17 ++++++++++++++--- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 4f2f09e1..0e23712e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -25,12 +25,9 @@ class Application extends App implements IBootstrap { public const APP_ID = 'integration_google'; + // consider that a job is not running anymore after N seconds + public const IMPORT_JOB_TIMEOUT = 3600; - /** - * Constructor - * - * @param array $urlParams - */ public function __construct(array $urlParams = []) { parent::__construct(self::APP_ID, $urlParams); diff --git a/lib/Service/GoogleDriveAPIService.php b/lib/Service/GoogleDriveAPIService.php index f7c7a23e..8c8eef38 100644 --- a/lib/Service/GoogleDriveAPIService.php +++ b/lib/Service/GoogleDriveAPIService.php @@ -12,6 +12,7 @@ namespace OCA\Google\Service; use Datetime; +use Exception; use OCP\Files\Folder; use OCP\Files\InvalidPathException; use OCP\Files\NotPermittedException; @@ -26,6 +27,7 @@ use OCA\Google\AppInfo\Application; use OCA\Google\BackgroundJob\ImportDriveJob; +use Throwable; class GoogleDriveAPIService { /** @@ -177,11 +179,20 @@ public function importDriveJob(string $userId): void { $this->userScopeService->setFilesystemScope($userId); $importingDrive = $this->config->getUserValue($userId, Application::APP_ID, 'importing_drive', '0') === '1'; - $jobRunning = $this->config->getUserValue($userId, Application::APP_ID, 'drive_import_running', '0') === '1'; - if (!$importingDrive || $jobRunning) { + if (!$importingDrive) { return; } + $jobRunning = $this->config->getUserValue($userId, Application::APP_ID, 'drive_import_running', '0') === '1'; + $nowTs = (new Datetime())->getTimestamp(); + if ($jobRunning) { + $lastJobStart = $this->config->getUserValue($userId, Application::APP_ID, 'drive_import_job_last_start'); + if ($lastJobStart !== '' && ($nowTs - intval($lastJobStart) < Application::IMPORT_JOB_TIMEOUT)) { + // last job has started less than an hour ago => we consider it can still be running + return; + } + } $this->config->setUserValue($userId, Application::APP_ID, 'drive_import_running', '1'); + $this->config->setUserValue($userId, Application::APP_ID, 'drive_import_job_last_start', strval($nowTs)); // import batch of files $targetPath = $this->config->getUserValue($userId, Application::APP_ID, 'drive_output_dir', '/Google Drive'); @@ -196,7 +207,7 @@ public function importDriveJob(string $userId): void { $alreadyImported = (int) $alreadyImported; try { $result = $this->importFiles($userId, $targetPath, 500000000, $alreadyImported, $directoryProgress); - } catch (\Exception | \Throwable $e) { + } catch (Exception | Throwable $e) { $result = [ 'error' => 'Unknown job failure. ' . $e, ]; diff --git a/lib/Service/GooglePhotosAPIService.php b/lib/Service/GooglePhotosAPIService.php index f1d2d9fd..e0bf55ff 100644 --- a/lib/Service/GooglePhotosAPIService.php +++ b/lib/Service/GooglePhotosAPIService.php @@ -12,6 +12,7 @@ namespace OCA\Google\Service; use Datetime; +use Exception; use OCP\Files\Folder; use OCP\IConfig; use OCP\Files\IRootFolder; @@ -23,6 +24,7 @@ use OCA\Google\AppInfo\Application; use OCA\Google\BackgroundJob\ImportPhotosJob; +use Throwable; class GooglePhotosAPIService { /** @@ -184,11 +186,20 @@ public function importPhotosJob(string $userId): void { $this->userScopeService->setFilesystemScope($userId); $importingPhotos = $this->config->getUserValue($userId, Application::APP_ID, 'importing_photos', '0') === '1'; - $jobRunning = $this->config->getUserValue($userId, Application::APP_ID, 'photo_import_running', '0') === '1'; - if (!$importingPhotos || $jobRunning) { + if (!$importingPhotos) { return; } + $jobRunning = $this->config->getUserValue($userId, Application::APP_ID, 'photo_import_running', '0') === '1'; + $nowTs = (new Datetime())->getTimestamp(); + if ($jobRunning) { + $lastJobStart = $this->config->getUserValue($userId, Application::APP_ID, 'photo_import_job_last_start'); + if ($lastJobStart !== '' && ($nowTs - intval($lastJobStart) < Application::IMPORT_JOB_TIMEOUT)) { + // last job has started less than an hour ago => we consider it can still be running + return; + } + } $this->config->setUserValue($userId, Application::APP_ID, 'photo_import_running', '1'); + $this->config->setUserValue($userId, Application::APP_ID, 'photo_import_job_last_start', strval($nowTs)); $targetPath = $this->config->getUserValue($userId, Application::APP_ID, 'photo_output_dir', '/Google Photos'); $targetPath = $targetPath ?: '/Google Photos'; @@ -197,7 +208,7 @@ public function importPhotosJob(string $userId): void { $alreadyImported = (int) $alreadyImported; try { $result = $this->importPhotos($userId, $targetPath, 500000000, $alreadyImported); - } catch (\Exception | \Throwable $e) { + } catch (Exception | Throwable $e) { $result = [ 'error' => 'Unknown job failure. ' . $e->getMessage(), ];