[cdc_stream] Automatically start service (#28)

Starts the streaming service if it's not up and running. This required
adding the ability to run a detached process. By default, all child
processes are killed when the parent process exits. Since detached
child processes don't run with a console, they need to create sub-
processes with CREATE_NO_WINDOW since otherwise a new console pops up,
e.g. for every ssh command.

Polls for 20 seconds while the service starts up. For this purpose,
a BackgroundServiceClient is added. This will be reused in a future CL
by a new stop-service command to exit the service.

Also adds --service-port as additional argument to start-service.
This commit is contained in:
Lutz Justen
2022-12-02 14:34:36 +01:00
committed by GitHub
parent 6d63aa72d7
commit 1120dcbee0
21 changed files with 372 additions and 76 deletions
+24
View File
@@ -33,6 +33,12 @@ namespace cdc_ft {
absl::Status LogOutput(const char* name, const char* data, size_t data_size,
absl::optional<LogLevel> log_level = {});
enum class ProcessFlags {
kNone = 0,
kDetached = 1 << 0,
kNoWindow = 1 << 1,
};
struct ProcessStartInfo {
// Handler for stdout/stderr. |data| is guaranteed to be NULL terminated, so
// it may be used like a C-string if it's known to be text, e.g. for printf().
@@ -63,8 +69,14 @@ struct ProcessStartInfo {
OutputHandler stdout_handler;
OutputHandler stderr_handler;
// Flags that define additional properties of the process.
ProcessFlags flags = ProcessFlags::kNone;
// Returns |name| if set, otherwise |command|.
const std::string& Name() const;
// Tests ALL flags (flags & flag) == flag.
bool HasFlag(ProcessFlags flag) const;
};
// Runs a background process and pipes stdin/stdout/stderr.
@@ -75,6 +87,8 @@ class Process {
static constexpr uint32_t kExitCodeFailedToGetExitCode = 4000000002;
explicit Process(const ProcessStartInfo& start_info);
// Terminates the process unless it's running with ProcessFlags::kDetached.
virtual ~Process();
// Start the background process.
@@ -140,6 +154,16 @@ class WinProcessFactory : public ProcessFactory {
std::unique_ptr<Process> Create(const ProcessStartInfo& start_info) override;
};
inline ProcessFlags operator|(ProcessFlags a, ProcessFlags b) {
using T = std::underlying_type_t<ProcessFlags>;
return static_cast<ProcessFlags>(static_cast<T>(a) | static_cast<T>(b));
}
inline ProcessFlags operator&(ProcessFlags a, ProcessFlags b) {
using T = std::underlying_type_t<ProcessFlags>;
return static_cast<ProcessFlags>(static_cast<T>(a) & static_cast<T>(b));
}
} // namespace cdc_ft
#endif // COMMON_PROCESS_H_