Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable22] Make background job time insensitive #730

Merged
merged 1 commit into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions lib/BackgroundJob/ExpireActivities.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand All @@ -22,34 +24,31 @@

namespace OCA\Activity\BackgroundJob;

use OC\BackgroundJob\TimedJob;
use OCA\Activity\Data;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;

/**
* Class ExpireActivities
*
* @package OCA\Activity\BackgroundJob
*/
class ExpireActivities extends TimedJob {
/** @var Data */
protected $data;
/** @var IConfig */
protected $config;

/**
* @param Data $data
* @param IConfig $config
*/
public function __construct(Data $data, IConfig $config) {
public function __construct(ITimeFactory $time,
Data $data,
IConfig $config) {
parent::__construct($time);

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

$this->data = $data;
$this->config = $config;
}

protected function run($argument) {
protected function run($argument): void {
// Remove activities that are older then one year
$expireDays = $this->config->getSystemValue('activity_expire_days', 365);
$this->data->expire($expireDays);
Expand Down
4 changes: 2 additions & 2 deletions tests/AppInfo/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function queryData(): array {
[EmailNotification::class],
[EmailNotification::class, TimedJob::class],
[ExpireActivities::class,],
[ExpireActivities::class, TimedJob::class],
[ExpireActivities::class, \OCP\BackgroundJob\TimedJob::class],

// Controller
[ActivitiesController::class],
Expand All @@ -131,6 +131,6 @@ public function testContainerQuery(string $service, ?string $expected = null): v
if ($expected === null) {
$expected = $service;
}
$this->assertInstanceOf($expected, $this->container->query($service));
$this->assertInstanceOf($expected, $this->container->get($service));
}
}
2 changes: 2 additions & 0 deletions tests/BackgroundJob/ExpireActivitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCA\Activity\BackgroundJob\ExpireActivities;
use OCA\Activity\Data;
use OCA\Activity\Tests\TestCase;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\BackgroundJob\IJobList;

Expand All @@ -40,6 +41,7 @@
class ExpireActivitiesTest extends TestCase {
public function testExecute(): void {
$backgroundJob = new ExpireActivities(
$this->createMock(ITimeFactory::class),
$this->createMock(Data::class),
$this->createMock(IConfig::class)
);
Expand Down
6 changes: 6 additions & 0 deletions tests/DataDeleteActivitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\Activity\BackgroundJob\ExpireActivities;
use OCA\Activity\Data;
use OCP\Activity\IExtension;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\IPreparedStatement;
use OCP\IConfig;
use OCP\IUserSession;
Expand Down Expand Up @@ -107,10 +108,15 @@ public function testDeleteActivities(array $condition, array $expected): void {

public function testExpireActivities(): void {
$config = $this->createMock(IConfig::class);
$time = $this->createMock(ITimeFactory::class);
$time->method('getTime')
->willReturn(time());
$backgroundjob = new ExpireActivities(
$time,
$this->data,
$config
);
$backgroundjob->setId(1);
$this->assertUserActivities(['delete', 'otherUser']);
$jobList = $this->createMock(IJobList::class);
$backgroundjob->execute($jobList);
Expand Down