pymtt
 All Classes Namespaces Files Functions Variables Groups
Watchdog.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from builtins import str
3 #!/usr/bin/env python
4 #
5 # Copyright (c) 2015-2018 Intel, Inc. All rights reserved.
6 # Copyright (c) 2018 Los Alamos National Security, LLC.
7 # All rights reserved.
8 # $COPYRIGHT$
9 #
10 # Additional copyrights may follow
11 #
12 # $HEADER$
13 #
14 
15 import shutil
16 import os
17 from threading import Timer
18 from BaseMTTUtility import *
19 import signal
20 import datetime
21 
22 ## @addtogroup Utilities
23 # @{
24 # @section Watchdog
25 # Generate and exception after a given amount of time
26 # @param timeout Time in seconds before generating exception
27 # @}
29 
30  def __init__(self, timeout=360, testDef=None):
31 
32  BaseMTTUtility.__init__(self)
33  self.options = {}
34  timeout = self.convert_to_timeout(timeout)
35  self.options['timeout'] = (timeout, "Time in seconds before generating exception")
36  self.timer = None
37  self.timeout = timeout
38  self.testDef = testDef
39  self.activated = False
40 
41  def print_name(self):
42  return "Watchdog"
43 
44  def print_options(self, testDef, prefix):
45  lines = testDef.printOptions(self.options)
46  for line in lines:
47  print(prefix + line)
48  return
49 
50  # Start the watchdog timer
51  def start(self):
52  if not self.timer or not self.timer.is_alive():
53  self.timer = Timer(int(self.timeout.total_seconds()),
54  self.defaultHandler)
55  self.timer.start()
56 
57  # Stop the watchdog timer
58  def stop(self):
59  if self.timer:
60  self.timer.cancel()
61  self.timer = None
62 
63  # Reset the watchdog timer
64  def reset(self):
65  self.stop()
66  self.start()
67 
68  def activate(self):
69  if not self.activated:
70  IPlugin.activate(self)
71  self.activated = True
72 
73  # Catch when deactivated to stop the timer thread
74  def deactivate(self):
75  if self.activated:
76  IPlugin.deactivate(self)
77  self.stop()
78  self.activated = False
79 
80  # This function is called when timer runs out of time!
81  # Give a SIGINT to parent process to stop execution
82  def defaultHandler(self):
83  if self.testDef: self.testDef.plugin_trans_sem.acquire()
84  os.kill(os.getpid(), signal.SIGINT)
85 
86  def convert_to_timeout(self, timeout):
87  if isinstance(timeout, int):
88  return datetime.timedelta(0, timeout)
89  if isinstance(timeout, basestring):
90  timeparts = timeout.split(":")
91  secs = 0
92  days = 0
93  try:
94  secs = int(timeparts[-1])
95  secs += int(timeparts[-2])*60
96  secs += int(timeparts[-3])*60*60
97  days = int(timeparts[-4])
98  except IndexError:
99  pass
100  return datetime.timedelta(days, secs)
101  return None
102 
103  # Start execution of the plugin from an INI file
104  def execute(self, log, keyvals, testDef):
105  testDef.logger.verbose_print("Watchdog Execute")
106  cmds = {}
107  testDef.parseOptions(log, self.options, keyvals, cmds)
108  self.testDef = testDef
109  try:
110  self.timeout = self.convert_to_timeout(cmds['timeout'])
111  if self.timeout is None:
112  log['status'] = 1
113  log['stderr'] = "Could not parse time input from ini file for Watchdog"
114  return
115  except ValueError:
116  log['status'] = 1
117  log['stderr'] = "Could not parse time input from ini file for Watchdog"
118  return
119  self.start()
120  log['status'] = 0
121  return
def convert_to_timeout
Definition: Watchdog.py:86