summaryrefslogtreecommitdiff
path: root/python/pgq/ticker.py
blob: e623b9cb2653453d19f21b88b09549cab9737f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""PgQ ticker.

It will also launch maintenance job.
"""

import time, threading
import skytools

from pgq.maint import MaintenanceJob

__all__ = ['SmallTicker']

class SmallTicker(skytools.DBScript):
    """Ticker that periodically calls pgq.ticker()."""
    tick_count = 0
    maint_thread = None

    def __init__(self, args):
        skytools.DBScript.__init__(self, 'pgqadm', args)

        self.ticker_log_time = 0
        self.ticker_log_delay = 5*60

    def reload(self):
        skytools.DBScript.reload(self)
        self.ticker_log_delay = self.cf.getfloat("ticker_log_delay", 5*60)

    def startup(self):
        if self.maint_thread:
            return

        # launch maint thread
        self.maint_thread = MaintenanceJob(self, [self.cf.filename])
        t = threading.Thread(name = 'maint_thread',
                             target = self.maint_thread.run)
        t.setDaemon(1)
        t.start()

    def work(self):
        db = self.get_database("db", autocommit = 1)
        cx = db.cursor()

        # run ticker
        cx.execute("select pgq.ticker()")
        self.tick_count += cx.fetchone()[0]

        cur_time = time.time()
        if cur_time > self.ticker_log_time + self.ticker_log_delay:
            self.ticker_log_time = cur_time
            self.stat_increase('ticks', self.tick_count)
            self.tick_count = 0