def update_application(self): if self.run_plan: current_backends = [bc.backend for bc in self.run_plan.backends] scheduler = Scheduler() new_backends = scheduler.find_backends(self.run_plan) if not new_backends: log.error(_("Can't update '{name}', no backend " "available").format(name=self.name)) return updated_backends = [] for backend_conf in new_backends: if backend_conf.backend in current_backends: # replace backend settings with updated version self.run_plan.update( pull__backends__backend=backend_conf.backend) self.run_plan.update(push__backends=backend_conf) updated_backends.append(backend_conf.backend) else: # add backend to run plan if not already there ApplicationRunPlan.objects( id=self.run_plan.id, backends__backend__nin=[ backend_conf.backend]).update_one( push__backends=backend_conf) log.info(_("Starting {name} on backend {backend}").format( name=self.name, backend=backend_conf.backend.name)) ApplicationFlag.objects( pending_backends__ne=backend_conf.backend, application=self, name=IsStartingFlag.name).update_one( add_to_set__pending_backends=backend_conf.backend, upsert=True) if updated_backends: ApplicationFlag.objects( application=self, name=NeedsRestartFlag.name).update_one( set__pending_backends=updated_backends, upsert=True) for backend in current_backends: if backend not in [bc.backend for bc in new_backends]: log.info(_("Stopping {name} on old backend " "{backend}").format(name=self.name, backend=backend.name)) ApplicationFlag.objects( pending_backends__ne=backend, application=self, name=NeedsStoppingFlag.name).update_one( add_to_set__pending_backends=backend, upsert=True)
def start_application(self): if self.current_package: if not self.run_plan: log.error("Trying to start '%s' without run plan" % self.name) return scheduler = Scheduler() backends = scheduler.find_backends(self.run_plan) if not backends: log.error(_("Can't start '{name}', no backend " "available").format(name=self.name)) self.run_plan.delete() return self.run_plan.update(set__backends=backends) ApplicationFlag.objects( application=self, name=IsStartingFlag.name).update_one( set__pending_backends=[b.backend for b in backends], upsert=True) # FIXME what if there are waiting stop tasks on other backends ? self.flags.filter(name=NeedsStoppingFlag.name).delete()