This commit is contained in:
MikhailBurdukov 2024-11-20 15:37:30 +00:00
parent 4f3dc60c93
commit 1cabe0f8be
4 changed files with 12 additions and 18 deletions

View File

@ -61,7 +61,7 @@ LoggerPtr ShellCommand::getLogger()
ShellCommand::~ShellCommand() ShellCommand::~ShellCommand()
{ {
if (is_manualy_terminated) if (do_not_terminate)
return; return;
if (wait_called) if (wait_called)

View File

@ -77,9 +77,9 @@ public:
return wait_called; return wait_called;
} }
void setManuallyTerminated() void setDoNotTerminate()
{ {
is_manualy_terminated = true; do_not_terminate = true;
} }
/// Run the command using /bin/sh -c. /// Run the command using /bin/sh -c.
@ -111,7 +111,7 @@ private:
pid_t pid; pid_t pid;
Config config; Config config;
bool wait_called = false; bool wait_called = false;
bool is_manualy_terminated = false; bool do_not_terminate = false;
ShellCommand(pid_t pid_, int & in_fd_, int & out_fd_, int & err_fd_, const Config & config); ShellCommand(pid_t pid_, int & in_fd_, int & out_fd_, int & err_fd_, const Config & config);

View File

@ -11,17 +11,11 @@ ShellCommandsHolder & ShellCommandsHolder::instance()
return instance; return instance;
} }
LoggerPtr ShellCommandsHolder::getLogger()
{
return ::getLogger("ShellCommandsHolder");
}
void ShellCommandsHolder::removeCommand(pid_t pid) void ShellCommandsHolder::removeCommand(pid_t pid)
{ {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
bool is_erased = shell_commands.erase(pid); bool is_erased = shell_commands.erase(pid);
LOG_TRACE(getLogger(), "Try to erase command with the pid {}, is_erased: {}", pid, is_erased); LOG_TRACE(log, "Try to erase command with the pid {}, is_erased: {}", pid, is_erased);
} }
void ShellCommandsHolder::addCommand(std::unique_ptr<ShellCommand> command) void ShellCommandsHolder::addCommand(std::unique_ptr<ShellCommand> command)
@ -30,30 +24,30 @@ void ShellCommandsHolder::addCommand(std::unique_ptr<ShellCommand> command)
pid_t command_pid = command->getPid(); pid_t command_pid = command->getPid();
if (command->waitIfProccesTerminated()) if (command->waitIfProccesTerminated())
{ {
LOG_TRACE(getLogger(), "Pid {} already finished. Do not insert it.", command_pid); LOG_TRACE(log, "Pid {} already finished. Do not insert it.", command_pid);
return; return;
} }
auto [iterator, is_inserted] = shell_commands.try_emplace(command_pid, std::move(command)); auto [iterator, is_inserted] = shell_commands.try_emplace(command_pid, std::move(command));
if (is_inserted) if (is_inserted)
{ {
LOG_TRACE(getLogger(), "Inserted the command with pid {}", command_pid); LOG_TRACE(log, "Inserted the command with pid {}", command_pid);
return; return;
} }
if (iterator->second->isWaitCalled()) if (iterator->second->isWaitCalled())
{ {
iterator->second = std::move(command); iterator->second = std::move(command);
LOG_TRACE(getLogger(), "Replaced the command with pid {}", command_pid); LOG_TRACE(log, "Replaced the command with pid {}", command_pid);
return; return;
} }
/// We got two active ShellCommand with the same pid. /// We got two active ShellCommand with the same pid.
/// Probably it is a bug, will try to replace the old shell command with a new one. /// Probably it is a bug, will try to replace the old shell command with a new one.
chassert(false);
LOG_WARNING(getLogger(), "The PID already presented in active shell commands, will try to replace with a new one."); LOG_WARNING(log, "The PID already presented in active shell commands, will try to replace with a new one.");
iterator->second->setManuallyTerminated(); iterator->second->setDoNotTerminate();
iterator->second = std::move(command); iterator->second = std::move(command);
} }
} }

View File

@ -26,7 +26,7 @@ private:
std::mutex mutex; std::mutex mutex;
ShellCommands shell_commands TSA_GUARDED_BY(mutex); ShellCommands shell_commands TSA_GUARDED_BY(mutex);
static LoggerPtr getLogger(); LoggerPtr log = getLogger("ShellCommandsHolder");
}; };
} }