Skip to content

Commit

Permalink
Add option to remove named servers to server culling
Browse files Browse the repository at this point in the history
  • Loading branch information
betatim committed Jan 27, 2020
1 parent d8eda10 commit b230ef8
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions jupyterhub/files/hub/cull_idle_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ def format_td(td):


@coroutine
def cull_idle(url, api_token, inactive_limit, cull_users=False, max_age=0, concurrency=10):
def cull_idle(
url,
api_token,
inactive_limit,
cull_users=False,
remove_named_servers=False,
max_age=0,
concurrency=10,
):
"""Shutdown idle single-user servers
If cull_users, inactive *users* will be deleted as well.
Expand Down Expand Up @@ -168,16 +176,24 @@ def handle_server(user, server_name, server):
log_name, format_td(age), format_td(inactive))
return False

body = None
if server_name:
# culling a named server
delete_url = url + "/users/%s/servers/%s"%(
quote(user['name']), quote(server['name'])
delete_url = url + "/users/%s/servers/%s" % (
quote(user['name']),
quote(server['name']),
)
if remove_named_servers:
body = json.dumps({"remove": True})
else:
delete_url = url + '/users/%s/server' % quote(user['name'])

req = HTTPRequest(
url=delete_url, method='DELETE', headers=auth_header,
url=delete_url,
method='DELETE',
headers=auth_header,
body=body,
allow_nonstandard_methods=True,
)
resp = yield fetch(req)
if resp.code == 202:
Expand Down Expand Up @@ -302,7 +318,11 @@ def handle_user(user):
help="The maximum age (in seconds) of servers that should be culled even if they are active")
define('cull_users', default=False,
help="""Cull users in addition to servers.
This is for use in temporary-user cases such as tmpnb.""",
This is for use in temporary-user cases such as BinderHub.""",
)
define('remove_named_servers', default=False,
help="""Remove named servers in addition to stopping them.
This is for use in temporary-user cases such as Binderhub.""",
)
define('concurrency', default=10,
help="""Limit the number of concurrent requests made to the Hub.
Expand Down Expand Up @@ -332,6 +352,7 @@ def handle_user(user):
api_token=api_token,
inactive_limit=options.timeout,
cull_users=options.cull_users,
remove_named_servers=options.remove_named_servers,
max_age=options.max_age,
concurrency=options.concurrency,
)
Expand Down

0 comments on commit b230ef8

Please sign in to comment.