From cc12b4765c1ac05166a147da74d9949e35ed477b Mon Sep 17 00:00:00 2001 From: unrealdragon Date: Thu, 14 May 2026 17:06:59 +0530 Subject: [PATCH] Version 0.0.1 fixing issues. --- appinfo.cpp | 11 +++++--- appinfo.h | 3 ++- executionmanager.cpp | 64 ++++++++++++++++++++++++-------------------- main.cpp | 19 +++++++------ 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/appinfo.cpp b/appinfo.cpp index 5f52a10..f24d7bf 100644 --- a/appinfo.cpp +++ b/appinfo.cpp @@ -4,7 +4,7 @@ #include AppInfo::~AppInfo() { - std::cout << "Module: " << getModuleName() << " finished ops." << std::endl; + std::cout << "Module: " << getModuleName() << " finished ops." << std::endl; } void AppInfo::setProgramName(std::string name) @@ -66,7 +66,7 @@ bool AppInfo::isParentDependencyCleared(std::osyncstream &out) const { for(auto &[key, value]: parents) { if(key->getState() != value) { - out << key->getModuleName() << " not at state: " << stateToString(state) << + out << key->getModuleName() << " not at state: " << stateToString(value) << " but is at:" << stateToString(key->getState()) << std::endl; return false; } @@ -130,6 +130,7 @@ void AppInfo::copyFromData(AppInfo::AppData &other) for(auto &i: other.args) { this->args.push_back(i); } + this->after = other.after; setRunMode(other.run_mode); } @@ -187,7 +188,8 @@ void AppInfo::printInfo(std::ostream &out) ss << key << "[" << value << "], "; } std::string after = ss.str(); - after = after.substr(0,after.size() - 2); + if(!after.empty() && after.size() >= 2) + after = after.substr(0,after.size() - 2); out << "After: " << after << std::endl; std::stringstream ss2; for(auto &[key, value]: parents) { @@ -263,7 +265,8 @@ void AppInfo::AppData::printInfo(std::ostream &out) ss << key << "[" << value << "], "; } std::string after = ss.str(); - after = after.substr(0,after.size() - 2); + if(!after.empty() && after.size() >= 2) + after = after.substr(0,after.size() - 2); out << "After: " << after << std::endl; out << "Create Log: " << AppData::create_log << std::endl; out << "************************************End************************************" << std::endl; diff --git a/appinfo.h b/appinfo.h index a830988..68cb232 100644 --- a/appinfo.h +++ b/appinfo.h @@ -7,6 +7,7 @@ #include #include #include +#include class AppInfo { @@ -42,7 +43,7 @@ private: string_map after; bool create_log = false; std::unordered_map, State> parents; - State state = State::initial; + std::atomic state{State::initial}; RunMode mode = RunMode::script; public: void setProgramName(std::string name); diff --git a/executionmanager.cpp b/executionmanager.cpp index dda1fac..e37f175 100644 --- a/executionmanager.cpp +++ b/executionmanager.cpp @@ -47,10 +47,9 @@ void ExecutionManager::run() { finished = false; initial_list_size = job_list.size(); - while(!job_list.empty()) { + while(true) { for(int i=active_thread_count.load(); i < threads ;i++) { int val = 0; - active_thread_count.fetch_add(1); { std::lock_guard lock_get(this->job_list_mutex); val = index_count++; @@ -63,6 +62,7 @@ void ExecutionManager::run() break; } } + active_thread_count.fetch_add(1); std::shared_ptr thread = std::make_shared([this, val](){ std::shared_ptr ptr; std::osyncstream os(log_file.rdbuf()); @@ -107,12 +107,17 @@ void ExecutionManager::run() bool create_log = ptr->getCreateLog(); std::deque pargs = ptr->getArgs(); int total_args = pargs.size() + 2; - char *args[total_args]; + std::vector args(total_args); args[0] = program_name.data(); for(int i=1,index=0; index < pargs.size(); i++,index++) { args[i] = pargs[index].data(); } args[total_args - 1] = (char *)NULL; + bool log_file_exists = std::filesystem::exists(program_log_file); + long long size = 0; + if(log_file_exists) + size = std::filesystem::file_size(program_log_file); + // Do fork and run auto pid = fork(); FILE *popen_file = nullptr; @@ -120,44 +125,40 @@ void ExecutionManager::run() case 0: { if(!dir.empty()) { int err = chdir(dir.c_str()); - if(err != 0) { - os << "Child changing dir failed: " << err << std::endl; - } } - os << "Child running: " << program_name << " with args: " << args << std::endl; - bool log_file_exists = std::filesystem::exists(program_log_file); if(create_log) { int output_fd = -99; if(!log_file_exists) output_fd = open(program_log_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); else { - auto size = std::filesystem::file_size(program_log_file); if(size < log_file_max_size) { output_fd = open(program_log_file.c_str(), O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR); } else { output_fd = open(program_log_file.c_str(), O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); - os << "Truncating log file: " << program_log_file << std::endl; } } if (output_fd < 0) { - std::cout << "Failed to open log file " << program_log_file << ":" - << std::endl; - std::perror("open"); - std::exit(1); + _exit(3); } if (dup2(output_fd, STDOUT_FILENO) < 0) { - std::perror("dup2 (stdout)"); - std::exit(1); + _exit(3); } if (dup2(output_fd, STDERR_FILENO) < 0) { - std::perror("dup2 (stderr)"); - std::exit(1); + _exit(3); } + close(output_fd); } - execv(program_name.c_str(), args); - exit(0); + execv(program_name.c_str(), args.data()); + _exit(1); + } + case -1: { + os << "Fork failed for module: " << module_name << std::endl; + ptr->setFinished(); // Or a dedicated failed state + state_change.fetch_add(1); + state_change.notify_all(); + break; } default: { // After fork in parent do @@ -174,27 +175,32 @@ void ExecutionManager::run() if(mode == AppInfo::RunMode::script) { os << "Run mode is script so will wait for child to finish: " << module_name << std::endl; int wait = waitpid(pid, &status, WUNTRACED); + ptr->setFinished(); os << "Parent wait finished: " << wait << " child status: " << status << std::endl; } // Set finished - ptr->setFinished(); state_change.fetch_add(1); state_change.notify_all(); os << std::vformat("Thread[{}] finished: ", std::make_format_args(val)) << module_name<< std::endl; - active_thread_count.fetch_min(1); + active_thread_count.fetch_sub(1); }); thread_list.push_back(thread); } - std::lock_guard lock_get(this->job_list_mutex); - if(job_list.empty()) { - log_file << "Main Thread: Job list is empty, waiting for finish." << std::endl; - break; + { + std::lock_guard lock_get(this->job_list_mutex); + if(job_list.empty()) { + log_file << "Main Thread: Job list is empty, waiting for finish." << std::endl; + break; + } + if((index_count) > (initial_list_size - 1)) { + log_file << "Main Thread: All jobs queued." << std::endl; + break; + } } - if((index_count) > (initial_list_size - 1)) { - log_file << "Main Thread: All jobs queued." << std::endl; - break; + if (active_thread_count.load() >= threads) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } for(auto i: thread_list) { diff --git a/main.cpp b/main.cpp index 9d7151b..7af40a5 100644 --- a/main.cpp +++ b/main.cpp @@ -178,7 +178,7 @@ void parseArgs(int c, char *v[]) { int i = contains(arg_list, "--json", "-j"); if(i >= 0) { int j = i + 1; - if(j > c) { + if(j > arg_list.size()) { printHelp(); exit(-1); } @@ -189,7 +189,7 @@ void parseArgs(int c, char *v[]) { i = contains(arg_list, "--log_dir", "-ld"); if(i >= 0) { int j = i + 1; - if(j > c) { + if(j > arg_list.size()) { printHelp(); exit(-1); } @@ -200,7 +200,7 @@ void parseArgs(int c, char *v[]) { i = contains(arg_list, "--threads", "-t"); if(i >= 0) { int j = i + 1; - if(j > c) { + if(j > arg_list.size()) { printHelp(); exit(-1); } @@ -211,7 +211,7 @@ void parseArgs(int c, char *v[]) { i = contains(arg_list, "--help", "-h"); if(i >= 0) { int j = i + 1; - if(j > c) { + if(j > arg_list.size()) { printHelp(); exit(-1); } @@ -237,7 +237,7 @@ std::string string_strip(std::string str) { bool front_found = false; for(int i=0; i checkAppList(std::deque ©_list, std::ve int reshuffle_count = 0; while(copy_list.size() != 0) { size_t copy_list_size = copy_list.size(); - for(auto item = copy_list.begin(); item != copy_list.end(); item++) { - if(item->getAfterString().size() == 0) { + for(auto item = copy_list.begin(); item != copy_list.end();) { + if(item->after.size() == 0) { std::shared_ptr app_info = std::make_shared(); app_info->copyFromData(*item); after_list.push_back(std::move(app_info)); @@ -364,6 +364,9 @@ std::optional checkAppList(std::deque ©_list, std::ve reshuffle_count = 0; item = copy_list.erase(item); } + else { + item++; + } if(copy_list.size() == 0 || item == copy_list.end()) break; }