Skip to content

Commit

Permalink
Merge pull request #46185 from nextcloud/debt/noid/migrate-background…
Browse files Browse the repository at this point in the history
…-commands-to-iappconfig

refactor: simplify background commands
  • Loading branch information
kesselb authored Jul 2, 2024
2 parents 164f4a3 + a773a8b commit 0f95c6e
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 134 deletions.
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',
};

$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 @@ -1081,15 +1081,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 @@ -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',
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'],
];
}
}

0 comments on commit 0f95c6e

Please sign in to comment.