Skip to content

Commit 174d262

Browse files
committed
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 <julien-nc@posteo.net>
1 parent 7a63744 commit 174d262

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

lib/AppInfo/Application.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@
2525
class Application extends App implements IBootstrap {
2626

2727
public const APP_ID = 'integration_google';
28+
// consider that a job is not running anymore after N seconds
29+
public const IMPORT_JOB_TIMEOUT = 3600;
2830

29-
/**
30-
* Constructor
31-
*
32-
* @param array $urlParams
33-
*/
3431
public function __construct(array $urlParams = []) {
3532
parent::__construct(self::APP_ID, $urlParams);
3633

lib/Service/GoogleDriveAPIService.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace OCA\Google\Service;
1313

1414
use Datetime;
15+
use Exception;
1516
use OCP\Files\Folder;
1617
use OCP\Files\InvalidPathException;
1718
use OCP\Files\NotPermittedException;
@@ -26,6 +27,7 @@
2627

2728
use OCA\Google\AppInfo\Application;
2829
use OCA\Google\BackgroundJob\ImportDriveJob;
30+
use Throwable;
2931

3032
class GoogleDriveAPIService {
3133
/**
@@ -177,11 +179,20 @@ public function importDriveJob(string $userId): void {
177179
$this->userScopeService->setFilesystemScope($userId);
178180

179181
$importingDrive = $this->config->getUserValue($userId, Application::APP_ID, 'importing_drive', '0') === '1';
180-
$jobRunning = $this->config->getUserValue($userId, Application::APP_ID, 'drive_import_running', '0') === '1';
181-
if (!$importingDrive || $jobRunning) {
182+
if (!$importingDrive) {
182183
return;
183184
}
185+
$jobRunning = $this->config->getUserValue($userId, Application::APP_ID, 'drive_import_running', '0') === '1';
186+
$nowTs = (new Datetime())->getTimestamp();
187+
if ($jobRunning) {
188+
$lastJobStart = $this->config->getUserValue($userId, Application::APP_ID, 'drive_import_job_last_start');
189+
if ($lastJobStart !== '' && ($nowTs - intval($lastJobStart) < Application::IMPORT_JOB_TIMEOUT)) {
190+
// last job has started less than an hour ago => we consider it can still be running
191+
return;
192+
}
193+
}
184194
$this->config->setUserValue($userId, Application::APP_ID, 'drive_import_running', '1');
195+
$this->config->setUserValue($userId, Application::APP_ID, 'drive_import_job_last_start', strval($nowTs));
185196

186197
// import batch of files
187198
$targetPath = $this->config->getUserValue($userId, Application::APP_ID, 'drive_output_dir', '/Google Drive');
@@ -196,7 +207,7 @@ public function importDriveJob(string $userId): void {
196207
$alreadyImported = (int) $alreadyImported;
197208
try {
198209
$result = $this->importFiles($userId, $targetPath, 500000000, $alreadyImported, $directoryProgress);
199-
} catch (\Exception | \Throwable $e) {
210+
} catch (Exception | Throwable $e) {
200211
$result = [
201212
'error' => 'Unknown job failure. ' . $e,
202213
];

lib/Service/GooglePhotosAPIService.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace OCA\Google\Service;
1313

1414
use Datetime;
15+
use Exception;
1516
use OCP\Files\Folder;
1617
use OCP\IConfig;
1718
use OCP\Files\IRootFolder;
@@ -23,6 +24,7 @@
2324

2425
use OCA\Google\AppInfo\Application;
2526
use OCA\Google\BackgroundJob\ImportPhotosJob;
27+
use Throwable;
2628

2729
class GooglePhotosAPIService {
2830
/**
@@ -184,11 +186,20 @@ public function importPhotosJob(string $userId): void {
184186
$this->userScopeService->setFilesystemScope($userId);
185187

186188
$importingPhotos = $this->config->getUserValue($userId, Application::APP_ID, 'importing_photos', '0') === '1';
187-
$jobRunning = $this->config->getUserValue($userId, Application::APP_ID, 'photo_import_running', '0') === '1';
188-
if (!$importingPhotos || $jobRunning) {
189+
if (!$importingPhotos) {
189190
return;
190191
}
192+
$jobRunning = $this->config->getUserValue($userId, Application::APP_ID, 'photo_import_running', '0') === '1';
193+
$nowTs = (new Datetime())->getTimestamp();
194+
if ($jobRunning) {
195+
$lastJobStart = $this->config->getUserValue($userId, Application::APP_ID, 'photo_import_job_last_start');
196+
if ($lastJobStart !== '' && ($nowTs - intval($lastJobStart) < Application::IMPORT_JOB_TIMEOUT)) {
197+
// last job has started less than an hour ago => we consider it can still be running
198+
return;
199+
}
200+
}
191201
$this->config->setUserValue($userId, Application::APP_ID, 'photo_import_running', '1');
202+
$this->config->setUserValue($userId, Application::APP_ID, 'photo_import_job_last_start', strval($nowTs));
192203

193204
$targetPath = $this->config->getUserValue($userId, Application::APP_ID, 'photo_output_dir', '/Google Photos');
194205
$targetPath = $targetPath ?: '/Google Photos';
@@ -197,7 +208,7 @@ public function importPhotosJob(string $userId): void {
197208
$alreadyImported = (int) $alreadyImported;
198209
try {
199210
$result = $this->importPhotos($userId, $targetPath, 500000000, $alreadyImported);
200-
} catch (\Exception | \Throwable $e) {
211+
} catch (Exception | Throwable $e) {
201212
$result = [
202213
'error' => 'Unknown job failure. ' . $e->getMessage(),
203214
];

0 commit comments

Comments
 (0)