Merge pull request #46185 from nextcloud/debt/noid/migrate-background-commands-to-iappconfig
refactor: simplify background commandspull/46235/head
commit
0f95c6e471
@ -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';
|
||||
}
|
||||
}
|
||||
@ -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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue