Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New work interface #1801

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions src/historywork/RunCommandWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,52 @@
namespace stellar
{

RunCommandWork::RunCommandWork(Application& app, WorkParent& parent,
std::string const& uniqueName, size_t maxRetries)
: Work(app, parent, uniqueName, maxRetries)
RunCommandWork::RunCommandWork(Application& app, std::function<void()> callback,
std::string const& name, size_t maxRetries)
: Work(app, callback, name, maxRetries)
{
}

RunCommandWork::~RunCommandWork()
BasicWork::State
RunCommandWork::doWork()
{
clearChildren();
}

void
RunCommandWork::onStart()
{
std::string cmd, outfile;
getCommand(cmd, outfile);
if (!cmd.empty())
if (mDone)
{
auto exit = mApp.getProcessManager().runProcess(cmd, outfile);
exit.async_wait(callComplete());
return mEc ? BasicWork::WORK_FAILURE_RETRY : BasicWork::WORK_SUCCESS;
}
else
{
scheduleSuccess();
std::string cmd, outfile;
std::tie(cmd, outfile) = getCommand();
if (!cmd.empty())
{
auto exit = mApp.getProcessManager().runProcess(cmd, outfile);

std::weak_ptr<RunCommandWork> weak(
std::static_pointer_cast<RunCommandWork>(shared_from_this()));
exit.async_wait([weak](asio::error_code ec) {
auto self = weak.lock();
if (self)
{
self->mEc = ec;
self->mDone = true;
self->wakeUp();
}
});
return BasicWork::WORK_WAITING;
}
else
{
return BasicWork::WORK_SUCCESS;
}
}
}

void
RunCommandWork::onRun()
RunCommandWork::onReset()
{
// Do nothing: we ran the command in onStart().
mDone = false;
mEc = asio::error_code();
Work::onReset();
}
}
31 changes: 18 additions & 13 deletions src/historywork/RunCommandWork.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@

namespace stellar
{
using RunCommandInfo = std::pair<std::string, std::string>;

// This subclass exists for two reasons: first, to factor out a little code
// around running commands, and second to ensure that command-running
// happens from onStart rather than onRun, and that onRun is an empty
// method; this way we only run a command _once_ (when it's first
// scheduled) rather than repeatedly (racing with other copies of itself)
// when rescheduled.
/**
* This class helps run various commands, that require
* process spawning. This work is not scheduled while it's
* waiting for a process to exit, and wakes up when it's ready
* to be scheduled again.
*/
class RunCommandWork : public Work
marta-lokhova marked this conversation as resolved.
Show resolved Hide resolved
{
virtual void getCommand(std::string& cmdLine, std::string& outFile) = 0;
bool mDone{false};
asio::error_code mEc;
virtual RunCommandInfo getCommand() = 0;

public:
RunCommandWork(Application& app, WorkParent& parent,
std::string const& uniqueName,
size_t maxRetries = Work::RETRY_A_FEW);
~RunCommandWork();
void onStart() override;
void onRun() override;
RunCommandWork(Application& app, std::function<void()> callback,
std::string const& name,
size_t maxRetries = BasicWork::RETRY_A_FEW);
~RunCommandWork() = default;

protected:
void onReset() override;
BasicWork::State doWork() override;
};
}