-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add background:execute occ command for running cron jobs manually #31617
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29ff58a
Add background:execute occ command for running cron jobs manually
0d526bd
Add background:queue:execute warning and accept-warning option
phil-davis b07bce8
Add unit test for background:queue:execute
phil-davis bcc3b19
Adjust tests, use timeFactory and add conditional logging
jvillafanez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,118 @@ | ||
<?php | ||
/** | ||
* @author Tom Needham <[email protected]> | ||
* | ||
* @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 OC\BackgroundJob\TimedJob; | ||
use OC\Log\CommandLogger; | ||
use OCP\BackgroundJob\IJobList; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\QuestionHelper; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
|
||
class Execute extends Command { | ||
|
||
/** | ||
* @var IJobList | ||
*/ | ||
protected $jobList; | ||
/** @var ITimeFactory */ | ||
protected $timeFactory; | ||
|
||
/** | ||
* @param IJobList $jobList | ||
*/ | ||
public function __construct(IJobList $jobList, ITimeFactory $timeFactory) { | ||
parent::__construct(); | ||
$this->jobList = $jobList; | ||
$this->timeFactory = $timeFactory; | ||
} | ||
|
||
protected function configure() { | ||
$this | ||
->setName('background:queue:execute') | ||
->setDescription("Run a single background job from the queue") | ||
->addArgument('Job ID', InputArgument::REQUIRED, 'ID of the job to run') | ||
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force run the job even if within timing interval') | ||
->addOption('accept-warning', null, InputOption::VALUE_NONE, 'No warning about the usage of this command will be displayed'); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
if (!$input->getOption('accept-warning')) { | ||
$helper = new QuestionHelper(); | ||
$q = <<<EOS | ||
<question>This command is for maintenance and support purposes. | ||
This will run the specified background job now. Regular scheduled runs of the job will | ||
continue to happen at their scheduled times. | ||
If you still want to use this command please confirm the usage by entering: yes | ||
</question> | ||
EOS; | ||
if (!$helper->ask($input, $output, new ConfirmationQuestion($q, false))) { | ||
return 1; | ||
} | ||
} | ||
|
||
// Get the job to run | ||
$jobId = $input->getArgument('Job ID'); | ||
// Try to find the job | ||
$job = $this->jobList->getById($jobId); | ||
if ($job === null) { | ||
$output->writeln("<error>Job not found</error>"); | ||
return 1; | ||
} | ||
|
||
$jobClass = \get_class($job); | ||
$output->writeln("<info>Found job: $jobClass with ID $jobId</info>"); | ||
|
||
$start = $this->timeFactory->getTime(); | ||
|
||
// Run the job if not reserved | ||
$logger = new \OC\Log(new CommandLogger($output), \OC::$server->getSystemConfig()); | ||
|
||
if ($job instanceof TimedJob && $input->getOption('force')) { | ||
// Force the execution to ignore the interval | ||
$output->writeln('<info>Forcing run, resetting last run value to 0</info>'); | ||
$job->setLastRun(0); | ||
} | ||
|
||
$output->writeln('<info>Running job...</info>'); | ||
|
||
$job->execute($this->jobList, $logger); | ||
|
||
$duration = $this->timeFactory->getTime() - $start; | ||
|
||
$output->writeln("<info>Finished in $duration seconds</info>"); | ||
$this->jobList->setLastJob($job); | ||
$this->jobList->setExecutionTime($job, $duration); | ||
|
||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either inject a
ITimeFactory
here or rephrase the log message in order not to use thetime()
function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this can't be changed properly without breaking things. We could make several changes around the job system, but it will also need to touch the actual jobs, which are spread around different apps.