Skip to content

Commit

Permalink
Fix error in event processing where the event is deleted before marki…
Browse files Browse the repository at this point in the history
…ng it as taken (#2280)

Co-authored-by: Carlos Quintana <74399022+cquintana92@users.noreply.github.com>
  • Loading branch information
acasajus and cquintana92 authored Oct 21, 2024
1 parent a585a84 commit 3457501
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from sqlalchemy.dialects.postgresql import TSVECTOR
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import deferred
from sqlalchemy.orm.exc import ObjectDeletedError
from sqlalchemy.sql import and_
from sqlalchemy_utils import ArrowType

Expand Down Expand Up @@ -3781,15 +3782,18 @@ class SyncEvent(Base, ModelMixin):
)

def mark_as_taken(self, allow_taken_older_than: Optional[Arrow] = None) -> bool:
taken_condition = ["taken_time IS NULL"]
args = {"taken_time": arrow.now().datetime, "sync_event_id": self.id}
if allow_taken_older_than:
taken_condition.append("taken_time < :taken_older_than")
args["taken_older_than"] = allow_taken_older_than.datetime
sql_taken_condition = "({})".format(" OR ".join(taken_condition))
sql = f"UPDATE sync_event SET taken_time = :taken_time WHERE id = :sync_event_id AND {sql_taken_condition}"
res = Session.execute(sql, args)
Session.commit()
try:
taken_condition = ["taken_time IS NULL"]
args = {"taken_time": arrow.now().datetime, "sync_event_id": self.id}
if allow_taken_older_than:
taken_condition.append("taken_time < :taken_older_than")
args["taken_older_than"] = allow_taken_older_than.datetime
sql_taken_condition = "({})".format(" OR ".join(taken_condition))
sql = f"UPDATE sync_event SET taken_time = :taken_time WHERE id = :sync_event_id AND {sql_taken_condition}"
res = Session.execute(sql, args)
Session.commit()
except ObjectDeletedError:
return False

return res.rowcount > 0

Expand Down

0 comments on commit 3457501

Please sign in to comment.