~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python
8687.15.4 by Karl Fogel
Add the copyright header block to more files; tweak format in a few files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
5
6
"""Kill <IDLE> in transaction connections that have hung around for too long.
7
"""
8
9
__metaclass__ = type
10
__all__ = []
11
12
13
from optparse import OptionParser
14
import os
15
import signal
16
import sys
17
14612.2.6 by William Grant
utilities
18
import psycopg2
19
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
20
21
def main():
22
    parser = OptionParser()
23
    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
24
        '-c', '--connection', type='string', dest='connect_string',
25
        default='', help="Psycopg connection string",
26
        )
27
    parser.add_option(
28
        '-s', '--max-idle-seconds', type='int',
29
        dest='max_idle_seconds', default=10*60,
30
        help='Maximum seconds time idle but open transactions are allowed',
31
        )
32
    parser.add_option(
33
        '-q', '--quiet', action='store_true', dest="quiet",
34
        default=False, help='Silence output',
35
        )
36
    parser.add_option(
37
        '-n', '--dry-run', action='store_true', default=False,
38
        dest='dryrun', help="Dry run - don't kill anything",
39
        )
40
    parser.add_option(
41
        '-i', '--ignore', action='append', dest='ignore',
42
        help='Ignore connections by USER', metavar='USER')
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
43
    options, args = parser.parse_args()
44
    if len(args) > 0:
45
        parser.error('Too many arguments')
46
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
47
    ignore_sql = ' AND usename <> %s' * len(options.ignore or [])
48
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
49
    con = psycopg2.connect(options.connect_string)
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
50
    cur = con.cursor()
51
    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
52
        SELECT usename, procpid, backend_start, query_start
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
53
        FROM pg_stat_activity
54
        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
55
            AND query_start < CURRENT_TIMESTAMP - '%d seconds'::interval %s
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
56
        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
57
        """ % (options.max_idle_seconds, ignore_sql), options.ignore)
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
58
59
    rows = cur.fetchall()
60
61
    if len(rows) == 0:
62
        if not options.quiet:
63
            print 'No IDLE transactions to kill'
64
            return 0
65
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
66
    for usename, procpid, backend_start, query_start in rows:
7178.5.2 by Stuart Bishop
Output username too, as per existing production version
67
        print 'Killing %s(%d), %s, %s' % (
68
            usename, procpid, backend_start, query_start,
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
69
            )
70
        if not options.dryrun:
71
            os.kill(procpid, signal.SIGTERM)
72
    return 0
73
74
75
if __name__ == '__main__':
76
    sys.exit(main())