2929from tornado .iostream import StreamClosedError
3030
3131from jupyterhub .spawner import Spawner
32+ from jupyterhub .traitlets import Command
3233from traitlets import (
3334 Integer , Unicode , Float , Dict , default
3435)
@@ -73,6 +74,9 @@ class BatchSpawnerBase(Spawner):
7374 state_gethost
7475 """
7576
77+ # override default since will need to set the listening port using the api
78+ cmd = Command (['batchspawner-singleuser' ], allow_none = True ).tag (config = True )
79+
7680 # override default since batch systems typically need longer
7781 start_timeout = Integer (300 ).tag (config = True )
7882
@@ -164,6 +168,9 @@ def _req_keepvars_default(self):
164168 # Will get the address of the server as reported by job manager
165169 current_ip = Unicode ()
166170
171+ # Will get the port of the server as reported by singleserver
172+ current_port = Integer ()
173+
167174 # Prepare substitution variables for templates using req_xyz traits
168175 def get_req_subvars (self ):
169176 reqlist = [ t for t in self .trait_names () if t .startswith ('req_' ) ]
@@ -342,14 +349,9 @@ def poll(self):
342349 @gen .coroutine
343350 def start (self ):
344351 """Start the process"""
345- if self .user and self .user .server and self .user .server .port :
346- self .port = self .user .server .port
347- self .db .commit ()
348- elif (jupyterhub .version_info < (0 ,7 ) and not self .user .server .port ) or (
349- jupyterhub .version_info >= (0 ,7 ) and not self .port
350- ):
351- self .port = random_port ()
352- self .db .commit ()
352+ if jupyterhub .version_info >= (0 ,8 ) and self .server :
353+ self .server .port = self .port
354+
353355 job = yield self .submit_batch_script ()
354356
355357 # We are called with a timeout, and if the timeout expires this function will
@@ -374,16 +376,19 @@ def start(self):
374376 yield gen .sleep (self .startup_poll_interval )
375377
376378 self .current_ip = self .state_gethost ()
379+ while self .current_port == 0 :
380+ yield gen .sleep (self .startup_poll_interval )
381+
377382 if jupyterhub .version_info < (0 ,7 ):
378383 # store on user for pre-jupyterhub-0.7:
379- self .user .server .port = self .port
384+ self .user .server .port = self .current_port
380385 self .user .server .ip = self .current_ip
381386 self .db .commit ()
382387 self .log .info ("Notebook server job {0} started at {1}:{2}" .format (
383- self .job_id , self .current_ip , self .port )
388+ self .job_id , self .current_ip , self .current_port )
384389 )
385390
386- return self .current_ip , self .port
391+ return self .current_ip , self .current_port
387392
388393 @gen .coroutine
389394 def stop (self , now = False ):
0 commit comments