Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39534
1
###
2
Manage Jupyter server
3
###
4
5
async = require('async')
6
winston = require('winston')
7
misc_node = require('smc-util-node/misc_node')
8
misc = require('smc-util/misc')
9
message = require('smc-util/message')
10
11
# Either give valid information about the port, etc., or cb(undefined, {status:'stopped'}).
12
# Never actually returns error as first output.
13
jupyter_status = (cb) ->
14
misc_node.execute_code
15
command : "smc-jupyter"
16
args : ['status']
17
err_on_exit : true
18
bash : false
19
timeout : 20
20
ulimit_timeout : false # very important -- so doesn't kill consoles after 60 seconds cputime!
21
cb : (err, out) ->
22
if err
23
cb(undefined, {"status": "stopped"})
24
else
25
try
26
cb(undefined, misc.from_json(out.stdout))
27
catch e
28
cb(undefined, {"status": "stopped"})
29
30
jupyter_start = (mathjax, cb) ->
31
misc_node.execute_code
32
command : "smc-jupyter"
33
args : ['start', mathjax]
34
err_on_exit : true
35
bash : false
36
timeout : 60
37
ulimit_timeout : false # very important -- so doesn't kill consoles after 60 seconds cputime!
38
cb : (err, out) ->
39
if not err
40
# do some checks on the output to make sure things really worked.
41
try
42
status = misc.from_json(out.stdout)
43
if not status?.port
44
err = "unable to start -- no port; status=#{misc.to_json(out)}"
45
catch e
46
err = "error parsing smc-jupyter startup output -- #{e}, {misc.to_json(out)}"
47
cb(err, status)
48
49
jupyter_port_queue = []
50
exports.jupyter_port = (socket, mesg) ->
51
dbg = (m) -> winston.debug("jupyter_port: #{m}")
52
dbg()
53
jupyter_port_queue.push({socket:socket, mesg:mesg})
54
# fallback during upgrade (TODO remove this)
55
mathjax = mesg.mathjax_url ? "/static/mathjax/MathJax.js"
56
if jupyter_port_queue.length > 1
57
dbg("already #{jupyter_port_queue.length} requests -- return immediately")
58
return
59
status = undefined
60
async.series([
61
(cb) ->
62
dbg("checking jupyter status")
63
jupyter_status (err, _status) ->
64
status = _status
65
dbg("got status=#{misc.to_json(status)}")
66
cb(err)
67
(cb) ->
68
if status?.port
69
dbg("already running; nothing more to do")
70
cb()
71
return
72
dbg("not running, so start it running")
73
jupyter_start mathjax, (err, _status) ->
74
status = _status
75
dbg("after starting, got status=#{misc.to_json(status)}")
76
cb(err)
77
], (err) ->
78
dbg("finished; (err=#{err}); now sending resp")
79
for x in jupyter_port_queue
80
if err
81
resp = message.error
82
id : x.mesg.id
83
error : "error starting jupyter -- #{err}"
84
else
85
resp = message.jupyter_port
86
id : x.mesg.id
87
port : status.port
88
x.socket.write_mesg('json', resp)
89
jupyter_port_queue = []
90
)
91