Skip to content

Commit

Permalink
feat(bg-jobs): support multiple arguments in cron.php and occ:backgro…
Browse files Browse the repository at this point in the history
…und-job:worker for the job class list

Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
  • Loading branch information
julien-nc committed Apr 22, 2024
1 parent d7071a0 commit f24e031
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
28 changes: 7 additions & 21 deletions core/Command/Background/JobWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected function configure(): void {
->setDescription('Run a background job worker')
->addArgument(
'job-classes',
InputArgument::OPTIONAL,
'The classes of the jobs to look for in the database, comma-separated'
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'The classes of the jobs to look for in the database'
)
->addOption(
'once',
Expand All @@ -73,25 +73,11 @@ protected function configure(): void {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$jobClassesString = $input->getArgument('job-classes');
// only keep non-empty strings
$jobClasses = $jobClassesString === null
? null
: array_filter(
explode(',', $jobClassesString),
static function (string $jobClass) {
return strlen($jobClass) > 0;
}
);
$jobClasses = $input->getArgument('job-classes');
$jobClasses = empty($jobClasses) ? null : $jobClasses;

if ($jobClasses !== null) {
// no class
if (count($jobClasses) === 0) {
$output->writeln('<error>Invalid job class list supplied</error>');
return 1;
}

// at least one invalid class
// at least one class is invalid
foreach ($jobClasses as $jobClass) {
if (!class_exists($jobClass)) {
$output->writeln('<error>Invalid job class: ' . $jobClass . '</error>');
Expand All @@ -115,10 +101,10 @@ static function (string $jobClass) {
$job = $this->jobList->getNext(false, $jobClasses);
if (!$job) {
if ($input->getOption('once') === true) {
if ($jobClassesString === null) {
if ($jobClasses === null) {
$output->writeln('No job is currently queued', OutputInterface::VERBOSITY_VERBOSE);
} else {
$output->writeln('No job of classes ' . $jobClassesString . ' is currently queued', OutputInterface::VERBOSITY_VERBOSE);
$output->writeln('No job of classes [' . implode(', ', $jobClasses) . '] is currently queued', OutputInterface::VERBOSITY_VERBOSE);
}
$output->writeln('Exiting...', OutputInterface::VERBOSITY_VERBOSE);
break;
Expand Down
18 changes: 6 additions & 12 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@
Run the background job routine
Usage:
php -f cron.php -- [-h] [<job-classes>]
php -f cron.php -- [-h] [<job-classes>...]
Arguments:
job-classes Optional job class comma-separated list to only run those jobs
job-classes Optional job class list to only run those jobs
Options:
-h, --help Display this help message' . PHP_EOL;
Expand Down Expand Up @@ -175,16 +175,10 @@
$endTime = time() + 14 * 60;

$executedJobs = [];
// a specific job class list can optionally be given as first argument
// only keep non-empty strings
$jobClasses = isset($argv[1])
? array_filter(
explode(',', $argv[1]),
static function (string $jobClass) {
return strlen($jobClass) > 0;
}
)
: null;
// a specific job class list can optionally be given as argument
$jobClasses = array_slice($argv, 1);
$jobClasses = empty($jobClasses) ? null : $jobClasses;

while ($job = $jobList->getNext($onlyTimeSensitive, $jobClasses)) {

Check notice

Code scanning / Psalm

ArgumentTypeCoercion Note

Argument 2 of OCP\BackgroundJob\IJobList::getNext expects array<array-key, class-string<OCP\BackgroundJob\IJob>>|null, but parent type non-empty-list|null provided
if (isset($executedJobs[$job->getId()])) {
$jobList->unlockJob($job);
Expand Down

0 comments on commit f24e031

Please sign in to comment.