Add error handling for database failure.

Addresses #889

When an error was being thrown by the database thread it wasn't being picked up by the scheduler.

This change adds checking to the scheduler to make sure the thread is still alive and a means of passing back exceptions from the rundb thread.

Some minor refactoring has been made to facilitate the change.