Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
"OCA\\Recognize\\": "lib/"
}
},
"require-dev": {
"nextcloud/ocp": "dev-master",
"symfony/console": "^6.4",
"symfony/process": "^6.4"
},
"scripts": {
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
"cs:check": "php-cs-fixer fix --dry-run --diff",
Expand Down
14 changes: 6 additions & 8 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCA\Recognize\AppInfo;

use OCA\DAV\Connector\Sabre\Principal;
use OCA\DAV\Events\SabrePluginAddEvent;
use OCA\Recognize\Dav\Faces\PropFindPlugin;
use OCA\Recognize\Hooks\FileListener;
use OCP\AppFramework\App;
Expand All @@ -22,7 +23,6 @@
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\NodeRemovedFromCache;
use OCP\SabrePluginEvent;

final class Application extends App implements IBootstrap {
public const APP_ID = 'recognize';
Expand Down Expand Up @@ -60,15 +60,13 @@ public function register(IRegistrationContext $context): void {
*/
public function boot(IBootContext $context): void {
$eventDispatcher = \OCP\Server::get(IEventDispatcher::class);
$eventDispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event): void {
$eventDispatcher->addListener(SabrePluginAddEvent::class, function (SabrePluginAddEvent $event): void {
$server = $event->getServer();

if ($server !== null) {
// We have to register the PropFindPlugin here and not info.xml,
// because info.xml plugins are loaded, after the
// beforeMethod:* hook has already been emitted.
$server->addPlugin($this->getContainer()->get(PropFindPlugin::class));
}
// We have to register the PropFindPlugin here and not info.xml,
// because info.xml plugins are loaded, after the
// beforeMethod:* hook has already been emitted.
$server->addPlugin($this->getContainer()->get(PropFindPlugin::class));
});
}
}
18 changes: 6 additions & 12 deletions lib/BackgroundJobs/ClassifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
declare(strict_types=1);
namespace OCA\Recognize\BackgroundJobs;

use OCA\Recognize\Db\QueueFile;
use OCA\Recognize\Service\QueueService;
use OCA\Recognize\Service\SettingsService;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -33,16 +34,16 @@ public function __construct(
$this->setAllowParallelRuns($settingsService->getSetting('concurrency.enabled') === 'true');
}

/**
* @param array{storageId: int, rootId: int} $argument
*/
protected function runClassifier(string $model, array $argument): void {
sleep(10);
if ($this->settingsService->getSetting('concurrency.enabled') !== 'true' && $this->anyOtherClassifierJobsRunning()) {
$this->logger->debug('Stalling job '.static::class.' with argument ' . var_export($argument, true) . ' because other classifiers are already reserved');
return;
}

/**
* @var int $storageId
*/
$storageId = $argument['storageId'];
$rootId = $argument['rootId'];
if ($this->settingsService->getSetting($model.'.enabled') !== 'true') {
Expand Down Expand Up @@ -100,22 +101,15 @@ protected function runClassifier(string $model, array $argument): void {
}
}

/**
* @return int
*/
abstract protected function getBatchSize(): int;

/**
* @param list<OCA\Recognize\Db\QueueFile> $files
* @return void
* @param list<QueueFile> $files
* @throws \RuntimeException|\ErrorException
*/
abstract protected function classify(array $files) : void;

/**
* @return bool
*/
private function anyOtherClassifierJobsRunning() {
private function anyOtherClassifierJobsRunning(): bool {
foreach ([
ClassifyFacesJob::class,
ClassifyImagenetJob::class,
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/ClassifyFacesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(ITimeFactory $time, Logger $logger, QueueService $qu
}

/**
* @inheritDoc
* @param array{storageId: int, rootId: int} $argument
*/
protected function run($argument): void {
$this->runClassifier(self::MODEL_NAME, $argument);
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/ClassifyImagenetJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(ITimeFactory $time, Logger $logger, QueueService $qu
}

/**
* @inheritDoc
* @param array{storageId: int, rootId: int} $argument
*/
protected function run($argument): void {
$this->runClassifier(self::MODEL_NAME, $argument);
Expand Down
5 changes: 3 additions & 2 deletions lib/BackgroundJobs/ClassifyLandmarksJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCA\Recognize\BackgroundJobs;

use OCA\Recognize\Classifiers\Images\LandmarksClassifier;
use OCA\Recognize\Db\QueueFile;
use OCA\Recognize\Service\Logger;
use OCA\Recognize\Service\QueueService;
use OCA\Recognize\Service\SettingsService;
Expand All @@ -28,14 +29,14 @@ public function __construct(ITimeFactory $time, Logger $logger, QueueService $qu
}

/**
* @inheritDoc
* @param array{storageId: int, rootId: int} $argument
*/
protected function run($argument): void {
$this->runClassifier(self::MODEL_NAME, $argument);
}

/**
* @param list<\OCA\Recognize\Db\QueueFile> $files
* @param list<QueueFile> $files
* @return void
*/
protected function classify(array $files) : void {
Expand Down
4 changes: 2 additions & 2 deletions lib/BackgroundJobs/ClassifyMovinetJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function __construct(ITimeFactory $time, Logger $logger, QueueService $qu
}

/**
* @inheritDoc
* @param array{storageId: int, rootId: int} $argument
*/
protected function run($argument):void {
protected function run($argument): void {
$this->runClassifier(self::MODEL_NAME, $argument);
}

Expand Down
6 changes: 2 additions & 4 deletions lib/BackgroundJobs/ClassifyMusicnnJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ public function __construct(ITimeFactory $time, Logger $logger, QueueService $qu
}

/**
* @inheritDoc
*
* @return void
* @param array{storageId: int, rootId: int} $argument
*/
protected function run($argument) {
protected function run($argument): void {
$this->runClassifier(self::MODEL_NAME, $argument);
}

Expand Down
8 changes: 3 additions & 5 deletions lib/BackgroundJobs/ClusterFacesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ public function __construct(ITimeFactory $time, Logger $logger, IJobList $jobLis
}

/**
* @inheritDoc
*
* @return void
* @param array{storageId: int, rootId: int, userId: string} $argument
*/
protected function run($argument) {
$userId = (string) $argument['userId'];
protected function run($argument): void {
$userId = $argument['userId'];
try {
$this->clusterAnalyzer->calculateClusters($userId, self::BATCH_SIZE);
} catch (\Throwable $e) {
Expand Down
5 changes: 2 additions & 3 deletions lib/BackgroundJobs/MaintenanceJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public function __construct(
}

/**
* @param mixed $argument
* @return void
* @param array{storageId: int, rootId: int} $argument
*/
protected function run($argument) {
protected function run($argument): void {
// Trigger clustering in case it's stuck
try {
$users = $this->faceDetectionMapper->getUsersForUnclustered();
Expand Down
3 changes: 3 additions & 0 deletions lib/BackgroundJobs/SchedulerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function __construct(ITimeFactory $timeFactory, Logger $logger, IJobList
$this->storageService = $storageService;
}

/**
* @param array $argument
*/
protected function run($argument): void {
/** @var list<string> $models */
$models = $argument['models'] ?? [
Expand Down
5 changes: 3 additions & 2 deletions lib/BackgroundJobs/StorageCrawlJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function __construct(ITimeFactory $timeFactory, Logger $logger, QueueServ

/**
* @param array{storage_id:int, root_id:int, override_root:int, last_file_id:int, models?: list<string>} $argument
* @return void
*/
protected function run($argument): void {
$storageId = $argument['storage_id'];
Expand All @@ -63,6 +62,8 @@ protected function run($argument): void {
// Remove current iteration
$this->jobList->remove(self::class, $argument);

/** @var ?QueueFile $queueFile */
$queueFile = null;
$i = 0;
foreach ($this->storageService->getFilesInMount($storageId, $overrideRoot, $models, $lastFileId, self::BATCH_SIZE) as $file) {
$i++;
Expand Down Expand Up @@ -102,7 +103,7 @@ protected function run($argument): void {
}
}

if ($i > 0) {
if ($queueFile) {
// Schedule next iteration
$this->jobList->add(self::class, [
'storage_id' => $storageId,
Expand Down
7 changes: 2 additions & 5 deletions lib/Classifiers/Audio/MusicnnClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\Files\IRootFolder;
use OCP\IPreview;
use OCP\ITempManager;
use Override;

final class MusicnnClassifier extends Classifier {
public const AUDIO_TIMEOUT = 40; // seconds
Expand All @@ -28,11 +29,7 @@ public function __construct(Logger $logger, IAppConfig $config, TagManager $tagM
$this->tagManager = $tagManager;
}

/**
* @param \OCA\Recognize\Db\QueueFile[] $queueFiles
* @return void
* @throws \ErrorException|\RuntimeException
*/
#[Override]
public function classify(array $queueFiles): void {
if ($this->config->getAppValueString('tensorflow.purejs', 'false') === 'true') {
$timeout = self::AUDIO_PUREJS_TIMEOUT;
Expand Down
5 changes: 2 additions & 3 deletions lib/Classifiers/Classifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,14 @@ public function setMaxExecutionTime(int $time): void {
}

/**
* @param QueueFile[] $queueFiles
* @return void
* @param list<QueueFile> $queueFiles
* @throws \ErrorException|\RuntimeException
*/
abstract public function classify(array $queueFiles): void;

/**
* @param string $model
* @param QueueFile[] $queueFiles
* @param list<QueueFile> $queueFiles
* @param int $timeout
* @return \Generator
* @psalm-return \Generator<QueueFile, mixed, mixed, null>
Expand Down
8 changes: 2 additions & 6 deletions lib/Classifiers/Images/ClusteringFaceClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCP\IPreview;
use OCP\ITempManager;
use OCP\Share\IManager;
use Override;

final class ClusteringFaceClassifier extends Classifier {
public const IMAGE_TIMEOUT = 120; // seconds
Expand Down Expand Up @@ -61,12 +62,7 @@ private function getUsersWithFileAccess(Node $node): array {
return array_values(array_unique($userIds));
}

/**
* @param string $user
* @param \OCA\Recognize\Db\QueueFile[] $queueFiles
* @return void
* @throws \ErrorException
*/
#[Override]
public function classify(array $queueFiles): void {
if ($this->config->getAppValueString('tensorflow.purejs', 'false') === 'true') {
$timeout = self::IMAGE_PUREJS_TIMEOUT;
Expand Down
7 changes: 2 additions & 5 deletions lib/Classifiers/Images/ImagenetClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\Files\IRootFolder;
use OCP\IPreview;
use OCP\ITempManager;
use Override;

final class ImagenetClassifier extends Classifier {
public const IMAGE_TIMEOUT = 480; // seconds
Expand All @@ -30,11 +31,7 @@ public function __construct(Logger $logger, IAppConfig $config, TagManager $tagM
$this->queue = $queue;
}

/**
* @param \OCA\Recognize\Db\QueueFile[] $queueFiles
* @return void
* @throws \ErrorException|\RuntimeException
*/
#[Override]
public function classify(array $queueFiles): void {
if ($this->config->getAppValueString('tensorflow.purejs', 'false') === 'true') {
$timeout = self::IMAGE_PUREJS_TIMEOUT;
Expand Down
10 changes: 4 additions & 6 deletions lib/Classifiers/Images/LandmarksClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
namespace OCA\Recognize\Classifiers\Images;

use OCA\Recognize\Classifiers\Classifier;
use OCA\Recognize\Db\QueueFile;
use OCA\Recognize\Service\Logger;
use OCA\Recognize\Service\QueueService;
use OCA\Recognize\Service\TagManager;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Files\IRootFolder;
use OCP\IPreview;
use OCP\ITempManager;
use Override;

final class LandmarksClassifier extends Classifier {
public const IMAGE_TIMEOUT = 480; // seconds
Expand All @@ -29,11 +31,7 @@ public function __construct(Logger $logger, IAppConfig $config, TagManager $tagM
$this->tagManager = $tagManager;
}

/**
* @param \OCA\Recognize\Db\QueueFile[] $queueFiles
* @return void
* @throws \ErrorException|\RuntimeException
*/
#[Override]
public function classify(array $queueFiles): void {
if ($this->config->getAppValueString('tensorflow.purejs', 'false') === 'true') {
$timeout = self::IMAGE_PUREJS_TIMEOUT;
Expand All @@ -42,7 +40,7 @@ public function classify(array $queueFiles): void {
}
$classifierProcess = $this->classifyFiles(self::MODEL_NAME, $queueFiles, $timeout);

/** @var \OCA\Recognize\Db\QueueFile $queueFile */
/** @var QueueFile $queueFile */
/** @var list<string> $results */
foreach ($classifierProcess as $queueFile => $results) {
$this->tagManager->assignTags($queueFile->getFileId(), $results);
Expand Down
7 changes: 2 additions & 5 deletions lib/Classifiers/Video/MovinetClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCP\Files\IRootFolder;
use OCP\IPreview;
use OCP\ITempManager;
use Override;

final class MovinetClassifier extends Classifier {
public const VIDEO_TIMEOUT = 480; // seconds
Expand All @@ -28,11 +29,7 @@ public function __construct(Logger $logger, IAppConfig $config, TagManager $tagM
$this->tagManager = $tagManager;
}

/**
* @param \OCA\Recognize\Db\QueueFile[] $queueFiles
* @return void
* @throws \ErrorException|\RuntimeException|Exception
*/
#[Override]
public function classify(array $queueFiles): void {
if ($this->config->getAppValueString('tensorflow.purejs', 'false') === 'true') {
throw new Exception('Movinet does not support WASM mode');
Expand Down
Loading
Loading