Merge pull request #46185 from nextcloud/debt/noid/migrate-background-commands-to-iappconfig

refactor: simplify background commands
pull/46235/head
Daniel 2024-07-02 11:21:55 +07:00 committed by GitHub
commit 0f95c6e471
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 108 additions and 134 deletions

@ -1,12 +0,0 @@
<?php
/**
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace OC\Core\Command\Background;
class Ajax extends Base {
protected function getMode(): string {
return 'ajax';
}
}

@ -1,49 +0,0 @@
<?php
/**
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace OC\Core\Command\Background;
use OCP\IAppConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* An abstract base class for configuring the background job mode
* from the command line interface.
* Subclasses will override the getMode() function to specify the mode to configure.
*/
abstract class Base extends Command {
abstract protected function getMode(): string;
public function __construct(
protected IAppConfig $config,
) {
parent::__construct();
}
protected function configure(): void {
$mode = $this->getMode();
$this->setName("background:$mode")
->setDescription("Use $mode to run background jobs");
}
/**
* Executing this command will set the background job mode for owncloud.
* The mode to set is specified by the concrete sub class by implementing the
* getMode() function.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$mode = $this->getMode();
$this->config->setValueString('core', 'backgroundjobs_mode', $mode);
$output->writeln("Set mode for background jobs to '$mode'");
return 0;
}
}

@ -1,12 +0,0 @@
<?php
/**
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace OC\Core\Command\Background;
class Cron extends Base {
protected function getMode(): string {
return 'cron';
}
}

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace OC\Core\Command\Background;
use OCP\IAppConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Mode extends Command {
public function __construct(
private IAppConfig $appConfig,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('background:cron')
->setAliases(['background:ajax', 'background:webcron'])
->setDescription('Use cron, ajax or webcron to run background jobs');
}
protected function execute(InputInterface $input, OutputInterface $output): int {
/** @var 'background:cron'|'background:ajax'|'background:webcron' $command */
$command = $input->getArgument('command');
$mode = match ($command) {
'background:cron' => 'cron',
'background:ajax' => 'ajax',
'background:webcron' => 'webcron',
};
$this->appConfig->setValueString('core', 'backgroundjobs_mode', $mode);
$output->writeln("Set mode for background jobs to '" . $mode . "'");
return 0;
}
}

@ -1,12 +0,0 @@
<?php
/**
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace OC\Core\Command\Background;
class WebCron extends Base {
protected function getMode(): string {
return 'webcron';
}
}

@ -38,9 +38,7 @@ if ($config->getSystemValueBool('installed', false)) {
$application->add(Server::get(Command\TwoFactorAuth\Disable::class));
$application->add(Server::get(Command\TwoFactorAuth\State::class));
$application->add(Server::get(Command\Background\Cron::class));
$application->add(Server::get(Command\Background\WebCron::class));
$application->add(Server::get(Command\Background\Ajax::class));
$application->add(Server::get(Command\Background\Mode::class));
$application->add(Server::get(Command\Background\Job::class));
$application->add(Server::get(Command\Background\ListCommand::class));
$application->add(Server::get(Command\Background\Delete::class));

@ -1081,15 +1081,12 @@ return array(
'OC\\Core\\Command\\App\\ListApps' => $baseDir . '/core/Command/App/ListApps.php',
'OC\\Core\\Command\\App\\Remove' => $baseDir . '/core/Command/App/Remove.php',
'OC\\Core\\Command\\App\\Update' => $baseDir . '/core/Command/App/Update.php',
'OC\\Core\\Command\\Background\\Ajax' => $baseDir . '/core/Command/Background/Ajax.php',
'OC\\Core\\Command\\Background\\Base' => $baseDir . '/core/Command/Background/Base.php',
'OC\\Core\\Command\\Background\\Cron' => $baseDir . '/core/Command/Background/Cron.php',
'OC\\Core\\Command\\Background\\Delete' => $baseDir . '/core/Command/Background/Delete.php',
'OC\\Core\\Command\\Background\\Job' => $baseDir . '/core/Command/Background/Job.php',
'OC\\Core\\Command\\Background\\JobBase' => $baseDir . '/core/Command/Background/JobBase.php',
'OC\\Core\\Command\\Background\\JobWorker' => $baseDir . '/core/Command/Background/JobWorker.php',
'OC\\Core\\Command\\Background\\ListCommand' => $baseDir . '/core/Command/Background/ListCommand.php',
'OC\\Core\\Command\\Background\\WebCron' => $baseDir . '/core/Command/Background/WebCron.php',
'OC\\Core\\Command\\Background\\Mode' => $baseDir . '/core/Command/Background/Mode.php',
'OC\\Core\\Command\\Base' => $baseDir . '/core/Command/Base.php',
'OC\\Core\\Command\\Broadcast\\Test' => $baseDir . '/core/Command/Broadcast/Test.php',
'OC\\Core\\Command\\Check' => $baseDir . '/core/Command/Check.php',

@ -1114,15 +1114,12 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\App\\ListApps' => __DIR__ . '/../../..' . '/core/Command/App/ListApps.php',
'OC\\Core\\Command\\App\\Remove' => __DIR__ . '/../../..' . '/core/Command/App/Remove.php',
'OC\\Core\\Command\\App\\Update' => __DIR__ . '/../../..' . '/core/Command/App/Update.php',
'OC\\Core\\Command\\Background\\Ajax' => __DIR__ . '/../../..' . '/core/Command/Background/Ajax.php',
'OC\\Core\\Command\\Background\\Base' => __DIR__ . '/../../..' . '/core/Command/Background/Base.php',
'OC\\Core\\Command\\Background\\Cron' => __DIR__ . '/../../..' . '/core/Command/Background/Cron.php',
'OC\\Core\\Command\\Background\\Delete' => __DIR__ . '/../../..' . '/core/Command/Background/Delete.php',
'OC\\Core\\Command\\Background\\Job' => __DIR__ . '/../../..' . '/core/Command/Background/Job.php',
'OC\\Core\\Command\\Background\\JobBase' => __DIR__ . '/../../..' . '/core/Command/Background/JobBase.php',
'OC\\Core\\Command\\Background\\JobWorker' => __DIR__ . '/../../..' . '/core/Command/Background/JobWorker.php',
'OC\\Core\\Command\\Background\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/Background/ListCommand.php',
'OC\\Core\\Command\\Background\\WebCron' => __DIR__ . '/../../..' . '/core/Command/Background/WebCron.php',
'OC\\Core\\Command\\Background\\Mode' => __DIR__ . '/../../..' . '/core/Command/Background/Mode.php',
'OC\\Core\\Command\\Base' => __DIR__ . '/../../..' . '/core/Command/Base.php',
'OC\\Core\\Command\\Broadcast\\Test' => __DIR__ . '/../../..' . '/core/Command/Broadcast/Test.php',
'OC\\Core\\Command\\Check' => __DIR__ . '/../../..' . '/core/Command/Check.php',

@ -1,38 +0,0 @@
<?php
/**
* SPDX-FileCopyrightText: 2016-2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace Test\Command;
use OC\Core\Command\Background\Ajax;
use OC\Core\Command\Background\Cron;
use OC\Core\Command\Background\WebCron;
use OCP\IAppConfig;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
use Test\TestCase;
class BackgroundJobsTest extends TestCase {
public function testCronCommand() {
$appConfig = \OCP\Server::get(IAppConfig::class);
$job = new Cron($appConfig);
$job->run(new StringInput(''), new NullOutput());
$this->assertEquals('cron', $appConfig->getValueString('core', 'backgroundjobs_mode'));
}
public function testAjaxCommand() {
$appConfig = \OCP\Server::get(IAppConfig::class);
$job = new Ajax($appConfig);
$job->run(new StringInput(''), new NullOutput());
$this->assertEquals('ajax', $appConfig->getValueString('core', 'backgroundjobs_mode'));
}
public function testWebCronCommand() {
$appConfig = \OCP\Server::get(IAppConfig::class);
$job = new WebCron($appConfig);
$job->run(new StringInput(''), new NullOutput());
$this->assertEquals('webcron', $appConfig->getValueString('core', 'backgroundjobs_mode'));
}
}

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
* SPDX-License-Identifier: MIT
*/
namespace Test\Command;
use OC\Core\Command\Background\Mode;
use OCP\IAppConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;
class BackgroundModeTest extends TestCase {
private IAppConfig $appConfig;
private Mode $command;
public function setUp(): void {
$this->appConfig = $this->createMock(IAppConfig::class);
$inputDefinition = new InputDefinition([
new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
]);
$this->command = new Mode($this->appConfig);
$this->command->setDefinition($inputDefinition);
}
/**
* @dataProvider dataModeCommand
*/
public function testModeCommand(string $mode): void {
$this->appConfig->expects($this->once())
->method('setValueString')
->with('core', 'backgroundjobs_mode', $mode);
$commandTester = new CommandTester($this->command);
$commandTester->execute(['command' => 'background:' . $mode]);
$commandTester->assertCommandIsSuccessful();
$output = $commandTester->getDisplay();
$this->assertStringContainsString($mode, $output);
}
public function dataModeCommand(): array {
return [
'ajax' => ['ajax'],
'cron' => ['cron'],
'webcron' => ['webcron'],
];
}
}