spawner.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. //
  3. // Copyright (c) 2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "forward.hpp"
  8. #include "policy.h"
  9. namespace rotor {
  10. namespace details {
  11. enum class request_state_t { NONE, SENT, CONFIRMED };
  12. }
  13. /** \struct spawner_t
  14. * \brief allows automatically restart actors
  15. *
  16. * Spawner will NOT create new actor instances when
  17. * supervisor is SHUTTING_DOWN or SHUT_DOWN.
  18. *
  19. */
  20. struct spawner_t {
  21. ~spawner_t();
  22. /** \brief minimum amount of time before respawning new actor
  23. *
  24. * Default value is 15 seconds.
  25. *
  26. */
  27. spawner_t &&restart_period(const pt::time_duration &) noexcept;
  28. /** \brief determines whether new actor instance should be spawned */
  29. spawner_t &&restart_policy(restart_policy_t) noexcept;
  30. /** \brief maximum amount of spawned actor instances
  31. *
  32. * The default value is `0`, which means try new restart
  33. * attempt if that is allowed by the policy.
  34. *
  35. */
  36. spawner_t &&max_attempts(size_t) noexcept;
  37. /** \brief shutdown actor when the spawned actor crashed
  38. *
  39. * The actor "crash" means actor terminated with non-zero
  40. * error code.
  41. *
  42. */
  43. spawner_t &&escalate_failure(bool = true) noexcept;
  44. /** \brief sends a message to supervisor to spawn
  45. * the new actor instance
  46. */
  47. void spawn() noexcept;
  48. private:
  49. spawner_t(factory_t factory, supervisor_t &supervisor) noexcept;
  50. factory_t factory;
  51. supervisor_t &supervisor;
  52. pt::time_duration period = pt::seconds{15};
  53. restart_policy_t policy = restart_policy_t::normal_only;
  54. size_t attempts = 0;
  55. bool done = false;
  56. bool escalate = false;
  57. friend struct supervisor_t;
  58. };
  59. } // namespace rotor