Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
korelstar committed Mar 27, 2022
1 parent d766937 commit bf5cc61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -73,10 +73,13 @@ jobs:
php server/occ files:scan --all
- name: Start Nextcloud server
working-directory: ../server/
run: "php -S localhost:8080 &"
run: "php -S localhost:8080 > data/php.log 2>&1 &"
- name: Test API
run: make test-api
- name: Show nextcloud.log
if: always()
run: "cat ../server/data/nextcloud.log"
- name: Show php.log
if: always()
run: "cat ../server/data/php.log"

26 changes: 23 additions & 3 deletions lib/Service/MetaService.php
Expand Up @@ -75,11 +75,14 @@ public function getAll(string $userId, array $notes, bool $forceUpdate = false)
}
}

$insertErrorCount = 0;
// insert/update changes
foreach ($notes as $id => $note) {
if (!array_key_exists($id, $metas)) {
// INSERT new notes
$meta = $this->createMeta($userId, $note);
$meta = $this->createMeta($userId, $note, function () use (&$insertErrorCount) {
$insertErrorCount++;
});
} else {
// UPDATE changed notes
$meta = $metas[$id];
Expand All @@ -89,6 +92,18 @@ public function getAll(string $userId, array $notes, bool $forceUpdate = false)
}
$result[$id] = new MetaNote($note, $meta);
}
if ($insertErrorCount) {
if ($insertErrorCount == count($notes)) {
$this->util->logger->warning(
'Database failed inserting Meta objects for all '.$insertErrorCount.' notes. '.
'If this happens consistently, there is a problem with your database.',
);
} else {
$this->util->logger->warning(
'Database failed inserting Meta objects for '.$insertErrorCount.' times.',
);
}
}
return $result;
}

Expand Down Expand Up @@ -116,7 +131,7 @@ private function getIndexedArray(array $data, string $property) : array {
return $result;
}

private function createMeta(string $userId, Note $note) : Meta {
private function createMeta(string $userId, Note $note, callable $onError = null) : Meta {
$meta = new Meta();
$meta->setUserId($userId);
$meta->setFileId($note->getId());
Expand All @@ -129,7 +144,12 @@ private function createMeta(string $userId, Note $note) : Meta {
// We can ignore this, since the result should be the same.
// But we log it for being able to detect other problems.
// (If this happens often, this may cause performance problems.)
$this->util->logger->warning(
$loglevel = 'warning';
if ($onError) {
$onError();
$loglevel = 'debug';
}
$this->util->logger->$loglevel(
'Could not insert meta data for note '.$note->getId(),
[ 'exception' => $e ]
);
Expand Down

0 comments on commit bf5cc61

Please sign in to comment.