Simplify the Job Queue:

This is a refactor aimed at cleaning up and simplifying the existing
job queue.

As of now, all jobs are cancelled at the same time and in the same
way, so this commit removes the per-job cancellation token. If the
need for such support is demonstrated, support can be re-added.

* Revise documentation for ClosureCounter and Workers.
* Simplify code, removing unnecessary function arguments and
  deduplicating expressions
* Restructure job handlers to no longer need to pass a job's
  handle to the job.
This commit is contained in:
John Freeman
2021-11-16 10:55:52 -06:00
committed by Nik Bougalis
parent df02eb125f
commit c2a08a1f26
31 changed files with 164 additions and 242 deletions

View File

@@ -360,7 +360,7 @@ ShardArchiveHandler::next(std::lock_guard<std::mutex> const& l)
// to prevent holding up the lock if the downloader
// sleeps.
auto const& url{archives_.begin()->second};
auto wrapper = jobCounter_.wrap([this, url, dstDir](Job&) {
auto wrapper = jobCounter_.wrap([this, url, dstDir]() {
auto const ssl = (url.scheme == "https");
auto const defaultPort = ssl ? 443 : 80;
@@ -416,42 +416,41 @@ ShardArchiveHandler::complete(path dstPath)
}
// Make lambdas mutable captured vars can be moved from
auto wrapper =
jobCounter_.wrap([=, dstPath = std::move(dstPath)](Job&) mutable {
if (stopping_)
return;
auto wrapper = jobCounter_.wrap([=,
dstPath = std::move(dstPath)]() mutable {
if (stopping_)
return;
// If not synced then defer and retry
auto const mode{app_.getOPs().getOperatingMode()};
if (mode != OperatingMode::FULL)
{
std::lock_guard lock(m_);
timer_.expires_from_now(static_cast<std::chrono::seconds>(
(static_cast<std::size_t>(OperatingMode::FULL) -
static_cast<std::size_t>(mode)) *
10));
// If not synced then defer and retry
auto const mode{app_.getOPs().getOperatingMode()};
if (mode != OperatingMode::FULL)
{
std::lock_guard lock(m_);
timer_.expires_from_now(static_cast<std::chrono::seconds>(
(static_cast<std::size_t>(OperatingMode::FULL) -
static_cast<std::size_t>(mode)) *
10));
auto wrapper = timerCounter_.wrap(
[=, dstPath = std::move(dstPath)](
boost::system::error_code const& ec) mutable {
if (ec != boost::asio::error::operation_aborted)
complete(std::move(dstPath));
});
auto wrapper = timerCounter_.wrap(
[=, dstPath = std::move(dstPath)](
boost::system::error_code const& ec) mutable {
if (ec != boost::asio::error::operation_aborted)
complete(std::move(dstPath));
});
if (!wrapper)
onClosureFailed(
"failed to wrap closure for operating mode timer",
lock);
else
timer_.async_wait(*wrapper);
}
if (!wrapper)
onClosureFailed(
"failed to wrap closure for operating mode timer", lock);
else
{
process(dstPath);
std::lock_guard lock(m_);
removeAndProceed(lock);
}
});
timer_.async_wait(*wrapper);
}
else
{
process(dstPath);
std::lock_guard lock(m_);
removeAndProceed(lock);
}
});
if (!wrapper)
{