From 4f9fc21ec114834b6df020f994e0f2aaeee1e737 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 23 Dec 2014 15:43:30 +0800 Subject: [PATCH 1/4] . --- output/outputs.go | 21 ++------------------- session/users.go | 2 +- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/output/outputs.go b/output/outputs.go index 0377625..f4d1726 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -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("Session [%s] 's running [id=%d, file=%s] has done [stdout %v], ", 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/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) } } } From 991cd7f60685f84ff925c0e615b5c988b36e2925 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 23 Dec 2014 16:03:09 +0800 Subject: [PATCH 2/4] reporting --- session/sessions.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/session/sessions.go b/session/sessions.go index d675b9e..300e1eb 100644 --- a/session/sessions.go +++ b/session/sessions.go @@ -27,6 +27,7 @@ import ( "encoding/json" "net/http" "os" + "sort" "strconv" "sync" "time" @@ -126,23 +127,23 @@ func (u *userReport) report() string { // FixedTimeReport reports the Wide sessions status periodically (10 minutes). func FixedTimeReport() { go func() { - for _ = range time.Tick(10 * time.Minute) { - users := map[string]*userReport{} // + for _ = range time.Tick(10 * time.Second) { + 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. From 243ddd63c43a0803a9f2569b6f0b5fb5e48a0bab Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 23 Dec 2014 16:15:45 +0800 Subject: [PATCH 3/4] reporting --- session/sessions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/session/sessions.go b/session/sessions.go index 300e1eb..5714ff8 100644 --- a/session/sessions.go +++ b/session/sessions.go @@ -127,7 +127,7 @@ func (u *userReport) report() string { // FixedTimeReport reports the Wide sessions status periodically (10 minutes). func FixedTimeReport() { go func() { - for _ = range time.Tick(10 * time.Second) { + for _ = range time.Tick(10 * time.Minute) { users := userReports{} processSum := 0 From 4e37625c93dd83f8aa6dbe4579ba3e9453c3ca1a Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 23 Dec 2014 17:25:00 +0800 Subject: [PATCH 4/4] logging --- output/outputs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/output/outputs.go b/output/outputs.go index f4d1726..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 %v], ", sid, runningId, filePath, err) + 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"