~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
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
15
import psycopg2
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
16
import signal
17
import sys
18
19
20
def main():
21
    parser = OptionParser()
22
    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
23
        '-c', '--connection', type='string', dest='connect_string',
24
        default='', help="Psycopg connection string",
25
        )
26
    parser.add_option(
27
        '-s', '--max-idle-seconds', type='int',
28
        dest='max_idle_seconds', default=10*60,
29
        help='Maximum seconds time idle but open transactions are allowed',
30
        )
31
    parser.add_option(
32
        '-q', '--quiet', action='store_true', dest="quiet",
33
        default=False, help='Silence output',
34
        )
35
    parser.add_option(
36
        '-n', '--dry-run', action='store_true', default=False,
37
        dest='dryrun', help="Dry run - don't kill anything",
38
        )
39
    parser.add_option(
40
        '-i', '--ignore', action='append', dest='ignore',
41
        help='Ignore connections by USER', metavar='USER')
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
42
    options, args = parser.parse_args()
43
    if len(args) > 0:
44
        parser.error('Too many arguments')
45
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
46
    ignore_sql = ' AND usename <> %s' * len(options.ignore or [])
47
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
48
    con = psycopg2.connect(options.connect_string)
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
49
    cur = con.cursor()
50
    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
51
        SELECT usename, procpid, backend_start, query_start
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
52
        FROM pg_stat_activity
53
        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
54
            AND query_start < CURRENT_TIMESTAMP - '%d seconds'::interval %s
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
55
        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
56
        """ % (options.max_idle_seconds, ignore_sql), options.ignore)
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
57
58
    rows = cur.fetchall()
59
60
    if len(rows) == 0:
61
        if not options.quiet:
62
            print 'No IDLE transactions to kill'
63
            return 0
64
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
65
    for usename, procpid, backend_start, query_start in rows:
7178.5.2 by Stuart Bishop
Output username too, as per existing production version
66
        print 'Killing %s(%d), %s, %s' % (
67
            usename, procpid, backend_start, query_start,
3929.1.1 by Stuart Bishop
Tool to kill idle transactions
68
            )
69
        if not options.dryrun:
70
            os.kill(procpid, signal.SIGTERM)
71
    return 0
72
73
74
if __name__ == '__main__':
75
    sys.exit(main())