def get_service_config(self, id): svc = self.datastore.get_by_id('service_definitions', id) if not svc: raise RpcException(errno.EINVAL, 'Invalid service name') if svc.get('get_config_rpc'): ret = self.dispatcher.call_sync(svc['get_config_rpc']) else: ret = ConfigNode('service.{0}'.format(svc['name']), self.configstore).__getstate__() if not ret: return return extend_dict(ret, {'type': 'service-{0}'.format(svc['name'])})
def get_service_config(self, id): svc = self.datastore.get_by_id('service_definitions', id) if not svc: raise RpcException(errno.EINVAL, 'Invalid service name') if svc.get('get_config_rpc'): ret = self.dispatcher.call_sync(svc['get_config_rpc']) else: ret = ConfigNode('service.{0}'.format(svc['name']), self.configstore).__getstate__() if not ret: return return extend_dict(ret, { 'type': 'service-{0}'.format(svc['name']) })
def query(self, filter=None, params=None): def extend(i): state, pid = get_status(self.dispatcher, i) entry = { 'id': i['id'], 'name': i['name'], 'state': state, } if pid is not None: entry['pid'] = pid entry['builtin'] = i['builtin'] return entry # Running extend sequentially might take too long due to the number of services # and `service ${name} onestatus`. To workaround that run it in parallel using gevent result = self.datastore.query('service_definitions', *(filter or []), **(params or {})) if result is None: return result single = (params or {}).get('single') if single is True: jobs = {gevent.spawn(extend, result): result} else: jobs = { gevent.spawn(extend, entry): entry for entry in result } gevent.joinall(list(jobs.keys()), timeout=15) group = gevent.pool.Group() def result(greenlet): if greenlet.value is None: entry = jobs.get(greenlet) return { 'name': entry['name'], 'state': 'UNKNOWN', 'builtin': entry['builtin'], } else: return greenlet.value result = group.map(result, jobs) result = list(map(lambda s: extend_dict(s, {'config': wrap(self.get_service_config(s['id']))}), result)) return result[0] if single is True else result
def query(self, filter=None, params=None): def extend(i): state, pid = get_status(self.dispatcher, self.datastore, i) entry = { 'id': i['id'], 'name': i['name'], 'state': state, } if pid is not None: entry['pid'] = pid entry['builtin'] = i['builtin'] return entry # Running extend sequentially might take too long due to the number of services # and `service ${name} onestatus`. To workaround that run it in parallel using gevent result = self.datastore.query('service_definitions', *(filter or [])) if result is None: return result jobs = {gevent.spawn(extend, entry): entry for entry in result} gevent.joinall(list(jobs.keys()), timeout=15) group = gevent.pool.Group() def result(greenlet): if greenlet.value is None: entry = jobs.get(greenlet) return { 'name': entry['name'], 'state': 'UNKNOWN', 'builtin': entry['builtin'], } else: return greenlet.value result = group.map(result, jobs) result = list( map( lambda s: extend_dict( s, {'config': self.get_service_config(s['id'])}), result)) return q.query(result, *(filter or []), stream=True, **(params or {}))