component.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from isc.log_messages.bind10_messages import *
  16. import time
  17. logger = isc.log.Logger("boss")
  18. """
  19. Module for managing components (abstraction of process). It allows starting
  20. them in given order, handling when they crash (what happens depends on kind
  21. of component) and shutting down. It also handles the configuration of this.
  22. Dependencies between them are not yet handled. It might turn out they are
  23. needed, in that case they will be added sometime in future.
  24. """
  25. class Component:
  26. """
  27. This represents a single component. It has some defaults of behaviour,
  28. which should be reasonable for majority of ordinary components, but
  29. it might be inherited and modified for special-purpose components,
  30. like the core modules with different ways of starting up.
  31. """
  32. def __init__(self, process, boss, kind):
  33. """
  34. Creates the component in not running mode.
  35. The parameters are:
  36. - `process` is the name of the process to start.
  37. - `boss` the boss object to plug into. The component needs to plug
  38. into it to know when it failed, etc.
  39. - `kind` is the kind of component. It may be one of:
  40. * 'core' means the system can't run without it and it can't be
  41. safely restarted. If it does not start, the system is brought
  42. down. If it crashes, the system is turned off as well (with
  43. non-zero exit status).
  44. * 'needed' means the system is able to restart the component,
  45. but it is vital part of the service (like auth server). If
  46. it fails to start or crashes in less than 10s after the first
  47. startup, the system is brought down. If it crashes later on,
  48. it is restarted.
  49. * 'dispensable' means the component should be running, but if it
  50. doesn't start or crashes for some reason, the system simply tries
  51. to restart it and keeps running.
  52. """
  53. if kind not in ['core', 'needed', 'dispensable']:
  54. raise ValueError('Component kind can not be ' + kind)
  55. self.__running = False
  56. # Dead like really dead. No resurrection possible.
  57. self.__dead = False
  58. self.__kind = kind
  59. self.__boss = boss
  60. def start(self):
  61. """
  62. Start the component for the first time or restart it. If you need to
  63. modify the way a component is started, do not replace this method,
  64. but start_internal. This one does some more bookkeeping around.
  65. If you try to start an already running component, it raises ValueError.
  66. """
  67. if self.__dead:
  68. raise ValueError("Can't resurrect already dead component")
  69. if self.running():
  70. raise ValueError("Can't start already running component")
  71. self.__running = True
  72. self.__start_time = time.time()
  73. try:
  74. self.start_internal()
  75. except:
  76. self.failed()
  77. raise
  78. def start_internal(self):
  79. """
  80. This method does the actual starting of a process. If you need to
  81. change the way the component is started, replace this method.
  82. """
  83. pass
  84. def stop(self):
  85. """
  86. Stop the component. If you need to modify the way a component is
  87. stopped, do not replace this method, but stop_internal. This one
  88. does some more bookkeeping.
  89. If you try to stop a component that is not running, it raises
  90. ValueError.
  91. """
  92. if not self.running():
  93. raise ValueError("Can't stop a component which is not running")
  94. self.stop_internal()
  95. self.__running = False
  96. def stop_internal(self):
  97. """
  98. This is the method that does the actual stopping of a component.
  99. You can replace this method if you want a different way to do it.
  100. """
  101. pass
  102. def failed(self):
  103. """
  104. Notify the component it crashed. This will be called from boss object.
  105. If you try to call failed on a component that is not running,
  106. a ValueError is raised.
  107. """
  108. if not self.running():
  109. raise ValueError("Can't fail component that isn't running")
  110. self.failed_internal()
  111. self.__running = False
  112. # If it is a core component or the needed component failed to start
  113. # (including it stopped really soon)
  114. if self.__kind == 'core' or \
  115. (self.__kind == 'needed' and time.time() - 10 < self.__start_time):
  116. self.__dead = True
  117. self.__boss.shutdown(1)
  118. # This means we want to restart
  119. else:
  120. self.start()
  121. def failed_internal(self):
  122. """
  123. This method is called from failed. You can replace it if you need
  124. some specific behaviour when the component crashes. The default
  125. implementation is empty.
  126. Do not raise exceptions from here, please. The propper shutdown
  127. would have not happened.
  128. """
  129. pass
  130. def running(self):
  131. """
  132. Informs if the component is currently running. It assumes the failed
  133. is called whenever the component really fails and there might be some
  134. time in between actual failure and the call.
  135. """
  136. return self.__running