Debian Codegraph @

cylc/flow/task_action_timer.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """Timer for task actions."""
18
19 from time import time
20
21 from cylc.flow.wallclock import (
22 get_seconds_as_interval_string, get_time_string_from_unix_time)
23
24
25 class TimerFlags:
26
27 EXECUTION_RETRY = 'execution-retry'
28 SUBMISSION_RETRY = 'submission-retry'
29
30
31 class TaskActionTimer:
32 """A timer with delays for task actions."""
33
34 # Memory optimization - constrain possible attributes to this list.
35 __slots__ = ["ctx", "delays", "num", "delay", "timeout", "is_waiting"]
36
37 def __init__(self, ctx=None, delays=None, num=0, delay=None, timeout=None):
38 self.ctx = ctx
39 self.delays = None
40 self.set_delays(delays)
41 self.num = int(num)
42 if delay is not None:
43 delay = float(delay)
44 self.delay = delay
45 if timeout is not None:
46 timeout = float(timeout)
47 self.timeout = timeout
48 self.is_waiting = False
49
50 def delay_timeout_as_str(self):
51 """Return a string in the form "delay (after timeout)"."""
52 return r"%s (after %s)" % (
53 get_seconds_as_interval_string(self.delay),
54 get_time_string_from_unix_time(self.timeout))
55
56 def is_delay_done(self, now=None):
57 """Is timeout done?"""
58 if self.timeout is None:
59 return False
60 if now is None:
61 now = time()
62 return now > self.timeout
63
64 def is_timeout_set(self):
65 """Return True if timeout is set."""
66 return self.timeout is not None
67
68 def next(self, no_exhaust=False): # noqa: A003 (method name not local)
69 """Return the next retry delay.
70
71 When delay list has no more item:
72 * Return None if no_exhaust is False
73 * Return the final delay if no_exhaust is True.
74 """
75 try:
76 self.delay = self.delays[self.num]
77 except IndexError:
78 if not no_exhaust:
79 self.delay = None
80 if self.delay is not None:
81 self.timeout = time() + self.delay
82 self.num += 1
83 return self.delay
84
85 def reset(self):
86 """Reset num, delay, timeout and is_waiting."""
87 self.num = 0
88 self.delay = None
89 self.timeout = None
90 self.is_waiting = False
91
92 def set_delays(self, delays=None):
93 """Set delays, ensuring that the values are floats."""
94 if delays is None:
95 self.delays = [float(0)]
96 else:
97 self.delays = [float(delay) for delay in delays]
98
99 def set_waiting(self):
100 """Set waiting flag, while waiting for action to complete."""
101 self.delay = None
102 self.is_waiting = True
103 self.timeout = None
104
105 def unset_waiting(self):
106 """Unset waiting flag after an action has completed."""
107 self.is_waiting = False