'Thread main function for a Cron instance.'

def run(self):

    play_nice(19)
    self.ALIVE = True
    while (self.ALIVE and (not mailpile.util.QUITTING)):
        tasksToBeExecuted = []
        now = time.time()
        with self.lock:
            for task_spec in self.schedule.values():
                (name, interval, task, last, status) = task_spec
                if ((last + interval) <= now):
                    tasksToBeExecuted.append((name, task))
                    self.schedule[name][4] = 'scheduled'
        for (name, task) in tasksToBeExecuted:
            self.schedule[name][3] = time.time()
            self.schedule[name][4] = 'running'
            try:
                self.last_run = time.time()
                self.running = name
                task()
            except Exception as e:
                self.schedule[name][4] = 'FAILED'
                self.session.ui.error(('%s    failed    in    %s:    %s' % (name, self.name, e)))
            finally:
                self.schedule[name][4] = 'ok'
                self.last_run = time.time()
                self.running = ('Finished    %s' % self.running)
        sleepTime = self.sleep
        delay = ((time.time() - now) + sleepTime)
        while ((delay > 0) and self.ALIVE):
            if (self.sleep != sleepTime):
                delay = 0
            else:
                time.sleep(max(0, min(1, delay)))
                delay -= 1
