Skip to content

Commit b932c05

Browse files
committed
Merge branch 'run_command_method'
2 parents ce0729d + 578cc1c commit b932c05

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

batchspawner/batchspawner.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,6 @@
3434
from jupyterhub.spawner import set_user_setuid
3535
import jupyterhub
3636

37-
@gen.coroutine
38-
def run_command(cmd, input=None, env=None):
39-
proc = Subprocess(cmd, shell=True, env=env, stdin=Subprocess.STREAM, stdout=Subprocess.STREAM,stderr=Subprocess.STREAM)
40-
inbytes = None
41-
if input:
42-
inbytes = input.encode()
43-
try:
44-
yield proc.stdin.write(inbytes)
45-
except StreamClosedError as exp:
46-
# Apparently harmless
47-
pass
48-
proc.stdin.close()
49-
out = yield proc.stdout.read_until_close()
50-
eout = yield proc.stderr.read_until_close()
51-
proc.stdout.close()
52-
proc.stderr.close()
53-
eout = eout.decode().strip()
54-
try:
55-
err = yield proc.wait_for_exit()
56-
except CalledProcessError:
57-
#self.log.error("Subprocess returned exitcode %s" % proc.returncode)
58-
#self.log.error(eout)
59-
raise RuntimeError(eout)
60-
if err != 0:
61-
return err # exit error?
62-
else:
63-
out = out.decode().strip()
64-
return out
6537

6638
class BatchSpawnerBase(Spawner):
6739
"""Base class for spawners using resource manager batch job submission mechanisms
@@ -177,6 +149,35 @@ def parse_job_id(self, output):
177149
def cmd_formatted_for_batch(self):
178150
return ' '.join(self.cmd + self.get_args())
179151

152+
@gen.coroutine
153+
def run_command(self, cmd, input=None, env=None):
154+
proc = Subprocess(cmd, shell=True, env=env, stdin=Subprocess.STREAM, stdout=Subprocess.STREAM,stderr=Subprocess.STREAM)
155+
inbytes = None
156+
if input:
157+
inbytes = input.encode()
158+
try:
159+
yield proc.stdin.write(inbytes)
160+
except StreamClosedError as exp:
161+
# Apparently harmless
162+
pass
163+
proc.stdin.close()
164+
out = yield proc.stdout.read_until_close()
165+
eout = yield proc.stderr.read_until_close()
166+
proc.stdout.close()
167+
proc.stderr.close()
168+
eout = eout.decode().strip()
169+
try:
170+
err = yield proc.wait_for_exit()
171+
except CalledProcessError:
172+
self.log.error("Subprocess returned exitcode %s" % proc.returncode)
173+
self.log.error(eout)
174+
raise RuntimeError(eout)
175+
if err != 0:
176+
return err # exit error?
177+
else:
178+
out = out.decode().strip()
179+
return out
180+
180181
@gen.coroutine
181182
def submit_batch_script(self):
182183
subvars = self.get_req_subvars()
@@ -187,7 +188,7 @@ def submit_batch_script(self):
187188
script = self.batch_script.format(**subvars)
188189
self.log.info('Spawner submitting job using ' + cmd)
189190
self.log.info('Spawner submitted script:\n' + script)
190-
out = yield run_command(cmd, input=script, env=self.get_env())
191+
out = yield self.run_command(cmd, input=script, env=self.get_env())
191192
try:
192193
self.log.info('Job submitted. cmd: ' + cmd + ' output: ' + out)
193194
self.job_id = self.parse_job_id(out)
@@ -213,7 +214,7 @@ def read_job_state(self):
213214
cmd = self.batch_query_cmd.format(**subvars)
214215
self.log.debug('Spawner querying job: ' + cmd)
215216
try:
216-
out = yield run_command(cmd)
217+
out = yield self.run_command(cmd)
217218
self.job_status = out
218219
except Exception as e:
219220
self.log.error('Error querying job ' + self.job_id)
@@ -231,7 +232,7 @@ def cancel_batch_job(self):
231232
subvars['job_id'] = self.job_id
232233
cmd = self.batch_cancel_cmd.format(**subvars)
233234
self.log.info('Cancelling job ' + self.job_id + ': ' + cmd)
234-
yield run_command(cmd)
235+
yield self.run_command(cmd)
235236

236237
def load_state(self, state):
237238
"""load job_id from state"""

0 commit comments

Comments
 (0)