Skip to content

Commit

Permalink
Handle more elegantly when external model service is not available.
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasdelellis committed Jan 10, 2024
1 parent dd4d95a commit 3cafd14
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/BackgroundJob/Tasks/CheckRequirementsTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
use OCA\FaceRecognition\Model\IModel;
use OCA\FaceRecognition\Model\ModelManager;

use OCA\FaceRecognition\Model\Exceptions\UnavailableException;

use OCA\FaceRecognition\Service\SettingsService;

/**
Expand Down Expand Up @@ -152,7 +154,13 @@ public function execute(FaceRecognitionContext $context) {
return false;
}

$model->open();
try {
$model->open();
} catch (UnavailableException $e) {
$this->logInfo("Error accessing the model to get required information: " . $e->getMessage());
return false;
}

$maxImageArea = $model->getMaximumArea();
if ($imageArea > $maxImageArea) {
$error_message =
Expand Down
2 changes: 2 additions & 0 deletions lib/BackgroundJob/Tasks/ImageProcessingTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public function execute(FaceRecognitionContext $context) {
}
$this->logInfo('Faces found: 0. Image will be skipped because of the following error: ' . $e->getMessage());
$this->logDebug((string) $e);

// Save an empty entry so it can be analyzed again later
$this->imageMapper->imageProcessed($image, array(), 0, $e);
} finally {
// Clean temporary image.
Expand Down
8 changes: 8 additions & 0 deletions lib/Model/Exceptions/UnavailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace OCA\FaceRecognition\Model\Exceptions;

class UnavailableException extends \Exception
{
}
24 changes: 20 additions & 4 deletions lib/Model/ExternalModel/ExternalModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

use OCA\FaceRecognition\Model\IModel;

use OCA\FaceRecognition\Model\Exceptions\UnavailableException;


class ExternalModel implements IModel {
/*
* Model description
Expand Down Expand Up @@ -138,12 +141,12 @@ public function open() {

$response = curl_exec($ch);
if (is_bool($response)) {
throw new \Exception('Cannot connect to external model: ' . curl_error($ch));
throw new UnavailableException(curl_error($ch));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
throw new \Exception('Can\'t connect with external model. HTTP status code: ' . $httpCode);
throw new \Exception('External model response /open with error. HTTP status code: ' . $httpCode);
}

$jsonResponse = json_decode($response, true);
Expand Down Expand Up @@ -171,13 +174,21 @@ public function detectFaces(string $imagePath, bool $compute = true): array {

$response = curl_exec($ch);
if (is_bool($response)) {
throw new \Exception('External model dont response: ' . curl_error($ch));
throw new UnavailableException(curl_error($ch));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
throw new \Exception('External model response /detect with error. HTTP status code: ' . $httpCode);
}

curl_close($ch);

$jsonResponse = json_decode($response, true);

if (!is_array($jsonResponse))
return [];

if ($jsonResponse['faces-count'] == 0)
return [];

Expand All @@ -204,7 +215,12 @@ public function compute(string $imagePath, array $face): array {

$response = curl_exec($ch);
if (is_bool($response)) {
throw new \Exception('External model dont response: ' . curl_error($ch));
throw new UnavailableException(curl_error($ch));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
throw new \Exception('External model response /compute with error. HTTP status code: ' . $httpCode);
}

curl_close($ch);
Expand Down

0 comments on commit 3cafd14

Please sign in to comment.