Skip to content

Commit

Permalink
Merge pull request #49461 from nextcloud/backport/48600/stable30
Browse files Browse the repository at this point in the history
[stable30] fix(BackgroundJobs): Adjust intervals and time sensitivities
  • Loading branch information
sorbaugh authored Nov 25, 2024
2 parents fb439da + a7f5b45 commit 03f8e90
Show file tree
Hide file tree
Showing 25 changed files with 39 additions and 49 deletions.
3 changes: 1 addition & 2 deletions apps/contactsinteraction/lib/BackgroundJob/CleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use OCA\ContactsInteraction\Db\RecentContactMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;

class CleanupJob extends TimedJob {
Expand All @@ -22,7 +21,7 @@ public function __construct(
parent::__construct($time);

$this->setInterval(24 * 60 * 60);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);

}

Expand Down
3 changes: 1 addition & 2 deletions apps/dav/lib/BackgroundJob/UploadCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use OC\User\NoUserException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\TimedJob;
use OCP\Files\File;
Expand All @@ -32,7 +31,7 @@ public function __construct(ITimeFactory $time, IRootFolder $rootFolder, IJobLis

// Run once a day
$this->setInterval(60 * 60 * 24);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

protected function run($argument) {
Expand Down
1 change: 1 addition & 0 deletions apps/federation/lib/SyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(SyncFederationAddressBooks $syncService, LoggerInter
parent::__construct($timeFactory);
// Run once a day
$this->setInterval(24 * 60 * 60);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
$this->syncService = $syncService;
$this->logger = $logger;
}
Expand Down
4 changes: 1 addition & 3 deletions apps/files/lib/BackgroundJob/CleanupDirectEditingTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
use OCP\DirectEditing\IManager;

class CleanupDirectEditingTokens extends TimedJob {
private const INTERVAL_MINUTES = 15 * 60;

private IManager $manager;

public function __construct(ITimeFactory $time,
IManager $manager) {
parent::__construct($time);
$this->interval = self::INTERVAL_MINUTES;
$this->setInterval(15 * 60);
$this->manager = $manager;
}

Expand Down
10 changes: 1 addition & 9 deletions apps/files/lib/BackgroundJob/CleanupFileLocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@
* Clean up all file locks that are expired for the DB file locking provider
*/
class CleanupFileLocks extends TimedJob {
/**
* Default interval in minutes
*
* @var int $defaultIntervalMin
**/
protected $defaultIntervalMin = 5;

/**
* sets the correct interval for this timed job
*/
public function __construct(ITimeFactory $time) {
parent::__construct($time);

$this->interval = $this->defaultIntervalMin * 60;
$this->setInterval(5 * 60);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use OCA\Files\Db\OpenLocalEditorMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;

/**
Expand All @@ -29,7 +28,7 @@ public function __construct(

// Run every 12h
$this->interval = 12 * 3600;
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions apps/files/lib/BackgroundJob/DeleteOrphanedItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
class DeleteOrphanedItems extends TimedJob {
public const CHUNK_SIZE = 200;
protected $defaultIntervalMin = 60;

/**
* sets the correct interval for this timed job
Expand All @@ -29,7 +28,7 @@ public function __construct(
protected LoggerInterface $logger,
) {
parent::__construct($time);
$this->interval = $this->defaultIntervalMin * 60;
$this->setInterval(60 * 60);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions apps/files_external/lib/BackgroundJob/CredentialsCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\UserGlobalStoragesService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -37,7 +36,7 @@ public function __construct(

// run every day
$this->setInterval(24 * 60 * 60);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

protected function run($argument) {
Expand Down
3 changes: 1 addition & 2 deletions apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use OCA\FilesReminders\Service\ReminderService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;

class CleanUpReminders extends TimedJob {
Expand All @@ -22,7 +21,7 @@ public function __construct(
parent::__construct($time);

$this->setInterval(24 * 60 * 60); // 1 day
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function __construct(
private LoggerInterface $logger,
) {
parent::__construct($time);
$this->setInterval(86400);
$this->setInterval(24 * 60 * 60);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

public function run($argument) {
Expand Down
3 changes: 1 addition & 2 deletions apps/files_sharing/lib/ExpireSharesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace OCA\Files_Sharing;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
use OCP\IDBConnection;
use OCP\Share\Exceptions\ShareNotFound;
Expand All @@ -33,7 +32,7 @@ public function __construct(ITimeFactory $time, IManager $shareManager, IDBConne

// Run once a day
$this->setInterval(24 * 60 * 60);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use OCA\OAuth2\Db\AccessTokenMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
use OCP\DB\Exception;
use Psr\Log\LoggerInterface;
Expand All @@ -28,7 +27,7 @@ public function __construct(
parent::__construct($timeFactory);
// 30 days
$this->setInterval(60 * 60 * 24 * 30);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\TimedJob;
use OCP\IUserManager;
Expand Down Expand Up @@ -43,7 +42,7 @@ public function __construct(IRegistry $registry,
$this->jobList = $jobList;

$this->setInterval(60 * 60 * 24 * 14);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

protected function run($argument) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct(
parent::__construct($timeFactory);
// Run once a day
$this->setInterval(60 * 60 * 24);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

protected function run($argument) {
Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/Jobs/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CleanUp extends TimedJob {
protected $limit;

/** @var int $defaultIntervalMin default interval in minutes */
protected $defaultIntervalMin = 51;
protected $defaultIntervalMin = 60;

/** @var User_LDAP|User_Proxy $userBackend */
protected $userBackend;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(ITimeFactory $time,
$this->mapper = $mapper;

// Run every time the cron is run
$this->setInterval(60);
$this->setInterval(0);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/BackgroundJobs/CleanupLoginFlowV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
) {
parent::__construct($time);

$this->setInterval(3600);
$this->setInterval(60 * 60);
}

protected function run($argument): void {
Expand Down
4 changes: 2 additions & 2 deletions core/BackgroundJobs/GenerateMetadataJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function __construct(
) {
parent::__construct($time);

$this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE);
$this->setInterval(24 * 3600);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
$this->setInterval(24 * 60 * 60);
}

protected function run(mixed $argument): void {
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Preview/BackgroundCleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function __construct(ITimeFactory $timeFactory,
bool $isCLI) {
parent::__construct($timeFactory);
// Run at most once an hour
$this->setInterval(3600);
$this->setInterval(60 * 60);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);

$this->connection = $connection;
$this->previewFolder = $previewFolder;
Expand Down
5 changes: 2 additions & 3 deletions lib/private/Security/Bruteforce/CleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace OC\Security\Bruteforce;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
Expand All @@ -22,8 +21,8 @@ public function __construct(
parent::__construct($time);

// Run once a day
$this->setInterval(3600 * 24);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setInterval(60 * 60 * 24);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

protected function run($argument): void {
Expand Down
4 changes: 2 additions & 2 deletions lib/private/TaskProcessing/RemoveOldTasksBackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Psr\Log\LoggerInterface;

class RemoveOldTasksBackgroundJob extends TimedJob {
public const MAX_TASK_AGE_SECONDS = 60 * 50 * 24 * 7 * 4; // 4 weeks
public const MAX_TASK_AGE_SECONDS = 60 * 60 * 24 * 7 * 4; // 4 weeks
private \OCP\Files\IAppData $appData;

public function __construct(
Expand All @@ -28,7 +28,7 @@ public function __construct(
parent::__construct($timeFactory);
$this->setInterval(60 * 60 * 24);
// can be deferred to maintenance window
$this->setTimeSensitivity(TimedJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
$this->appData = $appDataFactory->get('core');
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/TextProcessing/RemoveOldTasksBackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Psr\Log\LoggerInterface;

class RemoveOldTasksBackgroundJob extends TimedJob {
public const MAX_TASK_AGE_SECONDS = 60 * 50 * 24 * 7; // 1 week
public const MAX_TASK_AGE_SECONDS = 60 * 60 * 24 * 7; // 1 week

public function __construct(
ITimeFactory $timeFactory,
Expand All @@ -27,6 +27,7 @@ public function __construct(
) {
parent::__construct($timeFactory);
$this->setInterval(60 * 60 * 24);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/private/TextToImage/RemoveOldTasksBackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Psr\Log\LoggerInterface;

class RemoveOldTasksBackgroundJob extends TimedJob {
public const MAX_TASK_AGE_SECONDS = 60 * 50 * 24 * 7; // 1 week
public const MAX_TASK_AGE_SECONDS = 60 * 60 * 24 * 7; // 1 week

private IAppData $appData;

Expand All @@ -34,6 +34,7 @@ public function __construct(
parent::__construct($timeFactory);
$this->appData = $appDataFactory->get('core');
$this->setInterval(60 * 60 * 24);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/private/User/BackgroundJobs/CleanupDeletedUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use OC\User\PartiallyDeletedUsersBackend;
use OC\User\User;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
Expand All @@ -26,8 +25,8 @@ public function __construct(
private LoggerInterface $logger,
) {
parent::__construct($time);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setInterval(24 * 3600);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
$this->setInterval(24 * 60 * 60);
}

protected function run($argument): void {
Expand Down
9 changes: 7 additions & 2 deletions lib/public/BackgroundJob/TimedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace OCP\BackgroundJob;

use OCP\ILogger;
use OCP\Server;
use Psr\Log\LoggerInterface;

/**
* Simple base class to extend to run periodic background jobs.
Expand Down Expand Up @@ -52,8 +54,8 @@ public function isTimeSensitive(): bool {
* @since 24.0.0
*/
public function setTimeSensitivity(int $sensitivity): void {
if ($sensitivity !== IJob::TIME_SENSITIVE &&
$sensitivity !== IJob::TIME_INSENSITIVE) {
if ($sensitivity !== self::TIME_SENSITIVE &&
$sensitivity !== self::TIME_INSENSITIVE) {
throw new \InvalidArgumentException('Invalid sensitivity');
}

Expand All @@ -80,6 +82,9 @@ final public function execute(IJobList $jobList, ?ILogger $logger = null) {
*/
final public function start(IJobList $jobList): void {
if (($this->time->getTime() - $this->lastRun) > $this->interval) {
if ($this->interval >= 12 * 60 * 60 && $this->isTimeSensitive()) {
Server::get(LoggerInterface::class)->debug('TimedJob ' . get_class($this) . ' has a configured interval of ' . $this->interval . ' seconds, but is also marked as time sensitive. Please consider marking it as time insensitive to allow more sensitive jobs to run when needed.');
}
parent::start($jobList);
}
}
Expand Down

0 comments on commit 03f8e90

Please sign in to comment.