~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to daemons/cache-database-replication-lag.py

  • Committer: Stuart Bishop
  • Date: 2010-02-05 12:17:56 UTC
  • mto: (7675.1212.22 pending-db-changes)
  • mto: This revision was merged to the branch mainline in revision 10428.
  • Revision ID: stuart.bishop@canonical.com-20100205121756-olgk91iovhc2hpsy
Cache database replication lag information as querying the live Slony tables can be slow

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.5
 
2
#
 
3
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
4
# GNU Affero General Public License version 3 (see the file LICENSE).
 
5
 
 
6
"""Calculate database replication lag and cache it."""
 
7
 
 
8
__metaclass__ = type
 
9
__all__ = []
 
10
 
 
11
import _pythonpath
 
12
 
 
13
import sys
 
14
import time
 
15
 
 
16
import psycopg2
 
17
 
 
18
from canonical.database.sqlbase import connect, ISOLATION_LEVEL_AUTOCOMMIT
 
19
from canonical.launchpad.scripts import db_options, logger
 
20
from lp.scripts.helpers import LPOptionParser
 
21
 
 
22
 
 
23
def main(args=None):
 
24
    parser = LPOptionParser()
 
25
    db_options(parser)
 
26
    parser.add_option(
 
27
        "-s", "--sleep", dest="sleep", type="int", default=5,
 
28
        metavar="SECS", help="Wait SECS seconds between refreshes.")
 
29
 
 
30
    (options, args) = parser.parse_args(args)
 
31
    if len(args) != 0:
 
32
        parser.error("Too many arguments.")
 
33
 
 
34
    log = logger(options)
 
35
 
 
36
    while True:
 
37
        try:
 
38
            con = connect(user="lagmon", isolation=ISOLATION_LEVEL_AUTOCOMMIT)
 
39
            cur = con.cursor()
 
40
            while True:
 
41
                cur.execute("SELECT update_replication_lag_cache()")
 
42
                if cur.fetchone()[0]:
 
43
                    log.info("Updated.")
 
44
                else:
 
45
                    log.error("update_replication_lag_cache() failed.")
 
46
                time.sleep(options.sleep)
 
47
        except psycopg2.Error, x:
 
48
            log.error("%s. Retrying.", str(x).strip())
 
49
            time.sleep(options.sleep)
 
50
 
 
51
 
 
52
if __name__ == '__main__':
 
53
    sys.exit(main())