def get(self, name): with self._lock: params = self.conn_params[name] if params.is_active: timeout = float(params.timeout) if params.timeout else _GLOBAL_DEFAULT_TIMEOUT return FTPFacade(params.host, params.user, params.get('password'), params.acct, timeout, int(params.port), params.dircache) else: raise Inactive(params.name)
def invoke_by_impl_name(self, impl_name, payload='', channel=CHANNEL.INVOKE, data_format=DATA_FORMAT.DICT, transport=None, serialize=False, as_bunch=False, timeout=None, raise_timeout=True, **kwargs): """ Invokes a service synchronously by its implementation name (full dotted Python name). """ if self.component_enabled_target_matcher: orig_impl_name = impl_name impl_name, target = self.extract_target(impl_name) # It's possible we are being invoked through self.invoke or self.invoke_by_id target = target or kwargs.get('target', '') if not self._worker_store.target_matcher.is_allowed(target): raise ZatoException(self.cid, 'Invocation target `{}` not allowed ({})'.format(target, orig_impl_name)) if self.component_enabled_invoke_matcher: if not self._worker_store.invoke_matcher.is_allowed(impl_name): raise ZatoException(self.cid, 'Service `{}` (impl_name) cannot be invoked'.format(impl_name)) if self.impl_name == impl_name: msg = 'A service cannot invoke itself, name:[{}]'.format(self.name) self.logger.error(msg) raise ZatoException(self.cid, msg) service, is_active = self.server.service_store.new_instance(impl_name) if not is_active: raise Inactive(service.get_name()) set_response_func = kwargs.pop('set_response_func', service.set_response_data) invoke_args = (set_response_func, service, payload, channel, data_format, transport, self.server, self.broker_client, self._worker_store, kwargs.pop('cid', self.cid), self.request.simple_io_config) kwargs.update({'serialize':serialize, 'as_bunch':as_bunch}) try: if timeout: try: g = spawn(self.update_handle, *invoke_args, **kwargs) return g.get(block=True, timeout=timeout) except Timeout: g.kill() logger.warn('Service `%s` timed out (%s)', service.name, self.cid) if raise_timeout: raise else: out = self.update_handle(*invoke_args, **kwargs) if kwargs.get('skip_response_elem') and hasattr(out, 'keys'): keys = list(iterkeys(out)) response_elem = keys[0] return out[response_elem] else: return out except Exception: logger.warn('Could not invoke `%s`, e:`%s`', service.name, format_exc()) raise
def __getitem__(self, name, enforce_is_active=True): """ Checks out the connection pool. If enforce_is_active is False, the pool's is_active flag will be ignored. """ with self._lock: if enforce_is_active: wrapper = self.wrappers[name] if wrapper.config['is_active']: return wrapper raise Inactive(name) else: return self.wrappers[name]
def __getitem__(self, name): item = self._conn_store.get(name) if not item: msg = 'No such connection `{}` in `{}`'.format(name, sorted(self._conn_store.sessions)) logger.warn(msg) raise KeyError(msg) if not item.config.is_active: msg = 'Connection `{}` is not active'.format(name) logger.warn(msg) raise Inactive(msg) return item
def get(self, name, skip_inactive=False): item = self._conn_store.get(name) if not item: msg = 'No such item `{}` in `{}`'.format( name, sorted(self._conn_store.items)) logger.warn(msg) raise KeyError(msg) if not item.config.is_active and not skip_inactive: msg = '`{}` is inactive'.format(name) logger.warn(msg) raise Inactive(msg) return item
def get(self, name): with self._lock: params = self.conn_params[name] if params.is_active: timeout = float(params.timeout) if params.timeout else 180 # Python 2 vs. Python 3 builds of Zato have different versions # of the 'fs' dependency which in turn has a different API to its __init__ method # which is why 'dircache' cannot be used with Python 3. init_params = [params.host, params.user, params.get('password'), params.acct, timeout, int(params.port)] if PY2: init_params.append(params.dircache) return FTPFacade(*init_params) else: raise Inactive(params.name)
def _enforce_is_active(self): if not self.config['is_active']: raise Inactive(self.config['name'])