test_service.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "service.h"
  2. // A test service.
  3. //
  4. // This service can be induced to successfully start or fail (once it is STARTING) by calling either the
  5. // started() or failed_to_start() functions.
  6. //
  7. class test_service : public service_record
  8. {
  9. public:
  10. test_service(service_set *set, std::string name, service_type_t type_p,
  11. const std::list<prelim_dep> &deplist_p)
  12. : service_record(set, name, type_p, deplist_p)
  13. {
  14. }
  15. bool auto_stop = true; // whether to call stopped() immediately from bring_down()
  16. // Do any post-dependency startup; return false on failure
  17. virtual bool bring_up() noexcept override
  18. {
  19. // return service_record::bring_up();
  20. return true;
  21. }
  22. // All dependents have stopped.
  23. virtual void bring_down() noexcept override
  24. {
  25. waiting_for_deps = false;
  26. if (auto_stop) {
  27. stopped();
  28. }
  29. }
  30. void stopped() noexcept
  31. {
  32. assert(get_state() != service_state_t::STOPPED);
  33. service_record::stopped();
  34. }
  35. // Whether a STARTING service can immediately transition to STOPPED (as opposed to
  36. // having to wait for it reach STARTED and then go through STOPPING).
  37. virtual bool can_interrupt_start() noexcept override
  38. {
  39. return waiting_for_deps;
  40. }
  41. virtual bool interrupt_start() noexcept override
  42. {
  43. return true;
  44. }
  45. void started() noexcept
  46. {
  47. service_record::started();
  48. }
  49. void failed_to_start() noexcept
  50. {
  51. service_record::failed_to_start();
  52. }
  53. };