userver: userver/engine/subprocess/process_starter.hpp Source File
Loading...
Searching...
No Matches
process_starter.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/subprocess/process_starter.hpp
4/// @brief @copybrief engine::subprocess::ProcessStarter
5
6#include <optional>
7#include <string>
8#include <vector>
9
10#include <userver/engine/subprocess/child_process.hpp>
11#include <userver/engine/subprocess/environment_variables.hpp>
12#include <userver/engine/task/task_processor_fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace engine::ev {
17class ThreadControl;
18} // namespace engine::ev
19
20namespace engine::subprocess {
21
22/// @brief Structure of settings for executing commands in the OS.
23struct ExecOptions final {
24 /// EnvironmentVariables for the environment in execution, or `std::nullopt`
25 /// to use GetCurrentEnvironmentVariables()
26 std::optional<EnvironmentVariables> env{};
27 /// EnvironmentVariablesUpdate for the update environment before execution, or
28 /// `std::nullopt` to leave `env` as is
30 /// File path to be redirected stdout, or `std::nullopt` to use the service's
31 /// stdout
32 std::optional<std::string> stdout_file{};
33 /// File path to be redirected stderr, or `std::nullopt` to use the service's
34 /// stderr
35 std::optional<std::string> stderr_file{};
36 /// If `false`, `command` is treated as absolute path or a relative path.
37 /// If `true`, and `command` does not contain `/`, and PATH in environment
38 /// variables, then it will be searched in the colon-separated list of
39 /// directory pathnames specified in the PATH environment variable.
40 /// If `true`, and `command` contains `/`, `command` is treated as absolute
41 /// path or a relative path.
42 bool use_path{false};
43};
44
45/// @ingroup userver_clients
46///
47/// @brief Creates a new OS subprocess and executes a command in it.
49public:
50 /// @param task_processor will be used for executing asynchronous fork + exec.
51 /// `main-task-processor is OK for this purpose.
52 explicit ProcessStarter(TaskProcessor& task_processor);
53
54 /// @param command the absolute path or relative path. If `use_path` is
55 /// `true`, and `command` does not contains `/`, then it will be searched in
56 /// the colon-separated list of directory pathnames specified in the PATH
57 /// environment variable. More details @ref ExecOptions::use_path
58 /// @param args exact args passed to the executable
59 /// @param options @ref ExecOptions settings
60 /// @throws std::runtime_error if `use_path` is `true`, `command` contains `/`
61 /// and PATH not in environment variables
62 ChildProcess Exec(const std::string& command, const std::vector<std::string>& args, ExecOptions&& options = {});
63
64 /// @overload
65 /// @param command the absolute path or relative path
66 /// @param args exact args passed to the executable
67 /// @param env redefines all environment variables
68 /// @deprecated Use the `Exec` overload taking @ref ExecOptions
69 ChildProcess Exec(
70 const std::string& command,
71 const std::vector<std::string>& args,
72 const EnvironmentVariables& env,
73 // TODO: use something like pipes instead of path to files
74 const std::optional<std::string>& stdout_file = std::nullopt,
75 const std::optional<std::string>& stderr_file = std::nullopt
76 );
77
78 /// @overload
79 /// @param command the absolute path or relative path
80 /// @param args exact args passed to the executable
81 /// @param env_update variables to add to the current environment, overwriting
82 /// existing ones
83 /// @deprecated Use the `Exec` overload taking @ref ExecOptions
84 ChildProcess Exec(
85 const std::string& command,
86 const std::vector<std::string>& args,
88 // TODO: use something like pipes instead of path to files
89 const std::optional<std::string>& stdout_file = std::nullopt,
90 const std::optional<std::string>& stderr_file = std::nullopt
91 );
92
93private:
94 ev::ThreadControl& thread_control_;
95};
96
97} // namespace engine::subprocess
98
99USERVER_NAMESPACE_END