summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/test/qa-tests/buildscripts/resmokelib/utils/timer.py
blob: 80531d5db5c457f9e06364193b71b5e2c370fb80 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Alternative to the threading.Timer class.

Enables a timer to be restarted without needing to construct a new thread
each time. This is necessary to execute periodic actions, e.g. flushing
log messages to buildlogger, while avoiding errors related to "can't start
new thread" that would otherwise occur on Windows.
"""

from __future__ import absolute_import

import threading


class AlarmClock(threading.Thread):
    """
    Calls a function after a specified number of seconds.
    """

    def __init__(self, interval, func, args=None, kwargs=None):
        """
        Initializes the timer with a function to periodically execute.
        """

        threading.Thread.__init__(self)

        # A non-dismissed timer should not prevent the program from exiting
        self.daemon = True

        self.interval = interval
        self.func = func
        self.args = args if args is not None else []
        self.kwargs = kwargs if kwargs is not None else {}

        self.lock = threading.Lock()
        self.cond = threading.Condition(self.lock)

        self.snoozed = False  # canceled for one execution
        self.dismissed = False  # canceled for all time
        self.restarted = False

    def dismiss(self):
        """
        Disables the timer.
        """

        with self.lock:
            self.dismissed = True
            self.cond.notify_all()

        self.join()  # Tidy up the started thread.

    cancel = dismiss  # Expose API compatible with that of threading.Timer.

    def snooze(self):
        """
        Skips the next execution of 'func' if it has not already started.
        """

        with self.lock:
            if self.dismissed:
                raise ValueError("Timer cannot be snoozed if it has been dismissed")

            self.snoozed = True
            self.restarted = False
            self.cond.notify_all()

    def reset(self):
        """
        Restarts the timer, causing it to wait 'interval' seconds before calling
        'func' again.
        """

        with self.lock:
            if self.dismissed:
                raise ValueError("Timer cannot be reset if it has been dismissed")

            if not self.snoozed:
                raise ValueError("Timer cannot be reset if it has not been snoozed")

            self.restarted = True
            self.cond.notify_all()

    def run(self):
        """
        Repeatedly calls 'func' with a delay of 'interval' seconds between executions.

        If the timer is snoozed before 'func' is called, then it waits to be reset.
        After it has been reset, the timer will again wait 'interval' seconds and
        then try to call 'func'.

        If the timer is dismissed, then no subsequent executions of 'func' are made.
        """

        while True:
            with self.lock:
                if self.dismissed:
                    return

                # Wait for the specified amount of time.
                self.cond.wait(self.interval)

                if self.dismissed:
                    return

                # If the timer was snoozed, then it should wait to be reset.
                if self.snoozed:
                    while not self.restarted:
                        self.cond.wait()

                        if self.dismissed:
                            return

                    self.restarted = False
                    self.snoozed = False
                    continue

            # Execute the function after the lock has been released to prevent potential deadlocks
            # with the invoked function.
            self.func(*self.args, **self.kwargs)

            # Reacquire the lock.
            with self.lock:
                # Ignore snoozes that took place while the function was being executed.
                self.snoozed = False