35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
"""Close all active human sessions; waiting sessions and messages are preserved."""
|
|||
|
|
|
||
|
|
import argparse
|
||
|
|
import sqlite3
|
||
|
|
from contextlib import closing
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
|
||
|
|
DEFAULT_DB = Path(__file__).resolve().parents[1] / "history.db"
|
||
|
|
|
||
|
|
|
||
|
|
def close_active_sessions(db_path=None):
|
||
|
|
database = Path(db_path if db_path is not None else DEFAULT_DB).expanduser().resolve()
|
||
|
|
with closing(sqlite3.connect(database.as_uri() + "?mode=rw", uri=True)) as connection:
|
||
|
|
with connection:
|
||
|
|
count = connection.execute(
|
||
|
|
"UPDATE human_sessions SET status='closed' WHERE status='active'"
|
||
|
|
).rowcount
|
||
|
|
return count
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
||
|
|
parser.add_argument("--db", type=Path, default=DEFAULT_DB, help="Existing SQLite database")
|
||
|
|
args = parser.parse_args()
|
||
|
|
try:
|
||
|
|
count = close_active_sessions(args.db)
|
||
|
|
except sqlite3.Error as error:
|
||
|
|
parser.exit(1, f"Database error: {error}\n")
|
||
|
|
print(f"Closed {count} active session(s).")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|