def confirm_experiment(self, reservation_id, lab_session_id, initial_configuration, exp_info): self._remove_expired_reservations() weblab_reservation_pqueue = WEBLAB_RESOURCE_RESERVATION_PQUEUE % ( self.resource_type_name, reservation_id) client = self.redis_maker() pqueue_reservation_data_str = client.get(weblab_reservation_pqueue) if pqueue_reservation_data_str is None: return pqueue_reservation_data = json.loads(pqueue_reservation_data_str) resource_instance_str = pqueue_reservation_data.get(RESOURCE_INSTANCE) if resource_instance_str is not None: resource_instance = Resource.parse(resource_instance_str) if not self.resources_manager.check_working(resource_instance): # TODO: if the experiment is broken and the student is ACTIVE_STATUS, something should be done # return pqueue_reservation_data[LAB_SESSION_ID] = lab_session_id.id pqueue_reservation_data[INITIAL_CONFIGURATION] = initial_configuration pqueue_reservation_data[TIMESTAMP_AFTER] = self.time_provider.get_time( ) pqueue_reservation_data[ACTIVE_STATUS] = STATUS_RESERVED pqueue_reservation_data[EXP_INFO] = json.dumps(exp_info) pqueue_reservation_data_str = json.dumps(pqueue_reservation_data) client.set(weblab_reservation_pqueue, pqueue_reservation_data_str)
def confirm_experiment(self, reservation_id, lab_session_id, initial_configuration, exp_info): self._remove_expired_reservations() weblab_reservation_pqueue = WEBLAB_RESOURCE_RESERVATION_PQUEUE % (self.resource_type_name, reservation_id) client = self.redis_maker() pqueue_reservation_data_str = client.get(weblab_reservation_pqueue) if pqueue_reservation_data_str is None: return pqueue_reservation_data = json.loads(pqueue_reservation_data_str) resource_instance_str = pqueue_reservation_data.get(RESOURCE_INSTANCE) if resource_instance_str is not None: resource_instance = Resource.parse(resource_instance_str) if not self.resources_manager.check_working(resource_instance): # TODO: if the experiment is broken and the student is ACTIVE_STATUS, something should be done # return pqueue_reservation_data[LAB_SESSION_ID] = lab_session_id.id pqueue_reservation_data[INITIAL_CONFIGURATION] = initial_configuration pqueue_reservation_data[TIMESTAMP_AFTER] = self.time_provider.get_time() pqueue_reservation_data[ACTIVE_STATUS] = STATUS_RESERVED pqueue_reservation_data[EXP_INFO] = json.dumps(exp_info) pqueue_reservation_data_str = json.dumps(pqueue_reservation_data) client.set(weblab_reservation_pqueue, pqueue_reservation_data_str)
def _clean_current_reservation(self, reservation_id): client = self.redis_maker() enqueue_free_experiment_args = None if reservation_id is not None: weblab_reservation_pqueue = WEBLAB_RESOURCE_RESERVATION_PQUEUE % (self.resource_type_name, reservation_id) reservation_data_str = client.get(weblab_reservation_pqueue) if reservation_data_str is not None: downgraded = self.reservations_manager.downgrade_confirmation(reservation_id) if downgraded: reservation_data = json.loads(reservation_data_str) resource_instance_str = reservation_data.get(RESOURCE_INSTANCE) if resource_instance_str is not None: resource_instance = Resource.parse(resource_instance_str) weblab_resource_pqueue_instance_reservations = WEBLAB_RESOURCE_PQUEUE_INSTANCE_RESERVATIONS % (resource_instance.resource_type, resource_instance.resource_instance) client.srem(weblab_resource_pqueue_instance_reservations, reservation_id) # print "RELEASING AT _clean_current_reservation. SHOULD NEVER HAPPEN." # self.resources_manager.release_resource(resource_instance) lab_session_id = reservation_data.get(LAB_SESSION_ID) experiment_instance_str = reservation_data.get(EXPERIMENT_INSTANCE) experiment_instance_id = ExperimentInstanceId.parse(experiment_instance_str) if experiment_instance_id is not None: # If the experiment instance doesn't exist, there is no need to call the free_experiment method lab_coord_address = reservation_data.get(LAB_COORD) enqueue_free_experiment_args = (lab_coord_address, reservation_id, lab_session_id, experiment_instance_id) # otherwise the student has been removed return enqueue_free_experiment_args
def _clean_current_reservation(self, reservation_id): client = self.redis_maker() enqueue_free_experiment_args = None if reservation_id is not None: weblab_reservation_pqueue = WEBLAB_RESOURCE_RESERVATION_PQUEUE % (self.resource_type_name, reservation_id) reservation_data_str = client.get(weblab_reservation_pqueue) if reservation_data_str is not None: downgraded = self.reservations_manager.downgrade_confirmation(reservation_id) if downgraded: reservation_data = json.loads(reservation_data_str) resource_instance_str = reservation_data.get(RESOURCE_INSTANCE) if resource_instance_str is not None: resource_instance = Resource.parse(resource_instance_str) weblab_resource_pqueue_instance_reservations = WEBLAB_RESOURCE_PQUEUE_INSTANCE_RESERVATIONS % (resource_instance.resource_type, resource_instance.resource_instance) client.srem(weblab_resource_pqueue_instance_reservations, reservation_id) self.resources_manager.release_resource(resource_instance) lab_session_id = reservation_data.get(LAB_SESSION_ID) experiment_instance_str = reservation_data.get(EXPERIMENT_INSTANCE) experiment_instance_id = ExperimentInstanceId.parse(experiment_instance_str) if experiment_instance_id is not None: # If the experiment instance doesn't exist, there is no need to call the free_experiment method lab_coord_address = reservation_data.get(LAB_COORD) enqueue_free_experiment_args = (lab_coord_address, reservation_id, lab_session_id, experiment_instance_id) # otherwise the student has been removed return enqueue_free_experiment_args
def list_laboratories_addresses(self): client = self._redis_maker() laboratory_addresses = { # laboratory_coord_address : { # experiment_instance_id : resource_instance # } } for experiment_type in client.smembers(WEBLAB_EXPERIMENT_TYPES): experiment_id = ExperimentId.parse(experiment_type) experiment_instance_names = client.smembers( WEBLAB_EXPERIMENT_INSTANCES % experiment_type) for experiment_instance_name in experiment_instance_names: experiment_instance_id = ExperimentInstanceId( experiment_instance_name, experiment_id.exp_name, experiment_id.cat_name) weblab_experiment_instance = WEBLAB_EXPERIMENT_INSTANCE % ( experiment_type, experiment_instance_name) laboratory_address = client.hget(weblab_experiment_instance, LAB_COORD) resource_str = client.hget(weblab_experiment_instance, RESOURCE_INST) resource = Resource.parse(resource_str) current = laboratory_addresses.get(laboratory_address, {}) current[experiment_instance_id] = resource laboratory_addresses[laboratory_address] = current return laboratory_addresses
def get_resource_instance_by_experiment_instance_id(self, experiment_instance_id): experiment_id = experiment_instance_id.to_experiment_id() weblab_experiment_instance = WEBLAB_EXPERIMENT_INSTANCE % (experiment_id.to_weblab_str(), experiment_instance_id.inst_name) client = self._redis_maker() resource_instance = client.hget(weblab_experiment_instance, RESOURCE_INST) if resource_instance is None: raise CoordExc.ExperimentNotFoundError("Experiment not found: %s" % experiment_instance_id) return Resource.parse(resource_instance)
def get_resource_instance_by_experiment_instance_id(self, experiment_instance_id): experiment_id = experiment_instance_id.to_experiment_id() weblab_experiment_instance = WEBLAB_EXPERIMENT_INSTANCE % (experiment_id.to_weblab_str(), experiment_instance_id.inst_name) client = self._redis_maker() resource_instance = client.hget(weblab_experiment_instance, RESOURCE_INST) if resource_instance is None: raise CoordExc.ExperimentNotFoundError("Experiment not found: %s" % experiment_instance_id) return Resource.parse(resource_instance)
def remove_resource_instance_id(self, experiment_instance_id): client = self._redis_maker() experiment_id_str = experiment_instance_id.to_experiment_id().to_weblab_str() weblab_experiment_instances = WEBLAB_EXPERIMENT_INSTANCES % experiment_id_str weblab_experiment_instance = WEBLAB_EXPERIMENT_INSTANCE % (experiment_id_str, experiment_instance_id.inst_name) resource_instance = client.hget(weblab_experiment_instance, RESOURCE_INST) if resource_instance is not None: # else it does not exist resource = Resource.parse(resource_instance) weblab_resource_experiment_instances = WEBLAB_RESOURCE_INSTANCE_EXPERIMENTS % (resource.resource_type, resource.resource_instance) client.srem(weblab_experiment_instances, experiment_instance_id.inst_name) client.delete(weblab_experiment_instance) client.srem(weblab_resource_experiment_instances, experiment_instance_id.to_weblab_str())
def remove_resource_instance_id(self, experiment_instance_id): client = self._redis_maker() experiment_id_str = experiment_instance_id.to_experiment_id().to_weblab_str() weblab_experiment_instances = WEBLAB_EXPERIMENT_INSTANCES % experiment_id_str weblab_experiment_instance = WEBLAB_EXPERIMENT_INSTANCE % (experiment_id_str, experiment_instance_id.inst_name) resource_instance = client.hget(weblab_experiment_instance, RESOURCE_INST) if resource_instance is not None: # else it does not exist resource = Resource.parse(resource_instance) weblab_resource_experiment_instances = WEBLAB_RESOURCE_INSTANCE_EXPERIMENTS % (resource.resource_type, resource.resource_instance) client.srem(weblab_experiment_instances, experiment_instance_id.inst_name) client.delete(weblab_experiment_instance) client.srem(weblab_resource_experiment_instances, experiment_instance_id.to_weblab_str())
def list_laboratories_addresses(self): client = self._redis_maker() laboratory_addresses = { # laboratory_coord_address : { # experiment_instance_id : resource_instance # } } for experiment_type in client.smembers(WEBLAB_EXPERIMENT_TYPES): experiment_id = ExperimentId.parse(experiment_type) experiment_instance_names = client.smembers(WEBLAB_EXPERIMENT_INSTANCES % experiment_type) for experiment_instance_name in experiment_instance_names: experiment_instance_id = ExperimentInstanceId(experiment_instance_name, experiment_id.exp_name, experiment_id.cat_name) weblab_experiment_instance = WEBLAB_EXPERIMENT_INSTANCE % (experiment_type, experiment_instance_name) laboratory_address = client.hget(weblab_experiment_instance, LAB_COORD) resource_str = client.hget(weblab_experiment_instance, RESOURCE_INST) resource = Resource.parse(resource_str) current = laboratory_addresses.get(laboratory_address, {}) current[experiment_instance_id] = resource laboratory_addresses[laboratory_address] = current return laboratory_addresses