supervisor_config.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2020 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "policy.h"
  8. #include "actor_config.h"
  9. namespace rotor {
  10. /** \struct supervisor_config_t
  11. * \brief base supervisor config, which holds shutdowm timeout value
  12. */
  13. struct supervisor_config_t : actor_config_t {
  14. using actor_config_t::actor_config_t;
  15. /** \brief how to behave if child-actor fails */
  16. supervisor_policy_t policy = supervisor_policy_t::shutdown_self;
  17. /** \brief whether the registy actor should be instantiated by supervisor */
  18. bool create_registry = false;
  19. /** \brief whether supervisor should wait untill all actors confirmed
  20. * initialization, and only then send start signal to all of them */
  21. bool synchronize_start = false;
  22. /** \brief use the specified address of a registry
  23. *
  24. * Can be usesul if an registry was created on the different supervisors
  25. * hierarchy
  26. */
  27. address_ptr_t registry_address;
  28. };
  29. /** \brief CRTP supervisor config builder */
  30. template <typename Supervisor> struct supervisor_config_builder_t : actor_config_builder_t<Supervisor> {
  31. /** \brief final builder class */
  32. using builder_t = typename Supervisor::template config_builder_t<Supervisor>;
  33. /** \brief parent config builder */
  34. using parent_t = actor_config_builder_t<Supervisor>;
  35. using parent_t::parent_t;
  36. /** \brief defines actor's startup policy */
  37. builder_t &&policy(supervisor_policy_t policy_) &&noexcept {
  38. parent_t::config.policy = policy_;
  39. return std::move(*static_cast<typename parent_t::builder_t *>(this));
  40. }
  41. /** \brief instructs supervisor to create an registry */
  42. builder_t &&create_registry(bool value = true) &&noexcept {
  43. parent_t::config.create_registry = value;
  44. return std::move(*static_cast<typename parent_t::builder_t *>(this));
  45. }
  46. /** \brief instructs supervisor to synchornize start on it's children actors */
  47. builder_t &&synchronize_start(bool value = true) &&noexcept {
  48. parent_t::config.synchronize_start = value;
  49. return std::move(*static_cast<typename parent_t::builder_t *>(this));
  50. }
  51. /** \brief injects external registry address */
  52. builder_t &&registry_address(const address_ptr_t &value) &&noexcept {
  53. parent_t::config.registry_address = value;
  54. return std::move(*static_cast<typename parent_t::builder_t *>(this));
  55. }
  56. virtual bool validate() noexcept {
  57. bool r = parent_t::validate();
  58. if (r) {
  59. r = !(parent_t::config.registry_address && parent_t::config.create_registry);
  60. }
  61. return r;
  62. }
  63. };
  64. } // namespace rotor