Skip to content
Closed
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
10 changes: 8 additions & 2 deletions lib/Command/CirclesMaintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

use OC\Core\Command\Base;
use OCA\Circles\Db\CoreRequestBuilder;
use OCA\Circles\Exceptions\MaintenanceException;
use OCA\Circles\Service\MaintenanceService;
use OCA\Circles\Service\OutputService;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -84,7 +85,7 @@ protected function configure() {
parent::configure();
$this->setName('circles:maintenance')
->setDescription('Clean stuff, keeps the app running')
->addOption('level', '', InputOption::VALUE_REQUIRED, 'level of maintenance', '0')
->addOption('level', '', InputOption::VALUE_REQUIRED, 'level of maintenance', '3')
->addOption(
'reset', '', InputOption::VALUE_NONE, 'reset Circles; remove all data related to the App'
)
Expand Down Expand Up @@ -157,7 +158,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->outputService->setOccOutput($output);
$this->maintenanceService->runMaintenance($level);
for ($i = 1; $i <= $level; $i++) {
try {
$this->maintenanceService->runMaintenance($i);
} catch (MaintenanceException $e) {
}
}

$output->writeln('');
$output->writeln('<info>done</info>');
Expand Down
83 changes: 81 additions & 2 deletions lib/Cron/Maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
namespace OCA\Circles\Cron;


use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore;
use OC\BackgroundJob\TimedJob;
use OCA\Circles\Exceptions\MaintenanceException;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\MaintenanceService;


Expand All @@ -45,22 +48,98 @@ class Maintenance extends TimedJob {
/** @var MaintenanceService */
private $maintenanceService;

/** @var ConfigService */
private $configService;


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.
*/
public function __construct(MaintenanceService $maintenanceService) {
public function __construct(
MaintenanceService $maintenanceService,
ConfigService $configService
) {
$this->setInterval(10);

$this->maintenanceService = $maintenanceService;
$this->configService = $configService;
}


/**
* @param $argument
*/
protected function run($argument) {
$this->maintenanceService->runMaintenance(3);
$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);
}

}
Expand Down
1 change: 1 addition & 0 deletions lib/Db/CoreRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class CoreRequestBuilder {
'instance',
'interface',
'severity',
'retry',
'status',
'creation'
],
Expand Down
8 changes: 6 additions & 2 deletions lib/Db/EventWrapperRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function save(EventWrapper $wrapper): void {
->setValue('instance', $qb->createNamedParameter($wrapper->getInstance()))
->setValue('interface', $qb->createNamedParameter($wrapper->getInterface()))
->setValue('severity', $qb->createNamedParameter($wrapper->getSeverity()))
->setValue('retry', $qb->createNamedParameter($wrapper->getRetry()))
->setValue('status', $qb->createNamedParameter($wrapper->getStatus()))
->setValue('creation', $qb->createNamedParameter($wrapper->getCreation()));

Expand All @@ -70,7 +71,8 @@ public function save(EventWrapper $wrapper): void {
public function update(EventWrapper $wrapper): void {
$qb = $this->getEventWrapperUpdateSql();
$qb->set('result', $qb->createNamedParameter(json_encode($wrapper->getResult())))
->set('status', $qb->createNamedParameter($wrapper->getStatus()));
->set('status', $qb->createNamedParameter($wrapper->getStatus()))
->set('retry', $qb->createNamedParameter($wrapper->getRetry()));

$qb->limitToInstance($wrapper->getInstance());
$qb->limitToToken($wrapper->getToken());
Expand Down Expand Up @@ -98,9 +100,11 @@ public function updateAll(string $token, int $status): void {
*
* @return EventWrapper[]
*/
public function getFailedEvents(): array {
public function getFailedEvents(array $retryRange): array {
$qb = $this->getEventWrapperSelectSql();
$qb->limitInt('status', EventWrapper::STATUS_FAILED);
$qb->gt('retry', $retryRange[0], true);
$qb->lt('retry', $retryRange[1]);

return $this->getItemsFromRequest($qb);
}
Expand Down
46 changes: 46 additions & 0 deletions lib/Exceptions/MaintenanceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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 <[email protected]>
* @copyright 2021
* @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\Exceptions;


use Exception;


/**
* Class MaintenanceException
*
* @package OCA\Circles\Exceptions
*/
class MaintenanceException extends Exception {

}


6 changes: 6 additions & 0 deletions lib/Migration/Version0022Date20220526113601.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
'notnull' => false
]
);
$table->addColumn(
'retry', 'integer', [
'length' => 3,
'notnull' => false
]
);
$table->addColumn(
'status', 'integer', [
'length' => 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*
* @package OCA\Circles\Migration
*/
class Version0022Date20220623224231 extends SimpleMigrationStep {
class Version0022Date20220626112233 extends SimpleMigrationStep {


/**
Expand Down Expand Up @@ -75,6 +75,14 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
]
);
}
if (!$table->hasColumn('retry')) {
$table->addColumn(
'retry', 'integer', [
'length' => 3,
'notnull' => false
]
);
}
}

if ($schema->hasTable('circles_member')) {
Expand Down
22 changes: 21 additions & 1 deletion lib/Model/Federated/EventWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore;
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
use JsonSerializable;
use OCA\Circles\Exceptions\UnknownInterfaceException;


/**
Expand Down Expand Up @@ -75,6 +74,9 @@ class EventWrapper implements INC22QueryRow, JsonSerializable {
/** @var int */
private $severity = FederatedEvent::SEVERITY_LOW;

/** @var int */
private $retry = 0;

/** @var int */
private $status = 0;

Expand Down Expand Up @@ -207,6 +209,24 @@ public function setSeverity(int $severity): self {
return $this;
}

/**
* @param int $retry
*
* @return EventWrapper
*/
public function setRetry(int $retry): self {
$this->retry = $retry;

return $this;
}

/**
* @return int
*/
public function getRetry(): int {
return $this->retry;
}


/**
* @return int
Expand Down
4 changes: 4 additions & 0 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class ConfigService {
const MIGRATION_BYPASS = 'migration_bypass';
const MIGRATION_22 = 'migration_22';
const MIGRATION_RUN = 'migration_run';
const MAINTENANCE_UPDATE = 'maintenance_update';
const MAINTENANCE_RUN = 'maintenance_run';

const LOOPBACK_TMP_ID = 'loopback_tmp_id';
const LOOPBACK_TMP_SCHEME = 'loopback_tmp_scheme';
Expand Down Expand Up @@ -151,6 +153,8 @@ class ConfigService {
self::MIGRATION_BYPASS => '0',
self::MIGRATION_22 => '0',
self::MIGRATION_RUN => '0',
self::MAINTENANCE_UPDATE => '[]',
self::MAINTENANCE_RUN => '0',

self::FORCE_NC_BASE => '',
self::TEST_NC_BASE => '',
Expand Down
Loading