Restructure periodic cleanup

Get lists of node and image ids at the start of the cleanup,
then iterate over them one at a time, starting a new db session
for each.  This should reduce the incidence of errors due to the
db changing from under this long-running process.

Also, catch exceptions for each node or image, and continue working
through the list.

Change-Id: I2688b13cdb3885fabdcde4626c74d1bcd9bd4660