43class RunContext :
public StorageContext {
46 friend class IProcessor;
49 RunContext(
GlobalContext& context, std::atomic<bool>& terminate_signal,
50 std::string run_group_id, std::string run_id, std::string template_id,
52 : StorageContext(context),
53 global_context_(context),
54 start_time_(Clock::now()),
55 stop_time_(start_time_),
56 terminate_signal_(terminate_signal),
57 template_id_(template_id),
58 default_test_flag_(test_flag) {
62 run_group_id_ = run_group_id.empty() ?
"default" : run_group_id;
65 add_storage_context(
"rungroup", storage_context(
"runroot") + run_group_id_);
67 if (!template_id.empty()) {
68 add_storage_context(
"templatebase", storage_context(
"rungroup") +
"/" + template_id);
69 if ((stat(storage_context(
"templatebase").c_str(), &info) < 0) ||
70 !(info.st_mode & S_IFDIR)) {
71 throw std::runtime_error(
72 "Run source folder does not exist or is not accessible. (" +
73 storage_context(
"templatebase") +
")");
78 if (mkdir(storage_context(
"rungroup").c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) !=
80 if (errno != EEXIST) {
81 throw std::runtime_error(
"Cannot create run environment " +
82 storage_context(
"rungroup"));
94 timeinfo = localtime(&rawtime);
96 strftime(buffer, 20,
"%Y%m%d_%H%M%S", timeinfo);
101 add_storage_context(
"runbase", storage_context(
"rungroup") +
"/" + run_id);
105 if (mkdir(storage_context(
"runbase").c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
106 if (errno == EEXIST) {
107 throw std::runtime_error(
"Run base folder already exists. (" +
108 storage_context(
"runbase") +
")");
110 throw std::runtime_error(
"Cannot create run base folder " +
111 storage_context(
"runbase"));
116 std::string symlinkname = storage_context(
"runroot") +
"/_last_run";
118 std::remove(symlinkname.c_str());
121 auto runbase_path = storage_context(
"runbase");
125 fs::path target_abs = fs::absolute(runbase_path);
126 fs::path link_path_abs = fs::absolute(symlinkname);
131 fs::path relative_target = fs::relative(target_abs, link_path_abs.parent_path());
134 fs::create_directory_symlink(relative_target, link_path_abs);
136 }
catch (
const fs::filesystem_error& e) {
137 LOG(WARNING) <<
"Could not create symbolic link for last run: " << e.what();
142 symlinkname = storage_context(
"runroot") +
"_last_run_group";
144 std::remove(symlinkname.c_str());
146 auto rungroup_path = storage_context(
"rungroup");
150 fs::path target_abs = fs::absolute(rungroup_path);
151 fs::path link_path_abs = fs::absolute(symlinkname);
156 fs::path relative_target = fs::relative(target_abs, link_path_abs.parent_path());
159 fs::create_directory_symlink(relative_target, link_path_abs);
161 }
catch (
const fs::filesystem_error& e) {
162 LOG(WARNING) <<
"Could not create symbolic link for last run group: " << e.what();
165 add_storage_context(
"lastrunbase", storage_context(
"rungroup") +
"/_last_run");
166 add_storage_context(
"lastrungroup", storage_context(
"runroot") +
"/_last_run_group");
168 set_default_context(
"runbase");
173 bool terminated()
const {
return terminate_signal_.load(); }
176 if (!terminate_signal_.exchange(
true)) {
177 stop_time_ = Clock::now();
180 void TerminateWithError(std::string processor_name, std::string step,
181 std::string error_message) {
182 if (!terminate_signal_.exchange(
true)) {
183 stop_time_ = Clock::now();
185 "Processor `" + processor_name +
"` failed in `" + step +
"`: " + error_message;
189 TimePoint start_time()
const {
return start_time_; }
190 TimePoint stop_time()
const {
197 int minutes()
const {
199 std::chrono::duration_cast<std::chrono::minutes>(stop_time() - start_time()).count());
201 int seconds()
const {
203 std::chrono::duration_cast<std::chrono::seconds>(stop_time() - start_time()).count());
206 std::string error_message() {
208 return error_message_;
215 return error_message_.size() > 0;
221 std::string run_group_id()
const {
return run_group_id_; }
222 std::string run_id()
const {
return run_id_; }
223 std::string template_id()
const {
return template_id_; }
225 bool test()
const {
return default_test_flag_.load(); }
229 std::condition_variable go_condition;
230 bool go_signal =
false;
236 TimePoint start_time_;
237 TimePoint stop_time_;
238 std::atomic<bool>& terminate_signal_;
239 std::string error_message_;
242 std::string run_group_id_;
244 std::string template_id_;
245 std::atomic<bool> default_test_flag_;
248class ProcessingContext :
public StorageContext {
250 ProcessingContext(
RunContext& context, std::string processor_name,
bool test)
251 : StorageContext(context),
252 run_context_(context),
253 processor_name_(processor_name),
255 add_storage_context(
"run", storage_context(
"runbase") +
"/" + processor_name);
256 if (mkdir(storage_context(
"run").c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
257 if (errno != EEXIST) {
258 throw std::runtime_error(
"Cannot create processor run environment " +
259 storage_context(
"run"));
263 if (storage_map().count(
"templatebase") == 1) {
264 add_storage_context(
"template", storage_context(
"templatebase") +
"/" + processor_name);
269 add_storage_context(
"test", storage_context(
"run") +
"/test");
270 if (mkdir(storage_context(
"test").c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) !=
272 if (errno != EEXIST) {
273 throw std::runtime_error(
"Cannot create processor test environment " +
274 storage_context(
"test"));
278 if (storage_map().count(
"template") == 1) {
279 add_storage_context(
"templatetest", storage_context(
"template") +
"/test");
283 add_storage_context(
"lastrun", storage_context(
"lastrunbase") +
"/" + processor_name);
284 add_storage_context(
"lasttest", storage_context(
"lastrun") +
"/test");
285 set_default_context(
"run");
290 bool test()
const {
return test_flag_.load(); }
292 bool terminated()
const {
return run_context_.terminated(); }
293 void Terminate() { run_context_.Terminate(); }
294 void TerminateWithError(std::string step, std::string error_message) {
295 run_context_.TerminateWithError(processor_name_, step, error_message);
300 std::string processor_name_;
301 std::atomic<bool> test_flag_;
302 std::map<std::string, std::string> storage_map_;
Definition context.hpp:54
Definition runinfo.hpp:43