HeartbeatStopHandler_linux.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import os
  18. import logging
  19. import signal
  20. import threading
  21. import traceback
  22. logger = logging.getLogger()
  23. _handler = None
  24. def signal_handler(signum, frame):
  25. _handler.set_stop()
  26. def bind_signal_handlers(agentPid):
  27. if os.getpid() == agentPid:
  28. signal.signal(signal.SIGINT, signal_handler)
  29. signal.signal(signal.SIGTERM, signal_handler)
  30. signal.signal(signal.SIGUSR1, debug)
  31. global _handler
  32. _handler = HeartbeatStopHandler()
  33. return _handler
  34. def debug(sig, frame):
  35. """Interrupt running process, and provide a python prompt for
  36. interactive debugging."""
  37. d={'_frame':frame} # Allow access to frame object.
  38. d.update(frame.f_globals) # Unless shadowed by global
  39. d.update(frame.f_locals)
  40. message = "Signal received : entering python shell.\nTraceback:\n"
  41. message += ''.join(traceback.format_stack(frame))
  42. logger.info(message)
  43. class HeartbeatStopHandler:
  44. def __init__(self, stopEvent = None):
  45. # Event is used for synchronizing heartbeat iterations (to make possible
  46. # manual wait() interruption between heartbeats )
  47. self.heartbeat_wait_event = threading.Event()
  48. # Event is used to stop the Agent process
  49. if stopEvent is None:
  50. #Allow standalone testing
  51. self.stop_event = threading.Event()
  52. else:
  53. #Allow one unique event per process
  54. self.stop_event = stopEvent
  55. def set_heartbeat(self):
  56. self.heartbeat_wait_event.set()
  57. def reset_heartbeat(self):
  58. self.heartbeat_wait_event.clear()
  59. def set_stop(self):
  60. self.stop_event.set()
  61. def wait(self, timeout1, timeout2 = 0):
  62. if self.heartbeat_wait_event.wait(timeout = timeout1):
  63. #Event signaled, exit
  64. return 0
  65. # Stop loop when stop event received
  66. # Otherwise sleep a bit more to allow STATUS_COMMAND results to be collected
  67. # and sent in one heartbeat. Also avoid server overload with heartbeats
  68. if self.stop_event.wait(timeout = timeout2):
  69. logger.info("Stop event received")
  70. return 1
  71. #Timeout
  72. return -1