Example of using the database mounting tool

 1#  Copyright (c) 2024. Harvard University
 2#
 3#  Developed by Research Software Engineering,
 4#  Harvard University Research Computing and Data (RCD) Services.
 5#
 6#  Author: Michael A Bouzinier
 7#
 8#  Licensed under the Apache License, Version 2.0 (the "License");
 9#  you may not use this file except in compliance with the License.
10#  You may obtain a copy of the License at
11#
12#           http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing, software
15#  distributed under the License is distributed on an "AS IS" BASIS,
16#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17#  See the License for the specific language governing permissions and
18#  limitations under the License.
19#
20import logging
21import sys
22
23from psycopg2.extensions import connection
24
25from dorieh.platform.db import Connection
26from dorieh.platform.loader.monitor import DBActivityMonitor
27
28
29def sleep(cnxn: connection, seconds: int = 100):
30    """
31    Runs a dummy process in a database for specified number of seconds using
32    connection provided
33
34    :param cnxn: Connection object, connected to PostgreSQL database
35    :param seconds: number of seconds for the process to run
36    :return:
37    """
38
39    with (cnxn.cursor()) as cursor:
40        cursor.execute(f"SELECT pg_sleep({seconds})")
41
42
43def get_pid(cnxn: connection) -> int:
44    """
45    Obtains the process id for a given connection
46
47    :param cnxn: Connection object, connected to PostgreSQL database
48    :return:  process id (it is both a unix prcoess id and PostgreSQL process id)
49    """
50
51    with cnxn.cursor() as cursor:
52        cursor.execute("SELECT pg_backend_pid()")
53        for row in cursor:
54            return row[0]
55
56
57def execute_with_monitor ():
58    monitor = DBActivityMonitor()
59    db_ini = monitor.context.db
60    connection_name = monitor.context.connection
61    with Connection(db_ini, connection_name) as cnxn:
62        pid = get_pid(cnxn)
63        callback = lambda: monitor.log_activity(pid)
64        process = lambda: sleep(cnxn, 100)
65        DBActivityMonitor.execute(process, callback)
66    print("Process finished")
67
68
69if __name__ == '__main__':
70    handler = logging.StreamHandler(sys.stdout)
71    handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s'))
72    logging.basicConfig(level=logging.DEBUG, handlers = [handler])
73    execute_with_monitor()
74

Usage

python longprocess.py --db DB --connection CONNECTION

Where:

  --db DB, --database DB
                        Path to a database connection parameters file,
                        default: database.ini
  --connection CONNECTION, --connection_name CONNECTION
                        Section in the database connection parameters file,
                        default: nsaph2

[see Managing database connections] for details on how to manage and specify connections to PostgreSQL database.