~launchpad-pqm/launchpad/devel

3944.1.1 by Francis J. Lacoste
Use system version python2.4 for scripts.
1
#!/usr/bin/python2.4
7178.5.3 by Stuart Bishop
Update copyright
2
# Copyright 2007-2008 Canonical Ltd.  All rights reserved.
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
3
4
"""Kill <IDLE> in transaction connections that have hung around for too long.
5
"""
6
7
__metaclass__ = type
8
__all__ = []
9
10
11
from optparse import OptionParser
12
import os
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
13
import psycopg2
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
14
import signal
15
import sys
16
17
18
def main():
19
    parser = OptionParser()
20
    parser.add_option(
7178.5.1 by Stuart Bishop
Allow pgkillidle.py to ignore connections of particular users to stop replication and other processes requiring long running idle transactions from exploding
21
        '-c', '--connection', type='string', dest='connect_string',
22
        default='', help="Psycopg connection string",
23
        )
24
    parser.add_option(
25
        '-s', '--max-idle-seconds', type='int',
26
        dest='max_idle_seconds', default=10*60,
27
        help='Maximum seconds time idle but open transactions are allowed',
28
        )
29
    parser.add_option(
30
        '-q', '--quiet', action='store_true', dest="quiet",
31
        default=False, help='Silence output',
32
        )
33
    parser.add_option(
34
        '-n', '--dry-run', action='store_true', default=False,
35
        dest='dryrun', help="Dry run - don't kill anything",
36
        )
37
    parser.add_option(
38
        '-i', '--ignore', action='append', dest='ignore',
39
        help='Ignore connections by USER', metavar='USER')
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
40
    options, args = parser.parse_args()
41
    if len(args) > 0:
42
        parser.error('Too many arguments')
43
7178.5.1 by Stuart Bishop
Allow pgkillidle.py to ignore connections of particular users to stop replication and other processes requiring long running idle transactions from exploding
44
    ignore_sql = ' AND usename <> %s' * len(options.ignore or [])
45
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
46
    con = psycopg2.connect(options.connect_string)
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
47
    cur = con.cursor()
48
    cur.execute("""
7178.5.1 by Stuart Bishop
Allow pgkillidle.py to ignore connections of particular users to stop replication and other processes requiring long running idle transactions from exploding
49
        SELECT usename, procpid, backend_start, query_start
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
50
        FROM pg_stat_activity
51
        WHERE current_query = '<IDLE> in transaction'
7178.5.1 by Stuart Bishop
Allow pgkillidle.py to ignore connections of particular users to stop replication and other processes requiring long running idle transactions from exploding
52
            AND query_start < CURRENT_TIMESTAMP - '%d seconds'::interval %s
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
53
        ORDER BY procpid
7178.5.1 by Stuart Bishop
Allow pgkillidle.py to ignore connections of particular users to stop replication and other processes requiring long running idle transactions from exploding
54
        """ % (options.max_idle_seconds, ignore_sql), options.ignore)
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
55
56
    rows = cur.fetchall()
57
58
    if len(rows) == 0:
59
        if not options.quiet:
60
            print 'No IDLE transactions to kill'
61
            return 0
62
7178.5.1 by Stuart Bishop
Allow pgkillidle.py to ignore connections of particular users to stop replication and other processes requiring long running idle transactions from exploding
63
    for usename, procpid, backend_start, query_start in rows:
7178.5.2 by Stuart Bishop
Output username too, as per existing production version
64
        print 'Killing %s(%d), %s, %s' % (
65
            usename, procpid, backend_start, query_start,
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
66
            )
67
        if not options.dryrun:
68
            os.kill(procpid, signal.SIGTERM)
69
    return 0
70
71
72
if __name__ == '__main__':
73
    sys.exit(main())