try
{
while (!stop)
{
// Get the start of the current period
final Time startOfPeriod = Time.now();
if (log.isTraceEnabled())
{
log.trace("Run the job: " + code.toString());
}
try
{
// Run the user's code
code.run(getLog());
}
catch (Exception e)
{
log.error(
"Unhandled exception thrown by user code in task " + name, e);
}
if (log.isTraceEnabled())
{
log.trace("Finished with job: " + code.toString());
}
// Sleep until the period is over (or not at all if it's
// already passed)
startOfPeriod.add(frequency).fromNow().sleep();
}
}
catch (Throwable t)
{
log.error("Task " + name + " terminated", t);