diff --git a/output/outputs.go b/output/outputs.go index 0377625..23516ae 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -150,7 +150,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { defer util.Recover() defer cmd.Wait() - logger.Debugf("Session [%s] is running [id=%d, file=%s]", sid, runningId, filePath) + logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.Username, sid, runningId, filePath) // push once for front-end to get the 'run' state and pid if nil != wsChannel { @@ -183,7 +183,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { // remove the exited process from user process set processes.remove(wSession, cmd.Process) - logger.Tracef("Session [%s] 's running [id=%d, file=%s] has done [stdout err]", sid, runningId, filePath) + logger.Tracef("User [%s, %s] 's running [id=%d, file=%s] has done [stdout %v], ", wSession.Username, sid, runningId, filePath, err) if nil != wsChannel { channelRet["cmd"] = "run-done" @@ -227,24 +227,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { buf = strings.Replace(buf, "<", "<", -1) buf = strings.Replace(buf, ">", ">", -1) - if nil != err { - // remove the exited process from user process set - processes.remove(wSession, cmd.Process) - - logger.Tracef("Session [%s] 's running [id=%d, file=%s] has done [stderr err]", sid, runningId, filePath) - - channelRet["cmd"] = "run-done" - channelRet["output"] = "" + buf + "" - err := wsChannel.WriteJSON(&channelRet) - if nil != err { - logger.Error(err) - break - } - - wsChannel.Refresh() - - break - } else { + if nil == err { channelRet["cmd"] = "run" channelRet["output"] = "" + buf + "" err := wsChannel.WriteJSON(&channelRet) diff --git a/session/sessions.go b/session/sessions.go index d675b9e..5714ff8 100644 --- a/session/sessions.go +++ b/session/sessions.go @@ -27,6 +27,7 @@ import ( "encoding/json" "net/http" "os" + "sort" "strconv" "sync" "time" @@ -127,22 +128,22 @@ func (u *userReport) report() string { func FixedTimeReport() { go func() { for _ = range time.Tick(10 * time.Minute) { - users := map[string]*userReport{} // + users := userReports{} processSum := 0 for _, s := range WideSessions { processCnt := len(s.Processes) processSum += processCnt - if report, exists := users[s.Username]; exists { + if report, exists := contains(users, s.Username); exists { if s.Updated.After(report.updated) { - users[s.Username].updated = s.Updated + report.updated = s.Updated } report.sessionCnt++ report.processCnt += processCnt } else { - users[s.Username] = &userReport{username: s.Username, sessionCnt: 1, processCnt: processCnt, updated: s.Updated} + users = append(users, &userReport{username: s.Username, sessionCnt: 1, processCnt: processCnt, updated: s.Updated}) } } @@ -150,6 +151,8 @@ func FixedTimeReport() { buf.WriteString("\n [" + strconv.Itoa(len(users)) + "] users, [" + strconv.Itoa(processSum) + "] running processes and [" + strconv.Itoa(len(WideSessions)) + "] sessions currently\n") + sort.Sort(users) + for _, t := range users { buf.WriteString(" " + t.report() + "\n") } @@ -159,6 +162,22 @@ func FixedTimeReport() { }() } +func contains(reports []*userReport, username string) (*userReport, bool) { + for _, ur := range reports { + if username == ur.username { + return ur, true + } + } + + return nil, false +} + +type userReports []*userReport + +func (f userReports) Len() int { return len(f) } +func (f userReports) Swap(i, j int) { f[i], f[j] = f[j], f[i] } +func (f userReports) Less(i, j int) bool { return f[i].processCnt > f[j].processCnt } + // WSHandler handles request of creating session channel. // // When a channel closed, releases all resources associated with it. diff --git a/session/users.go b/session/users.go index 2d15abc..036239c 100644 --- a/session/users.go +++ b/session/users.go @@ -277,7 +277,7 @@ func FixedTimeSave() { for _, u := range users { if u.Save() { - logger.Tracef("Saved online user [%s]'s configurations") + logger.Tracef("Saved online user [%s]'s configurations", u.Name) } } }