1+ """
2+ Administrative utilities for managing database connections and passwords.
3+
4+ This module provides functions for viewing and terminating database connections
5+ through the MySQL processlist interface, as well as password management.
6+ """
7+
8+ from __future__ import annotations
9+
110import logging
211from getpass import getpass
312
413import pymysql
514from packaging import version
615
7- from .connection import conn
16+ from .connection import Connection , conn
817from .settings import config
918from .utils import user_choice
1019
1120logger = logging .getLogger (__name__ .split ("." )[0 ])
1221
1322
14- def set_password (new_password = None , connection = None , update_config = None ):
23+ def set_password (
24+ new_password : str | None = None ,
25+ connection : Connection | None = None ,
26+ update_config : bool | None = None ,
27+ ) -> None :
28+ """
29+ Change the database password for the current user.
30+
31+ Args:
32+ new_password: The new password. If None, prompts for input.
33+ connection: A datajoint.Connection object. If None, uses datajoint.conn().
34+ update_config: If True, save the new password to local config.
35+ If None, prompts the user.
36+ """
1537 connection = conn () if connection is None else connection
1638 if new_password is None :
1739 new_password = getpass ("New password: " )
@@ -36,20 +58,27 @@ def set_password(new_password=None, connection=None, update_config=None):
3658 config .save_local (verbose = True )
3759
3860
39- def kill (restriction = None , connection = None , order_by = None ):
61+ def kill (
62+ restriction : str | None = None ,
63+ connection : Connection | None = None ,
64+ order_by : str | list [str ] | None = None ,
65+ ) -> None :
4066 """
41- view and kill database connections.
67+ View and interactively kill database connections.
4268
43- :param restriction: restriction to be applied to processlist
44- :param connection: a datajoint.Connection object. Default calls datajoint.conn()
45- :param order_by: order by a single attribute or the list of attributes. defaults to 'id'.
69+ Displays active database connections matching the optional restriction and
70+ prompts the user to select connections to terminate.
4671
47- Restrictions are specified as strings and can involve any of the attributes of
48- information_schema.processlist: ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
72+ Args:
73+ restriction: SQL WHERE clause condition to filter the processlist.
74+ Can reference any column from information_schema.processlist:
75+ ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
76+ connection: A datajoint.Connection object. If None, uses datajoint.conn().
77+ order_by: Column name(s) to sort results by. Defaults to 'id'.
4978
5079 Examples:
51- dj.kill('HOST LIKE "%compute%"') lists only connections from hosts containing "compute".
52- dj.kill('TIME > 600') lists only connections in their current state for more than 10 minutes
80+ >>> dj.kill('HOST LIKE "%compute%"') # connections from hosts containing "compute"
81+ >>> dj.kill('TIME > 600') # connections idle for more than 10 minutes
5382 """
5483
5584 if connection is None :
@@ -95,18 +124,28 @@ def kill(restriction=None, connection=None, order_by=None):
95124 logger .warn ("Process not found" )
96125
97126
98- def kill_quick (restriction = None , connection = None ):
127+ def kill_quick (
128+ restriction : str | None = None ,
129+ connection : Connection | None = None ,
130+ ) -> int :
99131 """
100- Kill database connections without prompting. Returns number of terminated connections.
132+ Kill database connections without prompting.
133+
134+ Terminates all database connections matching the optional restriction
135+ without user confirmation.
101136
102- :param restriction: restriction to be applied to processlist
103- :param connection: a datajoint.Connection object. Default calls datajoint.conn()
137+ Args:
138+ restriction: SQL WHERE clause condition to filter the processlist.
139+ Can reference any column from information_schema.processlist:
140+ ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
141+ connection: A datajoint.Connection object. If None, uses datajoint.conn().
104142
105- Restrictions are specified as strings and can involve any of the attributes of
106- information_schema.processlist: ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO .
143+ Returns:
144+ Number of connections terminated .
107145
108146 Examples:
109- dj.kill('HOST LIKE "%compute%"') terminates connections from hosts containing "compute".
147+ >>> dj.kill_quick('HOST LIKE "%compute%"') # kill connections from "compute" hosts
148+ >>> dj.kill_quick('TIME > 600') # kill connections idle for more than 10 minutes
110149 """
111150 if connection is None :
112151 connection = conn ()
0 commit comments