ShellCommand: automatic wait call in destructor (#1210)

* ShellCommand: automatic wait call in destructor

* ShellCommand: tryWait in destructor

* Update ShellCommand.cpp

* Update ShellCommand.cpp
This commit is contained in:
proller 2017-09-07 03:12:39 +03:00 committed by alexey-milovidov
parent 6911974d56
commit d422777d9c
3 changed files with 23 additions and 2 deletions

View File

@ -76,6 +76,11 @@ namespace
namespace DB
{
ShellCommand::~ShellCommand()
{
if (!wait_called)
tryWait();
}
std::unique_ptr<ShellCommand> ShellCommand::executeImpl(const char * filename, char * const argv[], bool pipe_stdin_only)
{
@ -176,6 +181,8 @@ std::unique_ptr<ShellCommand> ShellCommand::executeDirect(const std::string & pa
int ShellCommand::tryWait()
{
wait_called = true;
int status = 0;
if (-1 == waitpid(pid, &status, 0))
throwFromErrno("Cannot waitpid", ErrorCodes::CANNOT_WAITPID);

View File

@ -27,6 +27,7 @@ class ShellCommand
{
private:
pid_t pid;
bool wait_called = false;
ShellCommand(pid_t pid, int in_fd, int out_fd, int err_fd)
: pid(pid), in(in_fd), out(out_fd), err(err_fd) {};
@ -38,6 +39,8 @@ public:
ReadBufferFromFile out;
ReadBufferFromFile err;
~ShellCommand();
/// Run the command using /bin/sh -c
static std::unique_ptr<ShellCommand> execute(const std::string & command, bool pipe_stdin_only = false);

View File

@ -5,6 +5,8 @@
#include <IO/WriteBufferFromFileDescriptor.h>
#include <IO/ReadBufferFromString.h>
#include <chrono>
#include <thread>
using namespace DB;
@ -43,6 +45,15 @@ try
command->wait();
}
// <defunct> hunting:
for (int i = 0; i < 1000; ++i) {
auto command = ShellCommand::execute("echo " + std::to_string(i));
//command->wait(); // now automatic
}
// std::cerr << "inspect me: ps auxwwf" << "\n";
// std::this_thread::sleep_for(std::chrono::seconds(100));
}
catch (...)
{