-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding background:queue commands: status and delete
- Loading branch information
1 parent
d445ab4
commit b57f68b
Showing
8 changed files
with
308 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* @author Thomas Müller <thomas.mueller@tmit.eu> | ||
* | ||
* @copyright Copyright (c) 2018, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OC\Core\Command\Background\Queue; | ||
|
||
use OCP\BackgroundJob\IJobList; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class Delete extends Command { | ||
|
||
/** @var \OCP\BackgroundJob\IJobList */ | ||
private $jobList; | ||
|
||
public function __construct(IJobList $jobList) { | ||
$this->jobList = $jobList; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() { | ||
$this | ||
->setName('background:queue:delete') | ||
->setDescription('Delete a job from the queue') | ||
->addArgument('id', InputArgument::REQUIRED, 'id of the job to be deleted'); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @return int | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$id = $input->getArgument('id'); | ||
|
||
$job = $this->jobList->getById($id); | ||
if ($job === null) { | ||
$output->writeln("Job with id <$id> is not known."); | ||
return 1; | ||
} | ||
|
||
$this->jobList->removeById($id); | ||
$output->writeln('Job has been deleted.'); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* @author Thomas Müller <thomas.mueller@tmit.eu> | ||
* | ||
* @copyright Copyright (c) 2018, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OC\Core\Command\Background\Queue; | ||
|
||
use OCP\BackgroundJob\IJob; | ||
use OCP\BackgroundJob\IJobList; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class Status extends Command { | ||
|
||
/** @var \OCP\BackgroundJob\IJobList */ | ||
private $jobList; | ||
|
||
public function __construct(IJobList $jobList) { | ||
$this->jobList = $jobList; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() { | ||
$this | ||
->setName('background:queue:status') | ||
->setDescription('List queue status'); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @return void | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$t = new Table($output); | ||
$t->setHeaders(['Id', 'Job', 'Last run', 'Job Arguments']); | ||
$this->jobList->listJobs(function (IJob $job) use ($t) { | ||
$t->addRow([$job->getId(), \get_class($job), \date('c', $job->getLastRun()), $job->getArgument()]); | ||
}); | ||
$t->render(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* @author Thomas Müller <thomas.mueller@tmit.eu> | ||
* | ||
* @copyright Copyright (c) 2017, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace Tests\Core\Command\Background\Queue; | ||
|
||
use OC\Core\Command\Background\Queue\Delete; | ||
use OCP\BackgroundJob\IJobList; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Test\TestCase; | ||
|
||
/** | ||
* Class DeleteTest | ||
* | ||
* @group DB | ||
*/ | ||
class DeleteTest extends TestCase { | ||
|
||
/** @var CommandTester */ | ||
private $commandTester; | ||
/** @var IJobList */ | ||
private $jobList; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
|
||
$this->jobList = $this->createMock(IJobList::class); | ||
$this->jobList->expects($this->any())->method('getById') | ||
->willReturnCallback(function ($id) { | ||
return ($id !== '666') ? true : null; | ||
}); | ||
|
||
$command = new Delete($this->jobList); | ||
$this->commandTester = new CommandTester($command); | ||
} | ||
|
||
/** | ||
* @dataProvider providesJobIds | ||
* @param $jobId | ||
* @param $expectedOutput | ||
*/ | ||
public function testCommandInput($jobId, $expectedOutput) { | ||
$input = ['id' => $jobId]; | ||
$this->commandTester->execute($input); | ||
$output = $this->commandTester->getDisplay(); | ||
$this->assertContains($expectedOutput, $output); | ||
} | ||
|
||
public function providesJobIds() { | ||
return [ | ||
['666', 'Job with id <666> is not known.'], | ||
['1', 'Job has been deleted.'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* @author Thomas Müller <thomas.mueller@tmit.eu> | ||
* | ||
* @copyright Copyright (c) 2017, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace Tests\Core\Command\Background\Queue; | ||
|
||
use OC\BackgroundJob\Legacy\RegularJob; | ||
use OC\Core\Command\Background\Queue\Status; | ||
use OCP\BackgroundJob\IJobList; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Test\TestCase; | ||
|
||
/** | ||
* Class StatusTest | ||
* | ||
* @group DB | ||
*/ | ||
class StatusTest extends TestCase { | ||
|
||
/** @var CommandTester */ | ||
private $commandTester; | ||
/** @var IJobList */ | ||
private $jobList; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
|
||
$this->jobList = $this->createMock(IJobList::class); | ||
$this->jobList->expects($this->any())->method('listJobs') | ||
->willReturnCallback(function (\Closure $callBack) { | ||
$job = new RegularJob(); | ||
$job->setId(666); | ||
$callBack($job); | ||
}); | ||
|
||
$command = new Status($this->jobList); | ||
$command->setApplication(new Application()); | ||
$this->commandTester = new CommandTester($command); | ||
} | ||
|
||
public function testCommandInput() { | ||
$this->commandTester->execute([]); | ||
$output = $this->commandTester->getDisplay(); | ||
$expected = <<<EOS | ||
+-----+------------------------------------+---------------------------+---------------+ | ||
| Id | Job | Last run | Job Arguments | | ||
+-----+------------------------------------+---------------------------+---------------+ | ||
| 666 | OC\BackgroundJob\Legacy\RegularJob | 1970-01-01T00:00:00+00:00 | | | ||
+-----+------------------------------------+---------------------------+---------------+ | ||
EOS; | ||
|
||
$this->assertContains($expected, $output); | ||
} | ||
} |