Version 0.0.1 fixing issues.
This commit is contained in:
parent
7e8c45e88e
commit
cc12b4765c
1 changed files with 55 additions and 42 deletions
|
|
@ -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<std::mutex> 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<std::thread> thread = std::make_shared<std::thread>([this, val](){
|
||||
std::shared_ptr<AppInfo> ptr;
|
||||
std::osyncstream os(log_file.rdbuf());
|
||||
|
|
@ -107,12 +107,17 @@ void ExecutionManager::run()
|
|||
bool create_log = ptr->getCreateLog();
|
||||
std::deque<std::string> pargs = ptr->getArgs();
|
||||
int total_args = pargs.size() + 2;
|
||||
char *args[total_args];
|
||||
std::vector<char*> 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<std::mutex> 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<std::mutex> 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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue