+ def assign_vim(self, target_id):
+ with self.write_lock:
+ return self._assign_vim(target_id)
+
+ def _assign_vim(self, target_id):
+ if target_id not in self.vims_assigned:
+ worker_id = self.vims_assigned[target_id] = self._create_worker()
+ self.workers[worker_id].insert_task(("load_vim", target_id))
+
+ def reload_vim(self, target_id):
+ # send reload_vim to the thread working with this VIM and inform all that a VIM has been changed,
+ # this is because database VIM information is cached for threads working with SDN
+ with self.write_lock:
+ for worker in self.workers:
+ if worker and not worker.idle:
+ worker.insert_task(("reload_vim", target_id))
+
+ def unload_vim(self, target_id):
+ with self.write_lock:
+ return self._unload_vim(target_id)
+
+ def _unload_vim(self, target_id):
+ if target_id in self.vims_assigned:
+ worker_id = self.vims_assigned[target_id]
+ self.workers[worker_id].insert_task(("unload_vim", target_id))
+ del self.vims_assigned[target_id]
+
+ def check_vim(self, target_id):
+ with self.write_lock:
+ if target_id in self.vims_assigned:
+ worker_id = self.vims_assigned[target_id]
+ else:
+ worker_id = self._create_worker()
+
+ worker = self.workers[worker_id]
+ worker.insert_task(("check_vim", target_id))
+
+ def unload_unused_vims(self):
+ with self.write_lock:
+ vims_to_unload = []
+
+ for target_id in self.vims_assigned:
+ if not self.db.get_one(
+ "ro_tasks",
+ q_filter={
+ "target_id": target_id,
+ "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
+ },
+ fail_on_empty=False,
+ ):
+ vims_to_unload.append(target_id)
+
+ for target_id in vims_to_unload:
+ self._unload_vim(target_id)