Skip to content

Commit 12058fe

Browse files
authored
Merge pull request #149 from nextcloud/fix/db-stress
fix: reduce db and cron load
2 parents edf0abf + 327771e commit 12058fe

2 files changed

Lines changed: 29 additions & 35 deletions

File tree

lib/BackgroundJobs/FileSystemListenerJob.php

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
use OCP\App\IAppManager;
1818
use OCP\AppFramework\Utility\ITimeFactory;
1919
use OCP\BackgroundJob\IJobList;
20-
use OCP\BackgroundJob\QueuedJob;
20+
use OCP\BackgroundJob\TimedJob;
2121
use OCP\Files\IRootFolder;
2222

23-
class FileSystemListenerJob extends QueuedJob {
23+
class FileSystemListenerJob extends TimedJob {
2424
private const BATCH_SIZE = 500;
2525

2626
public function __construct(
@@ -34,6 +34,8 @@ public function __construct(
3434
private IRootFolder $rootFolder,
3535
) {
3636
parent::__construct($timeFactory);
37+
$this->allowParallelRuns = false;
38+
$this->setInterval(5 * 60); // 5 minutes
3739
}
3840

3941
protected function run($argument): void {
@@ -50,40 +52,37 @@ protected function run($argument): void {
5052
return;
5153
}
5254

53-
try {
54-
foreach ($fsEvents as $fsEvent) {
55-
$this->diagnosticService->sendHeartbeat(static::class, $this->getId());
55+
foreach ($fsEvents as $fsEvent) {
56+
$this->diagnosticService->sendHeartbeat(static::class, $this->getId());
5657

57-
try {
58-
$node = current($this->rootFolder->getUserFolder($fsEvent->getUserId())->getById($fsEvent->getNodeId()));
59-
if ($node === false) {
60-
$this->logger->warning('Node with ID ' . $fsEvent->getNodeId() . ' not found for fs event "' . $fsEvent->getType() . '"');
61-
$this->fsEventMapper->delete($fsEvent);
62-
continue;
63-
}
58+
try {
59+
$node = current($this->rootFolder->getUserFolder($fsEvent->getUserId())->getById($fsEvent->getNodeId()));
60+
} catch (\Exception $e) {
61+
$this->logger->warning('Error retrieving node for fs event "' . $fsEvent->getType() . '": ' . $e->getMessage(), ['exception' => $e]);
62+
$node = false;
63+
}
64+
if ($node === false) {
65+
$this->logger->warning('Node with ID ' . $fsEvent->getNodeId() . ' not found for fs event "' . $fsEvent->getType() . '"');
66+
$this->fsEventMapper->delete($fsEvent);
67+
continue;
68+
}
6469

65-
switch ($fsEvent->getTypeObject()) {
66-
case FsEventType::CREATE:
67-
$this->fsEventService->onInsert($node);
68-
break;
69-
case FsEventType::ACCESS_UPDATE_DECL:
70-
$this->fsEventService->onAccessUpdateDecl($node);
71-
break;
72-
}
73-
$this->diagnosticService->sendHeartbeat(static::class, $this->getId());
74-
$this->fsEventMapper->delete($fsEvent);
75-
} catch (\RuntimeException $e) {
76-
$this->logger->warning('Error handling fs event "' . $fsEvent->getType() . '": ' . $e->getMessage(), ['exception' => $e]);
70+
try {
71+
switch ($fsEvent->getTypeObject()) {
72+
case FsEventType::CREATE:
73+
$this->fsEventService->onInsert($node);
74+
break;
75+
case FsEventType::ACCESS_UPDATE_DECL:
76+
$this->fsEventService->onAccessUpdateDecl($node);
77+
break;
7778
}
79+
$this->diagnosticService->sendHeartbeat(static::class, $this->getId());
80+
$this->fsEventMapper->delete($fsEvent);
81+
} catch (\RuntimeException $e) {
82+
$this->logger->warning('Error handling fs event "' . $fsEvent->getType() . '": ' . $e->getMessage(), ['exception' => $e]);
7883
}
79-
} catch (\Throwable $e) {
80-
// schedule in 5mins
81-
$this->jobList->scheduleAfter(static::class, $this->time->getTime() + 5 * 60);
82-
throw $e;
8384
}
8485

85-
// schedule in 5mins
86-
$this->jobList->scheduleAfter(static::class, $this->time->getTime() + 5 * 60);
8786
$this->diagnosticService->sendJobEnd(static::class, $this->getId());
8887
}
8988
}

lib/Service/FsEventScheduler.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace OCA\ContextChat\Service;
99

10-
use OCA\ContextChat\BackgroundJobs\FileSystemListenerJob;
1110
use OCA\ContextChat\Db\FsEvent;
1211
use OCA\ContextChat\Db\FsEventMapper;
1312
use OCA\ContextChat\Type\FsEventType;
@@ -52,10 +51,6 @@ private function scheduleEvent(FsEventType $type, string $userId, int $nodeId):
5251

5352
// do not catch DB exceptions
5453
$this->fsEventMapper->insert($item);
55-
56-
if (!$this->jobList->has(FileSystemListenerJob::class, null)) {
57-
$this->jobList->add(FileSystemListenerJob::class, null);
58-
}
5954
}
6055

6156
/**

0 commit comments

Comments
 (0)