运行的进程状态维护
This commit is contained in:
parent
75ee2b9c89
commit
c516a7e289
2
main.go
2
main.go
|
@ -24,7 +24,7 @@ import (
|
|||
func init() {
|
||||
// 默认启动参数
|
||||
flag.Set("logtostderr", "true")
|
||||
flag.Set("v", "1")
|
||||
flag.Set("v", "3")
|
||||
flag.Parse()
|
||||
|
||||
// 加载事件处理
|
||||
|
|
|
@ -32,7 +32,7 @@ type Notification struct {
|
|||
}
|
||||
|
||||
// 通知通道.
|
||||
// <sid, util.WSChannel>
|
||||
// <sid, *util.WSChannel>
|
||||
var notificationWSs = map[string]*util.WSChannel{}
|
||||
|
||||
// 用户事件处理:将事件转为通知,并通过通知通道推送给前端.
|
||||
|
|
|
@ -21,7 +21,7 @@ import (
|
|||
)
|
||||
|
||||
// 输出通道.
|
||||
// <sid, util.WSChannel>
|
||||
// <sid, *util.WSChannel>
|
||||
var outputWS = map[string]*util.WSChannel{}
|
||||
|
||||
// 建立输出通道.
|
||||
|
@ -83,9 +83,18 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
reader := io.MultiReader(stdout, stderr)
|
||||
|
||||
cmd.Start()
|
||||
if err := cmd.Start(); nil != err {
|
||||
glog.Error(err)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 添加到用户进程集中
|
||||
processes.add(sid, cmd.Process)
|
||||
|
||||
channelRet := map[string]interface{}{}
|
||||
channelRet["pid"] = cmd.Process.Pid
|
||||
|
||||
go func(runningId int) {
|
||||
glog.V(3).Infof("Session [%s] is running [id=%d, file=%s]", sid, runningId, filePath)
|
||||
|
@ -95,16 +104,33 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
|
|||
count, err := reader.Read(buf)
|
||||
|
||||
if nil != err || 0 == count {
|
||||
glog.V(3).Infof("Session [%s] 's running [id=%d, file=%s] has done", sid, runningId, filePath)
|
||||
// 从用户进程集中移除这个执行完毕的进程
|
||||
processes.remove(sid, cmd.Process)
|
||||
|
||||
break
|
||||
} else {
|
||||
channelRet["output"] = string(buf[:count])
|
||||
channelRet["cmd"] = "run"
|
||||
glog.V(3).Infof("Session [%s] 's running [id=%d, file=%s] has done", sid, runningId, filePath)
|
||||
|
||||
if nil != outputWS[sid] {
|
||||
wsChannel := outputWS[sid]
|
||||
|
||||
channelRet["cmd"] = "run-done"
|
||||
channelRet["output"] = string(buf[:count])
|
||||
err := wsChannel.Conn.WriteJSON(&channelRet)
|
||||
if nil != err {
|
||||
glog.Error(err)
|
||||
break
|
||||
}
|
||||
|
||||
// 更新通道最近使用时间
|
||||
wsChannel.Time = time.Now()
|
||||
}
|
||||
|
||||
break
|
||||
} else {
|
||||
if nil != outputWS[sid] {
|
||||
wsChannel := outputWS[sid]
|
||||
|
||||
channelRet["cmd"] = "run"
|
||||
channelRet["output"] = string(buf[:count])
|
||||
err := wsChannel.Conn.WriteJSON(&channelRet)
|
||||
if nil != err {
|
||||
glog.Error(err)
|
||||
|
@ -206,7 +232,12 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
|||
if data["succ"].(bool) {
|
||||
reader := io.MultiReader(stdout, stderr)
|
||||
|
||||
cmd.Start()
|
||||
if err := cmd.Start(); nil != err {
|
||||
glog.Error(err)
|
||||
data["succ"] = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
go func(runningId int) {
|
||||
glog.V(3).Infof("Session [%s] is building [id=%d, file=%s]", sid, runningId, filePath)
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package output
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
// 所有用户正在运行的程序进程集.
|
||||
// <sid, []*os.Process>
|
||||
type procs map[string][]*os.Process
|
||||
|
||||
var processes = procs{}
|
||||
|
||||
// 添加用户执行进程.
|
||||
func (procs *procs) add(sid string, proc *os.Process) {
|
||||
userProcesses := (*procs)[sid]
|
||||
|
||||
userProcesses = append(userProcesses, proc)
|
||||
(*procs)[sid] = userProcesses
|
||||
|
||||
glog.V(3).Infof("Session [%s] has [%d] processes", sid, len((*procs)[sid]))
|
||||
}
|
||||
|
||||
// 移除用户执行进程.
|
||||
func (procs *procs) remove(sid string, proc *os.Process) {
|
||||
userProcesses := (*procs)[sid]
|
||||
|
||||
var newProcesses []*os.Process
|
||||
for i, p := range userProcesses {
|
||||
if p.Pid == proc.Pid {
|
||||
newProcesses = append(userProcesses[:i], userProcesses[i+1:]...)
|
||||
(*procs)[sid] = newProcesses
|
||||
|
||||
glog.V(3).Infof("Session [%s] has [%d] processes", sid, len((*procs)[sid]))
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 结束用户正在执行的进程.
|
||||
func (procs *procs) kill(sid string, pid int) {
|
||||
pros := (*procs)[sid]
|
||||
|
||||
for _, p := range pros {
|
||||
if p.Pid == pid {
|
||||
if err := p.Kill(); nil != err {
|
||||
glog.Error("Kill a process [pid=%d] of session [%s] failed [error=%v]", pid, sid, err)
|
||||
} else {
|
||||
glog.V(3).Infof("Killed a process [pid=%d] of session [%s]", pid, sid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ import (
|
|||
)
|
||||
|
||||
// Shell 通道.
|
||||
// <sid, util.WSChannel>>
|
||||
// <sid, *util.WSChannel>>
|
||||
var shellWS = map[string]*util.WSChannel{}
|
||||
|
||||
// Shell 首页.
|
||||
|
|
|
@ -11,28 +11,6 @@ outputWS.onmessage = function(e) {
|
|||
goLintFound = [];
|
||||
}
|
||||
|
||||
if ('run' === data.cmd) {
|
||||
$('#output').text($('#output').text() + data.output);
|
||||
} else if ('build' === data.cmd || 'go install' === data.cmd) {
|
||||
$('#output').text(data.output);
|
||||
|
||||
if (0 !== data.output.length) { // 说明编译有错误输出
|
||||
for (var i = 0; i < data.lints.length; i++) {
|
||||
var lint = data.lints[i];
|
||||
|
||||
goLintFound.push({from: CodeMirror.Pos(lint.lineNo, 0),
|
||||
to: CodeMirror.Pos(lint.lineNo, 0),
|
||||
message: lint.msg, severity: lint.severity});
|
||||
}
|
||||
}
|
||||
|
||||
// 触发一次 gutter lint
|
||||
CodeMirror.signal(wide.curEditor, "change", wide.curEditor);
|
||||
} else if ('go get' === data.cmd || 'go install' === data.cmd) {
|
||||
$('#output').text($('#output').text() + data.output);
|
||||
}
|
||||
|
||||
if ('build' === data.cmd) {
|
||||
if ('run' === data.nextCmd) {
|
||||
var request = {
|
||||
executable: data.executable
|
||||
|
@ -51,6 +29,30 @@ outputWS.onmessage = function(e) {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
if ('run' === data.cmd) { // 正在运行
|
||||
$('#output').text($('#output').text() + data.output);
|
||||
} else if ('run-done' === data.cmd) { // 运行结束
|
||||
// TODO: 运行结束后修改 [构建&运行] 图标状态为可用状态
|
||||
} else if ('build' === data.cmd || 'go install' === data.cmd) {
|
||||
$('#output').text(data.output);
|
||||
|
||||
if (0 !== data.output.length) { // 说明编译有错误输出
|
||||
for (var i = 0; i < data.lints.length; i++) {
|
||||
var lint = data.lints[i];
|
||||
|
||||
goLintFound.push({from: CodeMirror.Pos(lint.lineNo, 0),
|
||||
to: CodeMirror.Pos(lint.lineNo, 0),
|
||||
message: lint.msg, severity: lint.severity});
|
||||
}
|
||||
|
||||
// TODO: 修改 [构建&运行] 图标状态为可用状态
|
||||
}
|
||||
|
||||
// 触发一次 gutter lint
|
||||
CodeMirror.signal(wide.curEditor, "change", wide.curEditor);
|
||||
} else if ('go get' === data.cmd || 'go install' === data.cmd) {
|
||||
$('#output').text($('#output').text() + data.output);
|
||||
}
|
||||
};
|
||||
outputWS.onclose = function (e) {
|
||||
|
@ -116,12 +118,15 @@ var wide = {
|
|||
exit: function () {
|
||||
// TODO: exit
|
||||
},
|
||||
// 构建 & 运行.
|
||||
run: function () {
|
||||
var request = {
|
||||
file: $(".edit-header .current span:eq(0)").attr("title"),
|
||||
code: wide.curEditor.getValue()
|
||||
};
|
||||
|
||||
// TODO: 修改 [构建&运行] 图标状态为不可用状态
|
||||
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: '/build',
|
||||
|
|
Loading…
Reference in New Issue