Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify background commands #46185

Merged
Merged
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
12 changes: 0 additions & 12 deletions core/Command/Background/Ajax.php

This file was deleted.

49 changes: 0 additions & 49 deletions core/Command/Background/Base.php

This file was deleted.

12 changes: 0 additions & 12 deletions core/Command/Background/Cron.php

This file was deleted.

46 changes: 46 additions & 0 deletions core/Command/Background/Mode.php
Original file line number Diff line number Diff line change
@@ -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',
};
susnux marked this conversation as resolved.
Show resolved Hide resolved

$this->appConfig->setValueString('core', 'backgroundjobs_mode', $mode);
$output->writeln("Set mode for background jobs to '" . $mode . "'");

return 0;
}
}
12 changes: 0 additions & 12 deletions core/Command/Background/WebCron.php

This file was deleted.

4 changes: 1 addition & 3 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
$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));
Expand Down
5 changes: 1 addition & 4 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,15 +1079,12 @@
'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',
Expand Down
5 changes: 1 addition & 4 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,15 +1112,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',
Expand Down
38 changes: 0 additions & 38 deletions tests/lib/Command/BackgroundJobsTest.php

This file was deleted.

59 changes: 59 additions & 0 deletions tests/lib/Command/BackgroundModeTest.php
Original file line number Diff line number Diff line change
@@ -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