spawner.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #if defined(_MSC_VER)
  10. #pragma warning(push)
  11. #pragma warning(disable : 4251)
  12. #endif
  13. namespace rotor {
  14. namespace details {
  15. enum class request_state_t { NONE, SENT, CONFIRMED };
  16. }
  17. /** \struct spawner_t
  18. * \brief allows automatically restart actors
  19. *
  20. * Spawner will NOT create new actor instances when
  21. * supervisor is SHUTTING_DOWN or SHUT_DOWN.
  22. *
  23. */
  24. struct ROTOR_API spawner_t {
  25. ~spawner_t();
  26. /** \brief minimum amount of time before respawning new actor
  27. *
  28. * Default value is 15 seconds.
  29. *
  30. */
  31. spawner_t &&restart_period(const pt::time_duration &) noexcept;
  32. /** \brief determines whether new actor instance should be spawned */
  33. spawner_t &&restart_policy(restart_policy_t) noexcept;
  34. /** \brief maximum amount of spawned actor instances
  35. *
  36. * The default value is `0`, which means try new restart
  37. * attempt if that is allowed by the policy.
  38. *
  39. */
  40. spawner_t &&max_attempts(size_t) noexcept;
  41. /** \brief shutdown actor when the spawned actor crashed
  42. *
  43. * The actor "crash" means actor terminated with non-zero
  44. * error code.
  45. *
  46. */
  47. spawner_t &&escalate_failure(bool = true) noexcept;
  48. /** \brief sends a message to supervisor to spawn
  49. * the new actor instance
  50. */
  51. void spawn() noexcept;
  52. private:
  53. spawner_t(factory_t factory, supervisor_t &supervisor) noexcept;
  54. factory_t factory;
  55. supervisor_t &supervisor;
  56. pt::time_duration period = pt::seconds{15};
  57. restart_policy_t policy = restart_policy_t::normal_only;
  58. size_t attempts = 0;
  59. bool done = false;
  60. bool escalate = false;
  61. friend struct supervisor_t;
  62. };
  63. } // namespace rotor
  64. #if defined(_MSC_VER)
  65. #pragma warning(pop)
  66. #endif