split heavy and low maintenance process

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
This commit is contained in:
Maxence Lange 2022-04-14 14:15:47 -01:00
parent bc0484bc21
commit d75da9435c
3 changed files with 126 additions and 92 deletions

View file

@ -31,47 +31,26 @@ declare(strict_types=1);
namespace OCA\Circles\Cron;
use OCA\Circles\Tools\Model\SimpleDataStore;
use OC\BackgroundJob\TimedJob;
use OCA\Circles\Exceptions\MaintenanceException;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\MaintenanceService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
/**
* Class Maintenance
*
* @package OCA\Cicles\Cron
*/
class Maintenance extends TimedJob {
private MaintenanceService $maintenanceService;
/** @var MaintenanceService */
private $maintenanceService;
/** @var ConfigService */
private $configService;
public static $DELAY =
[
1 => 60, // every minute
2 => 300, // every 5 minutes
3 => 3600, // every hour
4 => 75400, // every day
5 => 432000 // evey week
];
/**
* Cache constructor.
* @param ITimeFactory $time
* @param MaintenanceService $maintenanceService
*/
public function __construct(
MaintenanceService $maintenanceService,
ConfigService $configService
) {
public function __construct(ITimeFactory $time, MaintenanceService $maintenanceService) {
parent::__construct($time);
$this->setInterval(10);
$this->setTimeSensitivity(IJob::TIME_SENSITIVE);
$this->maintenanceService = $maintenanceService;
$this->configService = $configService;
}
@ -79,66 +58,6 @@ class Maintenance extends TimedJob {
* @param $argument
*/
protected function run($argument) {
$this->runMaintenances();
}
/**
*
*/
private function runMaintenances(): void {
$last = new SimpleDataStore();
$last->json($this->configService->getAppValue(ConfigService::MAINTENANCE_UPDATE));
$last->sInt('maximum', $this->maximumLevelBasedOnTime(($last->gInt('5') === 0)));
for ($i = 5; $i > 0; $i--) {
if ($this->canRunLevel($i, $last)) {
try {
$this->maintenanceService->runMaintenance($i);
} catch (MaintenanceException $e) {
continue;
}
$last->sInt((string)$i, time());
}
}
$this->configService->setAppValue(ConfigService::MAINTENANCE_UPDATE, json_encode($last));
}
/**
* @param bool $force
*
* @return int
*/
private function maximumLevelBasedOnTime(bool $force = false): int {
$currentHour = (int)date('H');
$currentDay = (int)date('N');
$isWeekEnd = ($currentDay >= 6);
if ($currentHour > 2 && $currentHour < 5 && ($isWeekEnd || $force)) {
return 5;
}
if ($currentHour > 1 && $currentHour < 6) {
return 4;
}
return 3;
}
private function canRunLevel(int $level, SimpleDataStore $last): bool {
if ($last->gInt('maximum') < $level) {
return false;
}
$now = time();
$timeLastRun = $last->gInt((string)$level);
if ($timeLastRun === 0) {
return true;
}
return ($timeLastRun + self::$DELAY[$level] < $now);
$this->maintenanceService->runMaintenances();
}
}

View file

@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2017
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Circles\Cron;
use OCA\Circles\Service\MaintenanceService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
class MaintenanceHeavy extends TimedJob {
private MaintenanceService $maintenanceService;
/**
* @param ITimeFactory $time
* @param MaintenanceService $maintenanceService
*/
public function __construct(ITimeFactory $time, MaintenanceService $maintenanceService) {
parent::__construct($time);
$this->setInterval(24 * 3600);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->maintenanceService = $maintenanceService;
}
/**
* @param $argument
*/
protected function run($argument) {
$this->maintenanceService->runMaintenances(true);
}
}

View file

@ -42,6 +42,7 @@ use OCA\Circles\Model\Circle;
use OCA\Circles\Model\Member;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Model\ShareWrapper;
use OCA\Circles\Tools\Model\SimpleDataStore;
use OCA\Circles\Tools\Traits\TNCLogger;
use OCP\IGroupManager;
use OCP\IUserManager;
@ -58,6 +59,15 @@ class MaintenanceService {
public const TIMEOUT = 18000;
public static $DELAY =
[
1 => 60, // every minute
2 => 300, // every 5 minutes
3 => 3600, // every hour
4 => 75400, // every day
5 => 432000 // evey week
];
/** @var IUserManager */
private $userManager;
@ -435,6 +445,48 @@ class MaintenanceService {
}
/**
* should only be called from a BackgroundJob
*
* @param bool $heavy - set to true to run heavy maintenance process.
*/
public function runMaintenances(bool $heavy = false): void {
$last = new SimpleDataStore();
$last->json($this->configService->getAppValue(ConfigService::MAINTENANCE_UPDATE));
$maxLevel = ($heavy) ? 5 : 3;
for ($i = $maxLevel; $i > 0; $i--) {
if ($this->canRunLevel($i, $last)) {
try {
$this->runMaintenance($i);
} catch (MaintenanceException $e) {
continue;
}
$last->sInt((string)$i, time());
}
}
$this->configService->setAppValue(ConfigService::MAINTENANCE_UPDATE, json_encode($last));
}
/**
* @param int $level
* @param SimpleDataStore $last
*
* @return bool
*/
private function canRunLevel(int $level, SimpleDataStore $last): bool {
$now = time();
$timeLastRun = $last->gInt((string)$level);
if ($timeLastRun === 0) {
return true;
}
return ($timeLastRun + self::$DELAY[$level] < $now);
}
/**
* @param string $message
*/