Skip to content

Commit 196be7a

Browse files
authored
fix(airflow_db_cleanup): sessions cleanup (GoogleCloudPlatform#11035)
This fixes an issue where the python operator gets killed if the ssession table is large neough
1 parent 980a984 commit 196be7a

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

composer/workflows/airflow_db_cleanup.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -468,24 +468,13 @@ def cleanup_sessions():
468468

469469
try:
470470
logging.info("Deleting sessions...")
471-
before = len(
472-
session.execute(
473-
text("SELECT * FROM session WHERE expiry < now()::timestamp(0);")
474-
)
475-
.mappings()
476-
.all()
477-
)
471+
count_statement = "SELECT COUNT(*) AS cnt FROM session WHERE expiry < now()::timestamp(0);"
472+
before = session.execute(text(count_statement)).one_or_none()["cnt"]
478473
session.execute(text("DELETE FROM session WHERE expiry < now()::timestamp(0);"))
479-
after = len(
480-
session.execute(
481-
text("SELECT * FROM session WHERE expiry < now()::timestamp(0);")
482-
)
483-
.mappings()
484-
.all()
485-
)
486-
logging.info("Deleted {} expired sessions.".format(before - after))
487-
except Exception as e:
488-
logging.error(e)
474+
after = session.execute(text(count_statement)).one_or_none()["cnt"]
475+
logging.info("Deleted %s expired sessions.", (before - after))
476+
except Exception as err:
477+
logging.exception(err)
489478

490479
session.commit()
491480
session.close()

0 commit comments

Comments
 (0)