Skip to content

Commit d75da94

Browse files
committed
split heavy and low maintenance process
Signed-off-by: Maxence Lange <[email protected]>
1 parent bc0484b commit d75da94

File tree

3 files changed

+126
-92
lines changed

3 files changed

+126
-92
lines changed

lib/Cron/Maintenance.php

Lines changed: 11 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -31,114 +31,33 @@
3131

3232
namespace OCA\Circles\Cron;
3333

34-
use OCA\Circles\Tools\Model\SimpleDataStore;
35-
use OC\BackgroundJob\TimedJob;
36-
use OCA\Circles\Exceptions\MaintenanceException;
37-
use OCA\Circles\Service\ConfigService;
3834
use OCA\Circles\Service\MaintenanceService;
35+
use OCP\AppFramework\Utility\ITimeFactory;
36+
use OCP\BackgroundJob\IJob;
37+
use OCP\BackgroundJob\TimedJob;
3938

40-
/**
41-
* Class Maintenance
42-
*
43-
* @package OCA\Cicles\Cron
44-
*/
4539
class Maintenance extends TimedJob {
40+
private MaintenanceService $maintenanceService;
4641

4742

48-
/** @var MaintenanceService */
49-
private $maintenanceService;
50-
51-
/** @var ConfigService */
52-
private $configService;
53-
54-
55-
public static $DELAY =
56-
[
57-
1 => 60, // every minute
58-
2 => 300, // every 5 minutes
59-
3 => 3600, // every hour
60-
4 => 75400, // every day
61-
5 => 432000 // evey week
62-
];
63-
6443
/**
65-
* Cache constructor.
44+
* @param ITimeFactory $time
45+
* @param MaintenanceService $maintenanceService
6646
*/
67-
public function __construct(
68-
MaintenanceService $maintenanceService,
69-
ConfigService $configService
70-
) {
47+
public function __construct(ITimeFactory $time, MaintenanceService $maintenanceService) {
48+
parent::__construct($time);
49+
7150
$this->setInterval(10);
51+
$this->setTimeSensitivity(IJob::TIME_SENSITIVE);
7252

7353
$this->maintenanceService = $maintenanceService;
74-
$this->configService = $configService;
7554
}
7655

7756

7857
/**
7958
* @param $argument
8059
*/
8160
protected function run($argument) {
82-
$this->runMaintenances();
83-
}
84-
85-
86-
/**
87-
*
88-
*/
89-
private function runMaintenances(): void {
90-
$last = new SimpleDataStore();
91-
$last->json($this->configService->getAppValue(ConfigService::MAINTENANCE_UPDATE));
92-
93-
$last->sInt('maximum', $this->maximumLevelBasedOnTime(($last->gInt('5') === 0)));
94-
for ($i = 5; $i > 0; $i--) {
95-
if ($this->canRunLevel($i, $last)) {
96-
try {
97-
$this->maintenanceService->runMaintenance($i);
98-
} catch (MaintenanceException $e) {
99-
continue;
100-
}
101-
$last->sInt((string)$i, time());
102-
}
103-
}
104-
105-
$this->configService->setAppValue(ConfigService::MAINTENANCE_UPDATE, json_encode($last));
106-
}
107-
108-
109-
/**
110-
* @param bool $force
111-
*
112-
* @return int
113-
*/
114-
private function maximumLevelBasedOnTime(bool $force = false): int {
115-
$currentHour = (int)date('H');
116-
$currentDay = (int)date('N');
117-
$isWeekEnd = ($currentDay >= 6);
118-
119-
if ($currentHour > 2 && $currentHour < 5 && ($isWeekEnd || $force)) {
120-
return 5;
121-
}
122-
123-
if ($currentHour > 1 && $currentHour < 6) {
124-
return 4;
125-
}
126-
127-
return 3;
128-
}
129-
130-
131-
private function canRunLevel(int $level, SimpleDataStore $last): bool {
132-
if ($last->gInt('maximum') < $level) {
133-
return false;
134-
}
135-
136-
$now = time();
137-
$timeLastRun = $last->gInt((string)$level);
138-
if ($timeLastRun === 0) {
139-
return true;
140-
}
141-
142-
return ($timeLastRun + self::$DELAY[$level] < $now);
61+
$this->maintenanceService->runMaintenances();
14362
}
14463
}

lib/Cron/MaintenanceHeavy.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
/**
7+
* Circles - Bring cloud-users closer together.
8+
*
9+
* This file is licensed under the Affero General Public License version 3 or
10+
* later. See the COPYING file.
11+
*
12+
* @author Maxence Lange <[email protected]>
13+
* @copyright 2017
14+
* @license GNU AGPL version 3 or any later version
15+
*
16+
* This program is free software: you can redistribute it and/or modify
17+
* it under the terms of the GNU Affero General Public License as
18+
* published by the Free Software Foundation, either version 3 of the
19+
* License, or (at your option) any later version.
20+
*
21+
* This program is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Affero General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Affero General Public License
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28+
*
29+
*/
30+
31+
32+
namespace OCA\Circles\Cron;
33+
34+
use OCA\Circles\Service\MaintenanceService;
35+
use OCP\AppFramework\Utility\ITimeFactory;
36+
use OCP\BackgroundJob\IJob;
37+
use OCP\BackgroundJob\TimedJob;
38+
39+
class MaintenanceHeavy extends TimedJob {
40+
private MaintenanceService $maintenanceService;
41+
42+
43+
/**
44+
* @param ITimeFactory $time
45+
* @param MaintenanceService $maintenanceService
46+
*/
47+
public function __construct(ITimeFactory $time, MaintenanceService $maintenanceService) {
48+
parent::__construct($time);
49+
50+
$this->setInterval(24 * 3600);
51+
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
52+
53+
$this->maintenanceService = $maintenanceService;
54+
}
55+
56+
57+
/**
58+
* @param $argument
59+
*/
60+
protected function run($argument) {
61+
$this->maintenanceService->runMaintenances(true);
62+
}
63+
}

lib/Service/MaintenanceService.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use OCA\Circles\Model\Member;
4343
use OCA\Circles\Model\Probes\CircleProbe;
4444
use OCA\Circles\Model\ShareWrapper;
45+
use OCA\Circles\Tools\Model\SimpleDataStore;
4546
use OCA\Circles\Tools\Traits\TNCLogger;
4647
use OCP\IGroupManager;
4748
use OCP\IUserManager;
@@ -58,6 +59,15 @@ class MaintenanceService {
5859

5960
public const TIMEOUT = 18000;
6061

62+
public static $DELAY =
63+
[
64+
1 => 60, // every minute
65+
2 => 300, // every 5 minutes
66+
3 => 3600, // every hour
67+
4 => 75400, // every day
68+
5 => 432000 // evey week
69+
];
70+
6171

6272
/** @var IUserManager */
6373
private $userManager;
@@ -435,6 +445,48 @@ private function fixSubCirclesDisplayName(): void {
435445
}
436446

437447

448+
/**
449+
* should only be called from a BackgroundJob
450+
*
451+
* @param bool $heavy - set to true to run heavy maintenance process.
452+
*/
453+
public function runMaintenances(bool $heavy = false): void {
454+
$last = new SimpleDataStore();
455+
$last->json($this->configService->getAppValue(ConfigService::MAINTENANCE_UPDATE));
456+
457+
$maxLevel = ($heavy) ? 5 : 3;
458+
for ($i = $maxLevel; $i > 0; $i--) {
459+
if ($this->canRunLevel($i, $last)) {
460+
try {
461+
$this->runMaintenance($i);
462+
} catch (MaintenanceException $e) {
463+
continue;
464+
}
465+
$last->sInt((string)$i, time());
466+
}
467+
}
468+
469+
$this->configService->setAppValue(ConfigService::MAINTENANCE_UPDATE, json_encode($last));
470+
}
471+
472+
473+
/**
474+
* @param int $level
475+
* @param SimpleDataStore $last
476+
*
477+
* @return bool
478+
*/
479+
private function canRunLevel(int $level, SimpleDataStore $last): bool {
480+
$now = time();
481+
$timeLastRun = $last->gInt((string)$level);
482+
if ($timeLastRun === 0) {
483+
return true;
484+
}
485+
486+
return ($timeLastRun + self::$DELAY[$level] < $now);
487+
}
488+
489+
438490
/**
439491
* @param string $message
440492
*/

0 commit comments

Comments
 (0)