add some re-usable APIs

define some pending/ready helpers as static constants on orm.Spawner

allows treating orm.Spawner the same as Spawner wrappers,
as long as `.active` etc. checks are performed first
This commit is contained in:
Min RK
2018-09-17 16:45:19 +02:00
parent cbe4095533
commit 9031b9aa57
3 changed files with 41 additions and 0 deletions

View File

@@ -135,6 +135,16 @@ class User(Base):
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(Unicode(255), unique=True) name = Column(Unicode(255), unique=True)
# properties on the spawner wrapper
# some APIs get these low-level objects
# when the spawner isn't running,
# for which these should all be False
active = running = ready = False
pending = None
@property
def orm_spawner(self):
return self
_orm_spawners = relationship( _orm_spawners = relationship(
"Spawner", "Spawner",
backref="user", backref="user",

View File

@@ -133,6 +133,10 @@ class Spawner(LoggingConfigurable):
proxy_spec = Unicode() proxy_spec = Unicode()
@property
def last_activity(self):
return self.orm_spawner.last_activity
@property @property
def server(self): def server(self):
if hasattr(self, '_server'): if hasattr(self, '_server'):

View File

@@ -182,6 +182,26 @@ class User:
await self.save_auth_state(auth_state) await self.save_auth_state(auth_state)
return auth_state return auth_state
def all_spawners(self, include_default=True):
"""Generator yielding all my spawners
including those that are not running.
Spawners that aren't running will be low-level orm.Spawner objects,
while those that are will be higher-level Spawner wrapper objects.
"""
for name, orm_spawner in sorted(self.orm_user.orm_spawners.items()):
if name == '' and not include_default:
continue
if name in self.spawners:
# yield wrapper if it exists
yield self.spawners[name]
else:
# otherwise, yield low-level object
yield orm_spawner
def _new_spawner(self, server_name, spawner_class=None, **kwargs): def _new_spawner(self, server_name, spawner_class=None, **kwargs):
"""Create a new spawner""" """Create a new spawner"""
if spawner_class is None: if spawner_class is None:
@@ -321,6 +341,13 @@ class User:
else: else:
return self.base_url return self.base_url
def server_url(self, server_name=''):
"""Get the url for a server with a given name"""
if not server_name:
return self.url
else:
return url_path_join(self.url, server_name)
def progress_url(self, server_name=''): def progress_url(self, server_name=''):
"""API URL for progress endpoint for a server with a given name""" """API URL for progress endpoint for a server with a given name"""
url_parts = [self.settings['hub'].base_url, 'api/users', self.escaped_name] url_parts = [self.settings['hub'].base_url, 'api/users', self.escaped_name]