-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTaskExecutor.hpp
More file actions
194 lines (170 loc) · 6.49 KB
/
TaskExecutor.hpp
File metadata and controls
194 lines (170 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef SPIDER_WORKER_TASKEXECUTOR_HPP
#define SPIDER_WORKER_TASKEXECUTOR_HPP
#include <unistd.h>
#include <array>
#include <condition_variable>
#include <cstdint>
#include <memory>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <absl/container/flat_hash_map.h>
#include <boost/filesystem/path.hpp>
#include <boost/process/v2/environment.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <fmt/format.h>
#include <spider/io/BoostAsio.hpp> // IWYU pragma: keep
#include <spider/io/MsgPack.hpp> // IWYU pragma: keep
#include <spider/utils/ProgramOptions.hpp>
#include <spider/worker/FunctionManager.hpp>
#include <spider/worker/message_pipe.hpp>
#include <spider/worker/Process.hpp>
namespace spider::worker {
enum class TaskExecutorState : std::uint8_t {
Running,
Waiting,
Succeed,
Error,
Cancelled,
};
class TaskExecutor {
public:
template <class... Args>
TaskExecutor(
boost::asio::io_context& context,
std::string const& func_name,
boost::uuids::uuid const task_id,
std::string const& storage_url,
std::vector<std::string> const& libs,
absl::flat_hash_map<
boost::process::v2::environment::key,
boost::process::v2::environment::value> const& environment,
Args&&... args
)
: m_read_pipe(context),
m_write_pipe(context) {
std::vector<std::string> process_args{
fmt::format("--{}", core::cFunctionOption),
func_name,
fmt::format("--{}", core::cTaskIdOption),
to_string(task_id),
fmt::format("--{}", core::cStorageUrlOption),
storage_url,
fmt::format("--{}", core::cLibsOption),
};
process_args.insert(process_args.end(), libs.begin(), libs.end());
boost::filesystem::path const exe = boost::process::v2::environment::find_executable(
"spider_task_executor",
environment
);
std::array<int, 2> write_pipe_fd{};
std::array<int, 2> read_pipe_fd{};
if (pipe(read_pipe_fd.data()) == -1 || pipe(write_pipe_fd.data()) == -1) {
throw std::runtime_error("Failed to create pipe");
}
m_write_pipe.assign(write_pipe_fd[1]);
m_read_pipe.assign(read_pipe_fd[0]);
m_process = std::make_unique<Process>(Process::spawn(
exe.string(),
process_args,
write_pipe_fd[0],
read_pipe_fd[1],
std::nullopt
));
close(write_pipe_fd[0]);
close(read_pipe_fd[1]);
// Set up handler for output file
boost::asio::co_spawn(context, process_output_handler(), boost::asio::detached);
// Send args
msgpack::sbuffer const args_request
= core::create_args_request(std::forward<Args>(args)...);
send_message(m_write_pipe, args_request);
}
TaskExecutor(
boost::asio::io_context& context,
std::string const& func_name,
boost::uuids::uuid const task_id,
std::string const& storage_url,
std::vector<std::string> const& libs,
absl::flat_hash_map<
boost::process::v2::environment::key,
boost::process::v2::environment::value> const& environment,
std::vector<msgpack::sbuffer> const& args_buffers
)
: m_read_pipe(context),
m_write_pipe(context) {
std::vector<std::string> process_args{
fmt::format("--{}", core::cFunctionOption),
func_name,
fmt::format("--{}", core::cTaskIdOption),
to_string(task_id),
fmt::format("--{}", core::cStorageUrlOption),
storage_url,
fmt::format("--{}", core::cLibsOption),
};
process_args.insert(process_args.end(), libs.begin(), libs.end());
boost::filesystem::path const exe = boost::process::v2::environment::find_executable(
"spider_task_executor",
environment
);
std::array<int, 2> write_pipe_fd{};
std::array<int, 2> read_pipe_fd{};
if (pipe(read_pipe_fd.data()) == -1 || pipe(write_pipe_fd.data()) == -1) {
throw std::runtime_error("Failed to create pipe");
}
m_write_pipe.assign(write_pipe_fd[1]);
m_read_pipe.assign(read_pipe_fd[0]);
m_process = std::make_unique<Process>(Process::spawn(
exe.string(),
process_args,
write_pipe_fd[0],
read_pipe_fd[1],
std::nullopt
));
close(write_pipe_fd[0]);
close(read_pipe_fd[1]);
// Set up handler for output file
boost::asio::co_spawn(context, process_output_handler(), boost::asio::detached);
// Send args
msgpack::sbuffer const args_request = core::create_args_request(args_buffers);
send_message(m_write_pipe, args_request);
}
TaskExecutor(TaskExecutor const&) = delete;
auto operator=(TaskExecutor const&) -> TaskExecutor& = delete;
TaskExecutor(TaskExecutor&&) = delete;
auto operator=(TaskExecutor&&) -> TaskExecutor& = delete;
~TaskExecutor() = default;
/*
* @return The process ID of the task executor.
*/
[[nodiscard]] auto get_pid() const -> pid_t;
auto completed() -> bool;
auto waiting() -> bool;
auto succeed() -> bool;
auto error() -> bool;
void wait();
void cancel();
template <class T>
auto get_result() const -> std::optional<T> {
return core::response_get_result<T>(m_result_buffer);
}
[[nodiscard]] auto get_result_buffers() const -> std::optional<std::vector<msgpack::sbuffer>>;
[[nodiscard]] auto get_error() const -> std::tuple<core::FunctionInvokeError, std::string>;
private:
auto process_output_handler() -> boost::asio::awaitable<void>;
std::mutex m_state_mutex;
std::condition_variable m_complete_cv;
TaskExecutorState m_state = TaskExecutorState::Running;
// Use `std::unique_ptr` to work around requirement of default constructor
std::unique_ptr<Process> m_process = nullptr;
boost::asio::readable_pipe m_read_pipe;
boost::asio::writable_pipe m_write_pipe;
msgpack::sbuffer m_result_buffer;
};
} // namespace spider::worker
#endif // SPIDER_WORKER_TASKEXECUTOR_HPP