remove last_activity from server

and put it on individual spawners
This commit is contained in:
Min RK
2017-07-31 16:29:32 +02:00
parent 449aff1b1d
commit 275a4ce18d
3 changed files with 14 additions and 7 deletions

View File

@@ -1449,11 +1449,14 @@ class JupyterHub(Application):
routes = yield self.proxy.get_all_routes() routes = yield self.proxy.get_all_routes()
users_count = 0 users_count = 0
active_users_count = 0 active_users_count = 0
now = datetime.utcnow()
for prefix, route in routes.items(): for prefix, route in routes.items():
route_data = route['data'] route_data = route['data']
if 'user' not in route_data: if 'user' not in route_data:
# not a user route, ignore it # not a user route, ignore it
continue continue
if 'server_name' not in route_data:
continue
users_count += 1 users_count += 1
if 'last_activity' not in route_data: if 'last_activity' not in route_data:
# no last activity data (possibly proxy other than CHP) # no last activity data (possibly proxy other than CHP)
@@ -1462,13 +1465,18 @@ class JupyterHub(Application):
if user is None: if user is None:
self.log.warning("Found no user for route: %s", route) self.log.warning("Found no user for route: %s", route)
continue continue
spawner = user.orm_spawners.get(route_data['server_name'])
if spawner is None:
self.log.warning("Found no spawner for route: %s", route)
continue
try: try:
dt = datetime.strptime(route_data['last_activity'], ISO8601_ms) dt = datetime.strptime(route_data['last_activity'], ISO8601_ms)
except Exception: except Exception:
dt = datetime.strptime(route_data['last_activity'], ISO8601_s) dt = datetime.strptime(route_data['last_activity'], ISO8601_s)
user.last_activity = max(user.last_activity, dt) user.last_activity = max(user.last_activity, dt)
spawner.last_activity = max(spawner.last_activity, dt)
# FIXME: Make this configurable duration. 30 minutes for now! # FIXME: Make this configurable duration. 30 minutes for now!
if (datetime.now() - user.last_activity).total_seconds() < 30 * 60: if (now - user.last_activity).total_seconds() < 30 * 60:
active_users_count += 1 active_users_count += 1
self.statsd.gauge('users.running', users_count) self.statsd.gauge('users.running', users_count)
self.statsd.gauge('users.active', active_users_count) self.statsd.gauge('users.active', active_users_count)

View File

@@ -65,16 +65,13 @@ class Server(Base):
""" """
__tablename__ = 'servers' __tablename__ = 'servers'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
proto = Column(Unicode(15), default='http') proto = Column(Unicode(15), default='http')
ip = Column(Unicode(255), default='') # could also be a DNS name ip = Column(Unicode(255), default='') # could also be a DNS name
port = Column(Integer, default=random_port) port = Column(Integer, default=random_port)
base_url = Column(Unicode(255), default='/') base_url = Column(Unicode(255), default='/')
cookie_name = Column(Unicode(255), default='cookie') cookie_name = Column(Unicode(255), default='cookie')
# added to handle multi-server feature
last_activity = Column(DateTime, default=datetime.utcnow)
def __repr__(self): def __repr__(self):
return "<Server(%s:%s)>" % (self.ip, self.port) return "<Server(%s:%s)>" % (self.ip, self.port)
@@ -186,6 +183,8 @@ class Spawner(Base):
state = Column(JSONDict) state = Column(JSONDict)
name = Column(Unicode(512)) name = Column(Unicode(512))
last_activity = Column(DateTime, default=datetime.utcnow)
class Service(Base): class Service(Base):
"""A service run with JupyterHub """A service run with JupyterHub

View File

@@ -414,7 +414,7 @@ class User(HasTraits):
if self.state is None: if self.state is None:
self.state = {} self.state = {}
spawner.orm_spawner.state = spawner.get_state() spawner.orm_spawner.state = spawner.get_state()
self.last_activity = datetime.utcnow() self.last_activity = spawner.orm_spawner.last_activity = datetime.utcnow()
db.commit() db.commit()
spawner._waiting_for_response = True spawner._waiting_for_response = True
try: try:
@@ -468,7 +468,7 @@ class User(HasTraits):
yield spawner.stop() yield spawner.stop()
spawner.clear_state() spawner.clear_state()
spawner.orm_spawner.state = spawner.get_state() spawner.orm_spawner.state = spawner.get_state()
self.last_activity = datetime.utcnow() self.last_activity = spawner.orm_spawner.last_activity = datetime.utcnow()
# remove server entry from db # remove server entry from db
spawner.server = None spawner.server = None
if not spawner.will_resume: if not spawner.will_resume: