mirror of
https://github.com/netzbegruenung/circles.git
synced 2024-05-04 12:53:41 +02:00
Merge pull request #1006 from nextcloud/enh/276/split-low-and-heavy-maintenance
split heavy and low maintenance process
This commit is contained in:
commit
4f2edc83c8
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
63
lib/Cron/MaintenanceHeavy.php
Normal file
63
lib/Cron/MaintenanceHeavy.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue