Skip to content
Closed
Prev Previous commit
Next Next commit
run background process when executed from command line
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Oct 18, 2023
commit 3c4905acb88df19f1b0f07069e2110979bb04b5f
12 changes: 8 additions & 4 deletions apps/files/lib/Command/Scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@
use OC\DB\Connection;
use OC\DB\ConnectionAdapter;
use OC\FilesMetadata\FilesMetadataManager;
use OC\ForbiddenException;
use OC\Metadata\MetadataManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\FileCacheUpdated;
use OCP\Files\Events\NodeAddedToCache;
use OCP\Files\Events\NodeRemovedFromCache;
use OCP\Files\File;
use OC\ForbiddenException;
use OC\Metadata\MetadataManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\StorageNotAvailableException;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -143,7 +144,10 @@ protected function scanFiles(string $user, string $path, bool $scanMetadata, Out
$this->metadataManager->generateMetadata($node, false);
}

$this->filesMetadataManager->refreshMetadata($node);
$this->filesMetadataManager->refreshMetadata(
$node,
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
);
}
});

Expand Down
13 changes: 6 additions & 7 deletions core/Command/FilesMetadata/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OC\DB\ConnectionAdapter;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\FilesMetadata\IFilesMetadataManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -68,12 +69,6 @@ protected function configure() {
'',
InputOption::VALUE_NONE,
'refresh metadata from scratch'
)
->addOption(
'background',
'',
InputOption::VALUE_NONE,
'emulate background jobs when refreshing metadata'
);
}

Expand All @@ -82,9 +77,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($input->getOption('refresh')) {
$node = $this->rootFolder->getUserFolder($input->getArgument('userId'))->getById($fileId);
$file = $node[0];
if (null === $file) {
throw new NotFoundException();
}

$metadata = $this->filesMetadataManager->refreshMetadata(
$file,
$input->getOption('background'),
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND,
$input->getOption('reset')
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Cache/QuerySearchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function equipQueryForMetadata(CacheQueryBuilder $query, ISearchQuery
// TODO: use $searchQuery to improve the query

// init the thing
$metadataQuery = $this->filesMetadataManager->getMetadataQuery($query, 'fc', 'fileid');
$metadataQuery = $this->filesMetadataManager->getMetadataQuery($query, 'file', 'fileid');

// get metadata aside the files
$metadataQuery->retrieveMetadata();
Expand Down
16 changes: 11 additions & 5 deletions lib/private/FilesMetadata/FilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function getMetadata(int $fileId): IFilesMetadata {

public function refreshMetadata(
Node $node,
bool $asBackgroundJob = false,
bool $fromScratch = false
int $process = self::PROCESS_LIVE,
bool $fromScratch = false,
): IFilesMetadata {
$metadata = null;
if (!$fromScratch) {
Expand All @@ -65,17 +65,23 @@ public function refreshMetadata(
$metadata = new FilesMetadata($node->getId(), true);
}

if ($asBackgroundJob) {
$event = new MetadataBackgroundEvent($node, $metadata);
} else {
// is $process is LIVE, we enforce LIVE
if ((self::PROCESS_LIVE & $process) !== 0) {
$event = new MetadataLiveEvent($node, $metadata);
} else {
$event = new MetadataBackgroundEvent($node, $metadata);
}

$this->eventDispatcher->dispatchTyped($event);
$this->saveMetadata($event->getMetadata());

// if requested, we add a new job for next cron to refresh metadata out of main thread
// if $process was set to LIVE+BACKGROUND, we run background process directly
if ($event instanceof MetadataLiveEvent && $event->isRunAsBackgroundJobRequested()) {
if ((self::PROCESS_BACKGROUND & $process) !== 0) {
return $this->refreshMetadata($node, self::PROCESS_BACKGROUND);
}

$this->jobList->add(UpdateSingleMetadata::class, [$node->getOwner()->getUID(), $node->getId()]);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/FilesMetadata/Job/UpdateSingleMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\BackgroundJob\QueuedJob;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\FilesMetadata\IFilesMetadataManager;

class UpdateSingleMetadata extends QueuedJob {
public function __construct(
Expand All @@ -50,7 +51,7 @@ protected function run($argument) {
$node = $this->rootFolder->getUserFolder($userId)->getById($fileId);
if (count($node) > 0) {
$file = array_shift($node);
$this->filesMetadataManager->refreshMetadata($file, true);
$this->filesMetadataManager->refreshMetadata($file, IFilesMetadataManager::PROCESS_BACKGROUND);
}
} catch (NotPermittedException |NoUserException $e) {
}
Expand Down
5 changes: 4 additions & 1 deletion lib/public/FilesMetadata/IFilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
use OCP\FilesMetadata\Model\IMetadataQuery;

interface IFilesMetadataManager {
public const PROCESS_LIVE = 1;
public const PROCESS_BACKGROUND = 2;

public function refreshMetadata(
Node $node,
bool $asBackgroundJob = false,
int $process = self::PROCESS_LIVE,
bool $fromScratch = false
): IFilesMetadata;

Expand Down