Skip to content

Commit 9073c7a

Browse files
committed
Protect against the current value being inserted in the DB by another process
Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
1 parent 1bc58d7 commit 9073c7a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

fedbadges/rules.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import datanommer.models
1717
from fedora_messaging.api import Message
18+
from sqlalchemy.exc import IntegrityError
1819
from tahrir_api.dbapi import TahrirDatabase
1920

2021
from fedbadges.cached import cache, get_cached_messages_count
@@ -259,8 +260,14 @@ def _get_current_value(self, candidate: str, previous_count_fn, tahrir: TahrirDa
259260
# Found in DB! Add one (the current message)
260261
messages_count += 1
261262
# Store the value in the DB for next time
262-
tahrir.set_current_value(self.badge_id, candidate_email, messages_count)
263-
tahrir.session.commit()
263+
try:
264+
tahrir.set_current_value(self.badge_id, candidate_email, messages_count)
265+
tahrir.session.commit()
266+
except IntegrityError:
267+
# Another process already added the value! (querying datanommer can be long)
268+
tahrir.session.rollback()
269+
# Try again, this time the value should be in the DB already.
270+
return self._get_current_value(candidate, previous_count_fn, tahrir)
264271
return messages_count
265272

266273
def matches(self, msg: Message, tahrir: TahrirDatabase):

0 commit comments

Comments
 (0)