def _visit_module(self, mod, is_internal, fs_location): """ Actually imports services from a module object. """ for name in dir(mod): item = getattr(mod, name) if self._should_deploy(name, item): timestamp = datetime.utcnow().isoformat() depl_info = deployment_info('ServiceStore', item, timestamp, fs_location) name = item.get_name() impl_name = item.get_impl_name() self.services[impl_name] = {} self.services[impl_name]['deployment_info'] = depl_info self.services[impl_name]['service_class'] = item si = self._get_source_code_info(mod) last_mod = datetime.fromtimestamp(getmtime(mod.__file__)) service_id, is_active, slow_threshold = self.odb.add_service( name, impl_name, is_internal, timestamp, dumps(str(depl_info)), si) self.services[impl_name]['is_active'] = is_active self.services[impl_name]['slow_threshold'] = slow_threshold self.id_to_impl_name[service_id] = impl_name self.name_to_impl_name[name] = impl_name
def _visit_class(self, mod, deployed, class_, fs_location, is_internal, service_id=None, is_active=None, slow_threshold=None): timestamp = datetime.utcnow() depl_info = dumps(deployment_info('service-store', str(class_), timestamp.isoformat(), fs_location)) name = class_.get_name() impl_name = class_.get_impl_name() set_up_class_attributes(class_, self, name) self.services[impl_name] = {} self.services[impl_name]['name'] = name self.services[impl_name]['deployment_info'] = depl_info self.services[impl_name]['service_class'] = class_ si = self._get_source_code_info(mod) if not(service_id and is_active is not None and slow_threshold): service_id, is_active, slow_threshold = self.odb.add_service( name, impl_name, is_internal, timestamp, dumps(str(depl_info)), si) deployed.append(class_) self.services[impl_name]['is_active'] = is_active self.services[impl_name]['slow_threshold'] = slow_threshold self.id_to_impl_name[service_id] = impl_name self.impl_name_to_id[impl_name] = service_id self.name_to_impl_name[name] = impl_name if has_debug: logger.debug('Imported service:`%s`', name) class_.after_add_to_store(logger)
def _visit_module(self, mod, is_internal, fs_location): """ Actually imports services from a module object. """ deployed = [] try: for name in sorted(dir(mod)): with self.update_lock: item = getattr(mod, name) if self._should_deploy(name, item): should_add = item.before_add_to_store(logger) if should_add: timestamp = datetime.utcnow() depl_info = dumps( deployment_info('service-store', str(item), timestamp.isoformat(), fs_location)) item.add_http_method_handlers() name = item.get_name() impl_name = item.get_impl_name() self.services[impl_name] = {} self.services[impl_name]['name'] = name self.services[impl_name][ 'deployment_info'] = depl_info self.services[impl_name]['service_class'] = item si = self._get_source_code_info(mod) service_id, is_active, slow_threshold = self.odb.add_service( name, impl_name, is_internal, timestamp, dumps(str(depl_info)), si) deployed.append(name) self.services[impl_name]['is_active'] = is_active self.services[impl_name][ 'slow_threshold'] = slow_threshold self.id_to_impl_name[service_id] = impl_name self.impl_name_to_id[impl_name] = service_id self.name_to_impl_name[name] = impl_name logger.debug('Imported service:[{}]'.format(name)) item.after_add_to_store(logger) else: msg = 'Skipping [{}] from [{}], should_add:[{}] is not True'.format( item, fs_location, should_add) logger.info(msg) except Exception, e: msg = 'Exception while visit mod:[{}], is_internal:[{}], fs_location:[{}], e:[{}]'.format( mod, is_internal, fs_location, format_exc(e)) logger.error(msg)
def _store_deployed_services_in_odb(self, session, batch_indexes, to_process, _utcnow=datetime.utcnow): """ Looks up all Service objects in ODB, checks if any is not deployed locally and deploys it if it is not. """ # Local objects now = _utcnow() now_iso = now.isoformat() # Get all services already deployed in ODB for comparisons (Service) - it is needed to do it again, # in addition to _store_deployed_services_in_odb, because that other method may have added # DB-level IDs that we need with our own objects. services = self.get_basic_data_services() # Same goes for deployed services objects (DeployedService) deployed_services = self.get_basic_data_deployed_services() # Modules visited may return a service that has been already visited via another module, # in which case we need to skip such a duplicate service. already_visited = set() # Add any missing DeployedService objects from each batch delineated by indexes found for start_idx, end_idx in batch_indexes: to_add = [] batch_services = to_process[start_idx:end_idx] for service in batch_services: # type: InRAMService if service.name in already_visited: continue else: already_visited.add(service.name) # At this point we wil always have IDs for all Service objects service_id = services[service.name]['id'] # Metadata about this deployment as a JSON object class_ = service.service_class path = service.source_code_info.path deployment_details = dumps(deployment_info('service-store', str(class_), now_iso, path)) # No such Service object in ODB so we need to store it if service.name not in deployed_services: to_add.append({ 'server_id': self.server.id, 'service_id': service_id, 'deployment_time': now, 'details': deployment_details, 'source': service.source_code_info.source, 'source_path': service.source_code_info.path, 'source_hash': service.source_code_info.hash, 'source_hash_method': service.source_code_info.hash_method, }) # If any services are to be deployed, do it now. if to_add: self.odb.add_deployed_services(session, to_add)
def _visit_module(self, mod, is_internal, fs_location): """ Actually imports services from a module object. """ deployed = [] try: for name in sorted(dir(mod)): with self.update_lock: item = getattr(mod, name) if self._should_deploy(name, item): should_add = item.before_add_to_store(logger) if should_add: timestamp = datetime.utcnow() depl_info = deployment_info('service-store', item, timestamp, fs_location) item.add_http_method_handlers() name = item.get_name() impl_name = item.get_impl_name() self.services[impl_name] = {} self.services[impl_name]['name'] = name self.services[impl_name]['deployment_info'] = depl_info self.services[impl_name]['service_class'] = item si = self._get_source_code_info(mod) service_id, is_active, slow_threshold = self.odb.add_service( name, impl_name, is_internal, timestamp, dumps(str(depl_info)), si) deployed.append(service_id) self.services[impl_name]['is_active'] = is_active self.services[impl_name]['slow_threshold'] = slow_threshold self.id_to_impl_name[service_id] = impl_name self.impl_name_to_id[impl_name] = service_id self.name_to_impl_name[name] = impl_name logger.debug('Imported service:[{}]'.format(name)) item.after_add_to_store(logger) else: msg = 'Skipping [{}] from [{}], should_add:[{}] is not True'.format( item, fs_location, should_add) logger.info(msg) except Exception, e: msg = 'Exception while visit mod:[{}], is_internal:[{}], fs_location:[{}], e:[{}]'.format( mod, is_internal, fs_location, format_exc(e)) logger.error(msg)