Dependencies - #410

- Completed support for 'task 1 depends:2,-3' to manipulate the
  dependencies.
- Now supports rc.dependency.reminder to indicate when to nag about
  dependency chain violations, defaulting to on.
- Now supports rc.dependency.confirm to require confirmation before
  fixing dependency chains, defaulting to on.
- New source file dependency.cpp which implements a low-level API for
  determining dependency status, and assorted handlers for task state
  changes.
- Adds blocking tasks to the 'next' report.
- Added more dependency unit tests, changed the wording in a couple of
  them and numbered them for easy reference.
This commit is contained in:
Paul Beckingham 2010-10-03 18:52:59 -04:00
parent dea7b72b70
commit 7fdfcbacc6
11 changed files with 230 additions and 140 deletions

View file

@ -31,16 +31,16 @@
#include <Context.h>
#include <text.h>
#include <util.h>
#include <main.h>
extern Context context;
////////////////////////////////////////////////////////////////////////////////
static bool followUpstream (const Task&, const Task&, const std::vector <Task>&,
std::vector <std::string>&);
static bool followUpstream (const Task&, const Task&, std::vector <std::string>&);
////////////////////////////////////////////////////////////////////////////////
// A task is blocked if it depends on tasks that are pending or waiting.
bool dependencyIsBlocked (Task& task)
bool dependencyIsBlocked (const Task& task)
{
if (task.has ("depends"))
{
@ -58,26 +58,25 @@ bool dependencyIsBlocked (Task& task)
}
////////////////////////////////////////////////////////////////////////////////
void dependencyGetBlocked (Task& task, std::vector <Task>& blocked)
void dependencyGetBlocked (const Task& task, std::vector <Task>& blocked)
{
std::string depends = task.get ("depends");
if (depends != "")
{
const std::vector <Task>& all = context.tdb.getAllPending ();
std::vector <Task>::const_iterator it;
for (it = all.begin (); it != all.end (); ++it)
if ((it->getStatus () == Task::pending ||
it->getStatus () == Task::waiting) &&
depends.find (it->get ("uuid")) != std::string::npos)
blocked.push_back (*it);
}
std::string uuid = task.get ("uuid");
const std::vector <Task>& all = context.tdb.getAllPending ();
std::vector <Task>::const_iterator it;
for (it = all.begin (); it != all.end (); ++it)
if ((it->getStatus () == Task::pending ||
it->getStatus () == Task::waiting) &&
it->has ("depends") &&
it->get ("depends").find (uuid) != std::string::npos)
blocked.push_back (*it);
}
////////////////////////////////////////////////////////////////////////////////
// To be a blocking task, there must be at least one other task that depends on
// this task, that is either pending or waiting.
bool dependencyIsBlocking (Task& task)
bool dependencyIsBlocking (const Task& task)
{
std::string uuid = task.get ("uuid");
@ -94,18 +93,19 @@ bool dependencyIsBlocking (Task& task)
}
////////////////////////////////////////////////////////////////////////////////
void dependencyGetBlocking (Task& task, std::vector <Task>& blocking)
void dependencyGetBlocking (const Task& task, std::vector <Task>& blocking)
{
std::string uuid = task.get ("uuid");
const std::vector <Task>& all = context.tdb.getAllPending ();
std::vector <Task>::const_iterator it;
for (it = all.begin (); it != all.end (); ++it)
if ((it->getStatus () == Task::pending ||
it->getStatus () == Task::waiting) &&
it->has ("depends") &&
it->get ("depends").find (uuid) != std::string::npos)
blocking.push_back (*it);
std::string depends = task.get ("depends");
if (depends != "")
{
const std::vector <Task>& all = context.tdb.getAllPending ();
std::vector <Task>::const_iterator it;
for (it = all.begin (); it != all.end (); ++it)
if ((it->getStatus () == Task::pending ||
it->getStatus () == Task::waiting) &&
depends.find (it->get ("uuid")) != std::string::npos)
blocking.push_back (*it);
}
}
////////////////////////////////////////////////////////////////////////////////
@ -118,12 +118,10 @@ void dependencyGetBlocking (Task& task, std::vector <Task>& blocking)
// Keep walking the chain, recording the links (a --> b, b --> c, ...) until
// either the end of the chain is found (therefore not circular), or the chain
// loops and a repeat link is spotted (therefore circular).
bool dependencyIsCircular (Task& task)
bool dependencyIsCircular (const Task& task)
{
std::vector <std::string> links;
const std::vector <Task>& all = context.tdb.getAllPending ();
return followUpstream (task, task, all, links);
std::vector <std::string> seen;
return followUpstream (task, task, seen);
}
////////////////////////////////////////////////////////////////////////////////
@ -132,50 +130,27 @@ bool dependencyIsCircular (Task& task)
static bool followUpstream (
const Task& task,
const Task& original,
const std::vector <Task>& all,
std::vector <std::string>& links)
std::vector <std::string>& seen)
{
if (task.has ("depends"))
std::vector <Task> blocking;
dependencyGetBlocking (task, blocking);
foreach (b, blocking)
{
std::vector <std::string> uuids;
split (uuids, task.get ("depends"), ',');
std::string link = task.get ("uuid") + " -> " + b->get ("uuid");
std::vector <std::string>::iterator outer;
for (outer = uuids.begin (); outer != uuids.end (); ++outer)
{
// Check that link has not already been seen.
// Have we seen this link before? If so, circularity has been detected.
if (std::find (seen.begin (), seen.end (), link) != seen.end ())
return true;
// This is the actual circularity check - the rest of this function is
// just chain-walking.
std::string link = task.get ("uuid") + " -> " + *outer;
if (std::find (links.begin (), links.end (), link) != links.end ())
return true;
seen.push_back (link);
links.push_back (link);
// Recurse up the chain.
std::vector <Task>::const_iterator inner;
for (inner = all.begin (); inner != all.end (); ++inner)
{
if (*outer == inner->get ("uuid"))
{
// Use the newly modified "task", not "*inner", which is the old
// unmodified version.
if (*outer == original.get ("uuid"))
{
if (followUpstream (task, original, all, links))
return true;
}
else
{
if (followUpstream (*inner, original, all, links))
return true;
}
break;
}
}
}
// Use 'original' over '*b' if they both refer to the same task, otherwise
// '*b' is from TDB's committed list, and lacks recent modifications.
if (followUpstream (
(b->get ("uuid") == original.get ("uuid") ? original : *b),
original,
seen))
return true;
}
return false;
@ -215,35 +190,49 @@ void dependencyChainOnComplete (Task& task)
std::vector <Task> blocking;
dependencyGetBlocking (task, blocking);
std::cout << "# Task " << task.id << "\n";
foreach (t, blocking)
std::cout << "# blocking " << t->id << " " << t->get ("uuid") << "\n";
// If the task is anything but the tail end of a dependency chain.
if (blocking.size ())
{
std::vector <Task> blocked;
dependencyGetBlocked (task, blocked);
foreach (t, blocked)
std::cout << "# blocked by " << t->id << " " << t->get ("uuid") << "\n";
// Nag about broken chain.
if (context.config.getBoolean ("dependency.reminder"))
{
std::cout << "Task " << task.id << " is blocked by:\n";
foreach (b, blocking)
std::cout << " " << b->id << " " << b->get ("description") << "\n";
}
// If there are both blocking and blocked tasks, the chain is broken.
if (blocked.size ())
{
// TODO Nag about broken chain.
std::cout << "# Chain broken - offer to repair\n";
// TODO Confirm that the chain should be repaired.
// Repair the chain - everything in blocked should now depend on
// everything in blocking, instead of task.id.
foreach (left, blocked)
if (context.config.getBoolean ("dependency.reminder"))
{
left->removeDependency (task.id);
std::cout << "and is blocking:\n";
foreach (b, blocked)
std::cout << " " << b->id << " " << b->get ("description") << "\n";
}
if (!context.config.getBoolean ("dependency.confirmation") ||
confirm ("Would you like the dependency chain fixed?"))
{
// Repair the chain - everything in blocked should now depend on
// everything in blocking, instead of task.id.
foreach (left, blocked)
{
left->removeDependency (task.id);
foreach (right, blocking)
left->addDependency (right->id);
}
// Now update TDB, now that the updates have all occurred.
foreach (left, blocked)
context.tdb.update (*left);
foreach (right, blocking)
left->addDependency (right->id);
context.tdb.update (*right);
}
}
}
@ -252,27 +241,64 @@ void dependencyChainOnComplete (Task& task)
////////////////////////////////////////////////////////////////////////////////
void dependencyChainOnStart (Task& task)
{
std::stringstream out;
if (context.config.getBoolean ("dependency.reminder") /* &&
TODO check that task is actually blocked */)
if (context.config.getBoolean ("dependency.reminder"))
{
out << "# dependencyChainScan nag! "
<< task.id
<< " "
<< task.get ("uuid")
<< "\n";
std::vector <Task> blocking;
dependencyGetBlocking (task, blocking);
context.footnote (out.str ());
// If the task is anything but the tail end of a dependency chain, nag about
// broken chain.
if (blocking.size ())
{
std::cout << "Task " << task.id << " is blocked by:\n";
foreach (b, blocking)
std::cout << " " << b->id << " " << b->get ("description") << "\n";
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Iff a dependency is being removed, is there something to do.
void dependencyChainOnModify (Task& before, Task& after)
{
// TODO Iff a dependency is being removed, is there anything to do.
// TODO It is not clear that this should even happen. TBD.
/*
// Get the dependencies from before.
std::string depends = before.get ("depends");
std::vector <std::string> before_depends;
split (before_depends, depends, ',');
std::cout << "# dependencyChainOnModify before has " << before_depends.size () << "\n";
// Get the dependencies from after.
depends = after.get ("depends");
std::vector <std::string> after_depends;
split (after_depends, depends, ',');
std::cout << "# dependencyChainOnModify after has " << after_depends.size () << "\n";
// listDiff
std::vector <std::string> before_only;
std::vector <std::string> after_only;
listDiff (before_depends, after_depends, before_only, after_only);
// Any dependencies in before_only indicates that a dependency was removed.
if (before_only.size ())
{
std::cout << "# dependencyChainOnModify detected a dependency removal\n";
// before dep:2,3
// after dep:2
//
// any tasks blocked by after might should be repaired to depend on 3.
std::vector <Task> blocked;
dependencyGetBlocked (after, blocked);
foreach (b, blocked)
{
std::cout << "# dependencyChainOnModify\n";
}
}
*/
}
////////////////////////////////////////////////////////////////////////////////