Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 1 | #ifndef AOS_STARTER_STARTER_RPC_LIB_H_ |
| 2 | #define AOS_STARTER_STARTER_RPC_LIB_H_ |
| 3 | |
| 4 | #include <chrono> |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 5 | #include <map> |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 6 | #include <optional> |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 7 | #include <vector> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 8 | |
| 9 | #include "aos/configuration.h" |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 10 | #include "aos/events/event_loop.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 11 | #include "aos/starter/starter_generated.h" |
| 12 | #include "aos/starter/starter_rpc_generated.h" |
| 13 | |
| 14 | namespace aos { |
| 15 | namespace starter { |
| 16 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 17 | // Data required to command that starter start/stop/restart a given application. |
| 18 | struct ApplicationCommand { |
| 19 | Command command; |
| 20 | std::string_view application; |
| 21 | std::vector<const aos::Node *> nodes; |
| 22 | }; |
| 23 | |
| 24 | // This class manages interacting with starterd so that you can conveniently |
| 25 | // start/stop applications programmatically. |
| 26 | // Note that the StarterClient only maintains internal state for a single set of |
| 27 | // commands at once, so once the user calls SendCommands() they must wait for |
| 28 | // the timeout or success handler to be called before calling SendCommands |
| 29 | // again. |
| 30 | class StarterClient { |
| 31 | public: |
| 32 | StarterClient(EventLoop *event_loop); |
| 33 | |
| 34 | void SendCommands(const std::vector<ApplicationCommand> &commands, |
| 35 | monotonic_clock::duration timeout); |
| 36 | |
| 37 | void SetTimeoutHandler(std::function<void()> handler) { |
| 38 | timeout_handler_ = handler; |
| 39 | } |
| 40 | |
| 41 | void SetSuccessHandler(std::function<void()> handler) { |
| 42 | success_handler_ = handler; |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | struct CommandStatus { |
| 47 | State expected_state; |
| 48 | std::string application; |
| 49 | std::optional<uint64_t> old_id; |
| 50 | }; |
| 51 | |
| 52 | bool CheckCommandsSucceeded(); |
| 53 | |
| 54 | void Timeout(); |
| 55 | |
| 56 | void Succeed(); |
| 57 | |
| 58 | EventLoop *event_loop_; |
| 59 | TimerHandler *timeout_timer_; |
| 60 | Sender<StarterRpc> cmd_sender_; |
| 61 | // Map of fetchers by node name. |
| 62 | std::map<std::string, Fetcher<Status>> status_fetchers_; |
| 63 | |
| 64 | // Mapping of node name to a list of applications with pending commands. |
| 65 | std::map<std::string, std::vector<CommandStatus>> current_commands_; |
| 66 | |
| 67 | std::function<void()> timeout_handler_; |
| 68 | std::function<void()> success_handler_; |
| 69 | }; |
| 70 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 71 | // Finds the status of an individual application within a starter status message |
| 72 | // Returns nullptr if no application found by the given name. |
| 73 | const aos::starter::ApplicationStatus *FindApplicationStatus( |
| 74 | const aos::starter::Status &status, std::string_view name); |
| 75 | |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 76 | // Checks if the name is an executable name and if it is, it returns that |
| 77 | // application's name, otherwise returns name as given |
| 78 | std::string_view FindApplication(const std::string_view &name, |
| 79 | const aos::Configuration *config); |
| 80 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 81 | // Sends the given command to the application with the name name. Creates a |
| 82 | // temporary event loop from the provided config for sending the command and |
| 83 | // receiving back status messages. Returns true if the command executed |
| 84 | // successfully, or false otherwise. Returns false if the desired state was not |
| 85 | // achieved within timeout. |
| 86 | bool SendCommandBlocking(aos::starter::Command, std::string_view name, |
| 87 | const aos::Configuration *config, |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 88 | std::chrono::milliseconds timeout, |
| 89 | std::vector<const aos::Node *> nodes = {}); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 90 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 91 | // Sends lots of commands and waits for them all to succeed. There must not be |
| 92 | // more than 1 conflicting command in here which modifies the state of a single |
| 93 | // application otherwise it will never succeed. An example is having both a |
| 94 | // start and stop command for a single application. |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 95 | bool SendCommandBlocking(const std::vector<ApplicationCommand> &commands, |
| 96 | const aos::Configuration *config, |
| 97 | std::chrono::milliseconds timeout); |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 98 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 99 | // Fetches the status of the application with the given name. Creates a |
James Kuszmaul | e4bb0a2 | 2022-01-07 18:14:43 -0800 | [diff] [blame^] | 100 | // temporary event loop from the provided config for fetching. Returns nullopt |
| 101 | // if the application is not found. |
| 102 | const std::optional< |
| 103 | aos::FlatbufferDetachedBuffer<aos::starter::ApplicationStatus>> |
| 104 | GetStatus(std::string_view name, const aos::Configuration *config, |
| 105 | const aos::Node *node); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 106 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 107 | // Fetches the entire status message of starter. Creates a temporary event loop |
| 108 | // from the provided config for fetching. |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 109 | // The returned pair is the time at which the Status was sent on the node it was |
| 110 | // sent from, to allow calculating uptimes on remote nodes. |
| 111 | // TODO(james): Use the ServerStatistics message and return the monotonic offset |
| 112 | // instead, so that we can correctly handle high message latencies. Because |
| 113 | // people don't generally care about ultra-high-precision uptime calculations, |
| 114 | // this hasn't been prioritized. |
| 115 | std::optional<std::pair<aos::monotonic_clock::time_point, |
| 116 | const aos::FlatbufferVector<aos::starter::Status>>> |
| 117 | GetStarterStatus(const aos::Configuration *config, const aos::Node *node); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 118 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 119 | } // namespace starter |
| 120 | } // namespace aos |
| 121 | |
| 122 | #endif // AOS_STARTER_STARTER_RPC_LIB_H_ |