Skip to content

Commit c39765d

Browse files
committed
fixup! fix(userstatus): catch unique constrain violation on revert
1 parent b8a4356 commit c39765d

2 files changed

Lines changed: 27 additions & 14 deletions

File tree

apps/dav/lib/CalDAV/Status/StatusService.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\AppFramework\Db\DoesNotExistException;
3333
use OCP\AppFramework\Utility\ITimeFactory;
3434
use OCP\Calendar\IManager;
35+
use OCP\DB\Exception;
3536
use OCP\ICache;
3637
use OCP\ICacheFactory;
3738
use OCP\IUser as User;
@@ -72,7 +73,18 @@ public function processCalendarStatus(string $userId): void {
7273
}
7374

7475
if(empty($calendarEvents)) {
75-
$this->userStatusService->revertUserStatus($userId, IUserStatus::MESSAGE_CALENDAR_BUSY);
76+
try {
77+
$this->userStatusService->revertUserStatus($userId, IUserStatus::MESSAGE_CALENDAR_BUSY);
78+
} catch (Exception $e) {
79+
if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
80+
// A different process might have written another status
81+
// update to the DB while we're processing our stuff.
82+
// We cannot safely restore the status as we don't know which one is valid at this point
83+
// So let's silently log this one and exit
84+
$this->logger->debug('Unique constraint violation for live user status', ['exception' => $e]);
85+
return;
86+
}
87+
}
7688
$this->logger->debug('No calendar events found for status check', ['user' => $userId]);
7789
return;
7890
}
@@ -118,7 +130,18 @@ public function processCalendarStatus(string $userId): void {
118130
});
119131

120132
if(empty($applicableEvents)) {
121-
$this->userStatusService->revertUserStatus($userId, IUserStatus::MESSAGE_CALENDAR_BUSY);
133+
try {
134+
$this->userStatusService->revertUserStatus($userId, IUserStatus::MESSAGE_CALENDAR_BUSY);
135+
} catch (Exception $e) {
136+
if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
137+
// A different process might have written another status
138+
// update to the DB while we're processing our stuff.
139+
// We cannot safely restore the status as we don't know which one is valid at this point
140+
// So let's silently log this one and exit
141+
$this->logger->debug('Unique constraint violation for live user status', ['exception' => $e]);
142+
return;
143+
}
144+
}
122145
$this->logger->debug('No status relevant events found, skipping calendar status change', ['user' => $userId]);
123146
return;
124147
}

apps/user_status/lib/Service/StatusService.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -543,18 +543,8 @@ public function revertUserStatus(string $userId, string $messageId, bool $revert
543543
$backupUserStatus->setIsBackup(false);
544544
// Remove the underscore prefix added when creating the backup
545545
$backupUserStatus->setUserId(substr($backupUserStatus->getUserId(), 1));
546-
try {
547-
$this->mapper->update($backupUserStatus);
548-
} catch (Exception $e) {
549-
if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
550-
// A different process might have written another status
551-
// update to the DB while we're processing our stuff.
552-
// We cannot safely restore the status as we don't know which one is valid at this point
553-
// So let's silently log this one and exit
554-
$this->logger->debug('Unique constraint violation for live user status', ['exception' => $e]);
555-
return null;
556-
}
557-
}
546+
$this->mapper->update($backupUserStatus);
547+
558548
return $backupUserStatus;
559549
}
560550

0 commit comments

Comments
 (0)