Skip to content

Commit

Permalink
split heavy and low maintenance process
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Apr 14, 2022
1 parent 8589063 commit 9c3e247
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 92 deletions.
103 changes: 11 additions & 92 deletions lib/Cron/Maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,114 +31,33 @@

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;
}


/**
* @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 changes: 63 additions & 0 deletions lib/Cron/MaintenanceHeavy.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
52 changes: 52 additions & 0 deletions lib/Service/MaintenanceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -420,6 +430,48 @@ private function fixSubCirclesDisplayName(): void {
}


/**
* 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
*/
Expand Down

0 comments on commit 9c3e247

Please sign in to comment.