mirror of
https://github.com/simple-login/app.git
synced 2024-11-16 08:58:30 +01:00
Fix error in event processing where the event is deleted before marking it as taken (#2280)
Co-authored-by: Carlos Quintana <74399022+cquintana92@users.noreply.github.com>
This commit is contained in:
parent
a585a84302
commit
34575016fd
1 changed files with 13 additions and 9 deletions
|
@ -24,6 +24,7 @@ from sqlalchemy import text, desc, CheckConstraint, Index, Column
|
||||||
from sqlalchemy.dialects.postgresql import TSVECTOR
|
from sqlalchemy.dialects.postgresql import TSVECTOR
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import deferred
|
from sqlalchemy.orm import deferred
|
||||||
|
from sqlalchemy.orm.exc import ObjectDeletedError
|
||||||
from sqlalchemy.sql import and_
|
from sqlalchemy.sql import and_
|
||||||
from sqlalchemy_utils import ArrowType
|
from sqlalchemy_utils import ArrowType
|
||||||
|
|
||||||
|
@ -3781,15 +3782,18 @@ class SyncEvent(Base, ModelMixin):
|
||||||
)
|
)
|
||||||
|
|
||||||
def mark_as_taken(self, allow_taken_older_than: Optional[Arrow] = None) -> bool:
|
def mark_as_taken(self, allow_taken_older_than: Optional[Arrow] = None) -> bool:
|
||||||
taken_condition = ["taken_time IS NULL"]
|
try:
|
||||||
args = {"taken_time": arrow.now().datetime, "sync_event_id": self.id}
|
taken_condition = ["taken_time IS NULL"]
|
||||||
if allow_taken_older_than:
|
args = {"taken_time": arrow.now().datetime, "sync_event_id": self.id}
|
||||||
taken_condition.append("taken_time < :taken_older_than")
|
if allow_taken_older_than:
|
||||||
args["taken_older_than"] = allow_taken_older_than.datetime
|
taken_condition.append("taken_time < :taken_older_than")
|
||||||
sql_taken_condition = "({})".format(" OR ".join(taken_condition))
|
args["taken_older_than"] = allow_taken_older_than.datetime
|
||||||
sql = f"UPDATE sync_event SET taken_time = :taken_time WHERE id = :sync_event_id AND {sql_taken_condition}"
|
sql_taken_condition = "({})".format(" OR ".join(taken_condition))
|
||||||
res = Session.execute(sql, args)
|
sql = f"UPDATE sync_event SET taken_time = :taken_time WHERE id = :sync_event_id AND {sql_taken_condition}"
|
||||||
Session.commit()
|
res = Session.execute(sql, args)
|
||||||
|
Session.commit()
|
||||||
|
except ObjectDeletedError:
|
||||||
|
return False
|
||||||
|
|
||||||
return res.rowcount > 0
|
return res.rowcount > 0
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue