Merge branch 'master' of https://github.com/b3log/wide
This commit is contained in:
commit
1372fbd88c
|
@ -150,7 +150,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
defer util.Recover()
|
defer util.Recover()
|
||||||
defer cmd.Wait()
|
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
|
// push once for front-end to get the 'run' state and pid
|
||||||
if nil != wsChannel {
|
if nil != wsChannel {
|
||||||
|
@ -183,7 +183,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// remove the exited process from user process set
|
// remove the exited process from user process set
|
||||||
processes.remove(wSession, cmd.Process)
|
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 {
|
if nil != wsChannel {
|
||||||
channelRet["cmd"] = "run-done"
|
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)
|
||||||
buf = strings.Replace(buf, ">", ">", -1)
|
buf = strings.Replace(buf, ">", ">", -1)
|
||||||
|
|
||||||
if nil != err {
|
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"] = "<span class='stderr'>" + buf + "</span>"
|
|
||||||
err := wsChannel.WriteJSON(&channelRet)
|
|
||||||
if nil != err {
|
|
||||||
logger.Error(err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
wsChannel.Refresh()
|
|
||||||
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
channelRet["cmd"] = "run"
|
channelRet["cmd"] = "run"
|
||||||
channelRet["output"] = "<span class='stderr'>" + buf + "</span>"
|
channelRet["output"] = "<span class='stderr'>" + buf + "</span>"
|
||||||
err := wsChannel.WriteJSON(&channelRet)
|
err := wsChannel.WriteJSON(&channelRet)
|
||||||
|
|
|
@ -27,6 +27,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -127,22 +128,22 @@ func (u *userReport) report() string {
|
||||||
func FixedTimeReport() {
|
func FixedTimeReport() {
|
||||||
go func() {
|
go func() {
|
||||||
for _ = range time.Tick(10 * time.Minute) {
|
for _ = range time.Tick(10 * time.Minute) {
|
||||||
users := map[string]*userReport{} // <username, *userReport>
|
users := userReports{}
|
||||||
processSum := 0
|
processSum := 0
|
||||||
|
|
||||||
for _, s := range WideSessions {
|
for _, s := range WideSessions {
|
||||||
processCnt := len(s.Processes)
|
processCnt := len(s.Processes)
|
||||||
processSum += processCnt
|
processSum += processCnt
|
||||||
|
|
||||||
if report, exists := users[s.Username]; exists {
|
if report, exists := contains(users, s.Username); exists {
|
||||||
if s.Updated.After(report.updated) {
|
if s.Updated.After(report.updated) {
|
||||||
users[s.Username].updated = s.Updated
|
report.updated = s.Updated
|
||||||
}
|
}
|
||||||
|
|
||||||
report.sessionCnt++
|
report.sessionCnt++
|
||||||
report.processCnt += processCnt
|
report.processCnt += processCnt
|
||||||
} else {
|
} 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 [" +
|
buf.WriteString("\n [" + strconv.Itoa(len(users)) + "] users, [" + strconv.Itoa(processSum) + "] running processes and [" +
|
||||||
strconv.Itoa(len(WideSessions)) + "] sessions currently\n")
|
strconv.Itoa(len(WideSessions)) + "] sessions currently\n")
|
||||||
|
|
||||||
|
sort.Sort(users)
|
||||||
|
|
||||||
for _, t := range users {
|
for _, t := range users {
|
||||||
buf.WriteString(" " + t.report() + "\n")
|
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.
|
// WSHandler handles request of creating session channel.
|
||||||
//
|
//
|
||||||
// When a channel closed, releases all resources associated with it.
|
// When a channel closed, releases all resources associated with it.
|
||||||
|
|
|
@ -277,7 +277,7 @@ func FixedTimeSave() {
|
||||||
|
|
||||||
for _, u := range users {
|
for _, u := range users {
|
||||||
if u.Save() {
|
if u.Save() {
|
||||||
logger.Tracef("Saved online user [%s]'s configurations")
|
logger.Tracef("Saved online user [%s]'s configurations", u.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue