diff options
Diffstat (limited to 'lorry-controller')
-rwxr-xr-x | lorry-controller | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/lorry-controller b/lorry-controller index 2a18d8c..89e20d8 100755 --- a/lorry-controller +++ b/lorry-controller @@ -90,7 +90,8 @@ class LorryController(cliapp.Application): logging.debug("Delete any old lorries...") for dead_lorry in prev_lorries - cur_lorries: logging.debug("Dead lorry: %s" % dead_lorry) - should_delete = mgr.lorry_state[dead_lorry]['conf']['delete'] + conf_uuid = mgr.lorry_state[dead_lorry]['conf'] + should_delete = self.conf.configs[conf_uuid]['delete'] # TODO: also handle 'unchanged' if should_delete == "always": logging.debug("TODO: Delete from Trove") @@ -100,8 +101,9 @@ class LorryController(cliapp.Application): logging.debug("Create any new lorries...") for new_lorry in cur_lorries - prev_lorries: logging.debug("New lorry: %s" % new_lorry) - conf = self.conf.configs[new_lorry] lorry = self.conf.lorries[new_lorry] + conf_uuid = lorry['controller-uuid'] + conf = self.conf.configs[conf_uuid] nextdue = self.conf.duetimes[new_lorry] should_create = conf['create'] == "always" store_state = True @@ -113,7 +115,7 @@ class LorryController(cliapp.Application): store_state = False if store_state: mgr.lorry_state[new_lorry] = { - 'conf': conf, + 'conf': conf_uuid, 'lorry': lorry, 'next-due': nextdue, } @@ -124,16 +126,21 @@ class LorryController(cliapp.Application): # 3. For every lorry we have, update the settings if necessary. # and reset the next-due as appropriate. logging.debug("Update active lorry configurations...") - for lorry in cur_lorries: - if mgr.lorry_state[lorry]['lorry'] != self.conf.lorries[lorry]: - conf = self.conf.configs[new_lorry] - lorry = self.conf.lorries[new_lorry] - nextdue = self.conf.duetimes[new_lorry] - mgr.lorry_state[new_lorry] = { - 'conf': conf, + updated_count = 0 + for upd_lorry in cur_lorries: + if mgr.lorry_state[upd_lorry]['lorry'] != \ + self.conf.lorries[upd_lorry]: + lorry = self.conf.lorries[upd_lorry] + conf_uuid = lorry['controller-uuid'] + nextdue = self.conf.duetimes[upd_lorry] + mgr.lorry_state[upd_lorry] = { + 'conf': conf_uuid, 'lorry': lorry, 'next-due': nextdue, } + updated += 1 + logging.debug("Result: %d/%d lorries needed updating" % ( + updated_count, len(cur_lorries))) # 3. Iterate all active lorries and see if they're due logging.debug("Iterate active lorries looking for work...") @@ -141,21 +148,35 @@ class LorryController(cliapp.Application): lorried = 0 earliest_due = None what_early_due = "" + lorries_to_run = [] for lorry in cur_lorries: state = mgr.lorry_state[lorry] + conf_uuid = state['conf'] + conf = self.conf.configs[conf_uuid] due = state['next-due'] if now >= due: - logging.debug("Running Lorry for: %s" % lorry) - with mgr.runner(lorry) as runner: - runner.run_lorry(*self.lorrycmd) - while state['next-due'] <= now: - state['next-due'] += state['conf']['interval-parsed'] - lorried += 1 + lorries_to_run.append(lorry) + lorries_to_run.sort() + for lorry in lorries_to_run: + state = mgr.lorry_state[lorry] + conf_uuid = state['conf'] + conf = self.conf.configs[conf_uuid] + due = state['next-due'] + lorried += 1 + logging.debug("Running %d/%d. Lorrying: %s" % ( + lorried, len(lorries_to_run),lorry)) + with mgr.runner(lorry) as runner: + runner.run_lorry(*self.lorrycmd) + while state['next-due'] <= now: + state['next-due'] += conf['interval-parsed'] + for lorry in cur_lorries: + state = mgr.lorry_state[lorry] due = state['next-due'] if earliest_due is None or due < earliest_due: earliest_due = due what_early_due = lorry + if earliest_due is None: logging.debug("Lorried %d. No idea what's next." % lorried) else: |