Skip to content

Commit

Permalink
fix(ProcessRunner): add runCount flag #4064
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavle committed Nov 6, 2023
1 parent 025b1a0 commit ecb2d36
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Foundation/include/Poco/ProcessRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class Foundation_API ProcessRunner: public Poco::Runnable
int result() const;
/// Returns process return code.

bool done() const;
/// Returns true if the process was completely executed, otherwise false.
int runCount() const;
/// Returns the number of times the process has been executed.


private:
Expand All @@ -145,7 +145,7 @@ class Foundation_API ProcessRunner: public Poco::Runnable
std::atomic<Poco::ProcessHandle*> _pPH;
std::atomic<bool> _started;
std::atomic<int> _rc;
std::atomic<bool> _done;
std::atomic<int> _runCount;
};


Expand Down Expand Up @@ -177,9 +177,9 @@ inline int ProcessRunner::result() const
}


inline bool ProcessRunner::done() const
inline int ProcessRunner::runCount() const
{
return _done;
return _runCount;
}


Expand Down
13 changes: 8 additions & 5 deletions Foundation/src/ProcessRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ProcessRunner::ProcessRunner(const std::string& cmd,
_pPH(nullptr),
_started(false),
_rc(RESULT_UNKNOWN),
_done(false)
_runCount(0)
{
if (!File(_cmd).exists())
throw Poco::NotFoundException(_cmd);
Expand Down Expand Up @@ -102,8 +102,7 @@ std::string ProcessRunner::cmdLine() const
void ProcessRunner::run()
{
ProcessHandle* pPH = nullptr;
_done = false;


try
{
_pPH.store(new ProcessHandle(Process::launch(_cmd, _args, _options)));
Expand All @@ -117,7 +116,7 @@ void ProcessRunner::run()

_pid = INVALID_PID;
_pPH.store(nullptr);
_done = true;
_runCount++;

delete pPH;
}
Expand Down Expand Up @@ -168,12 +167,16 @@ void ProcessRunner::start()
{
if (!_started.exchange(true))
{
int prevRunCnt = runCount();

_t.start(*this);

std::string msg;
Poco::format(msg, "Waiting for process to start (pidFile: '%s')", _pidFile);
Stopwatch sw; sw.start();
while (!running() || done()) checkTimeout(sw, msg);

// wait for the process to be either running or completed by monitoring run counts.
while (!running() && prevRunCnt >= runCount()) checkTimeout(sw, msg);

// we could wait for the process handle != INVALID_PID,
// but if pidFile name was given, we should wait for
Expand Down

0 comments on commit ecb2d36

Please sign in to comment.