Skip to content

Commit

Permalink
refs #35 #115 #116 add a 1h timeout after which a job is not consider…
Browse files Browse the repository at this point in the history
…ed running anymore so another import can start

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Dec 21, 2022
1 parent 7a63744 commit 174d262
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
7 changes: 2 additions & 5 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
17 changes: 14 additions & 3 deletions lib/Service/GoogleDriveAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace OCA\Google\Service;

use Datetime;
use Exception;
use OCP\Files\Folder;
use OCP\Files\InvalidPathException;
use OCP\Files\NotPermittedException;
Expand All @@ -26,6 +27,7 @@

use OCA\Google\AppInfo\Application;
use OCA\Google\BackgroundJob\ImportDriveJob;
use Throwable;

class GoogleDriveAPIService {
/**
Expand Down Expand Up @@ -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');
Expand All @@ -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,
];
Expand Down
17 changes: 14 additions & 3 deletions lib/Service/GooglePhotosAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace OCA\Google\Service;

use Datetime;
use Exception;
use OCP\Files\Folder;
use OCP\IConfig;
use OCP\Files\IRootFolder;
Expand All @@ -23,6 +24,7 @@

use OCA\Google\AppInfo\Application;
use OCA\Google\BackgroundJob\ImportPhotosJob;
use Throwable;

class GooglePhotosAPIService {
/**
Expand Down Expand Up @@ -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';
Expand All @@ -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(),
];
Expand Down

0 comments on commit 174d262

Please sign in to comment.