Getting code ready for production
I am currently doing the production-ready pass through the Rhino DivanDB code base, and I thought that this change was interesting enough to post about:
public void Execute()
{
while(context.DoWork)
{
bool foundWork = false;
transactionalStorage.Batch(actions =>
{
var task = actions.GetFirstTask();
if(task == null)
{
actions.Commit();
return;
}
foundWork = true;
task.Execute(context);
actions.CompleteCurrentTask();
actions.Commit();
});
if(foundWork == false)
context.WaitForWork();
}
}
This is “just get things working” phase. When getting a piece of code ready for production, I am looking for several things:
- If this is running in production, and I get the log file, will I be able to understand what is going on?
- Should this code handle any exceptions?
- What happens if I send values from a previous version? From a future version?
- Am I doing unbounded operations?
- For error handling, can I avoid any memory allocations?
The result for this piece of code was:
public void Execute()
{
while(context.DoWork)
{
bool foundWork = false;
transactionalStorage.Batch(actions =>
{
var taskAsJson = actions.GetFirstTask();
if (taskAsJson == null)
{
actions.Commit();
return;
}
log.DebugFormat("Executing {0}", taskAsJson);
foundWork = true;
Task task;
try
{
task = Task.ToTask(taskAsJson);
try
{
task.Execute(context);
}
catch (Exception e)
{
if (log.IsWarnEnabled)
{
log.Warn(string.Format("Task {0} has failed and was deleted without completing any work", taskAsJson), e);
}
}
}
catch (Exception e)
{
log.Error("Could not create instance of a task from " + taskAsJson, e);
}
actions.CompleteCurrentTask();
actions.Commit();
});
if(foundWork == false)
context.WaitForWork();
}
}
The code size blows up really quickly.