This commit is contained in:
Liang Ding 2017-05-04 18:05:01 +08:00
parent 81f3bce1c5
commit 4893c974bc
2 changed files with 34 additions and 15 deletions

23
main.go
View File

@ -23,8 +23,10 @@ import (
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal"
"runtime" "runtime"
"strings" "strings"
"syscall"
"time" "time"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
@ -72,14 +74,11 @@ func init() {
} }
i18n.Load() i18n.Load()
event.Load() event.Load()
conf.Load(*confPath, *confIP, *confPort, *confServer, *confLogLevel, *confStaticServer, *confContext, *confChannel, conf.Load(*confPath, *confIP, *confPort, *confServer, *confLogLevel, *confStaticServer, *confContext, *confChannel,
*confPlayground, *confDocker, *confUsersWorkspaces) *confPlayground, *confDocker, *confUsersWorkspaces)
conf.FixedTimeCheckEnv() conf.FixedTimeCheckEnv()
session.FixedTimeSave() session.FixedTimeSave()
session.FixedTimeRelease() session.FixedTimeRelease()
@ -96,6 +95,7 @@ func main() {
runtime.GOMAXPROCS(conf.Wide.MaxProcs) runtime.GOMAXPROCS(conf.Wide.MaxProcs)
initMime() initMime()
handleSignal()
// IDE // IDE
http.HandleFunc(conf.Wide.Context+"/", handlerGzWrapper(indexHandler)) http.HandleFunc(conf.Wide.Context+"/", handlerGzWrapper(indexHandler))
@ -242,7 +242,6 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
logger.Debugf("User [%s] has [%d] sessions", username, len(wideSessions)) logger.Debugf("User [%s] has [%d] sessions", username, len(wideSessions))
t, err := template.ParseFiles("views/index.html") t, err := template.ParseFiles("views/index.html")
if nil != err { if nil != err {
logger.Error(err) logger.Error(err)
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
@ -253,6 +252,22 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
t.Execute(w, model) t.Execute(w, model)
} }
// handleSignal handles system signal for graceful shutdown.
func handleSignal() {
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
s := <-c
logger.Tracef("Got signal [%s]", s)
session.SaveOnlineUsers()
logger.Tracef("Saved all online user, exit")
os.Exit(0)
}()
}
// serveSingle registers the handler function for the given pattern and filename. // serveSingle registers the handler function for the given pattern and filename.
func serveSingle(pattern string, filename string) { func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {

View File

@ -70,11 +70,11 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
tmpLinux := user.GoBuildArgsForLinux tmpLinux := user.GoBuildArgsForLinux
tmpWindows := user.GoBuildArgsForWindows tmpWindows := user.GoBuildArgsForWindows
tmpDarwin := user.GoBuildArgsForDarwin tmpDarwin := user.GoBuildArgsForDarwin
user.GoBuildArgsForLinux = strings.Replace(user.GoBuildArgsForLinux, `"`, `&quot;`, -1) user.GoBuildArgsForLinux = strings.Replace(user.GoBuildArgsForLinux, `"`, `&quot;`, -1)
user.GoBuildArgsForWindows = strings.Replace(user.GoBuildArgsForWindows, `"`, `&quot;`, -1) user.GoBuildArgsForWindows = strings.Replace(user.GoBuildArgsForWindows, `"`, `&quot;`, -1)
user.GoBuildArgsForDarwin = strings.Replace(user.GoBuildArgsForDarwin, `"`, `&quot;`, -1) user.GoBuildArgsForDarwin = strings.Replace(user.GoBuildArgsForDarwin, `"`, `&quot;`, -1)
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(user.Locale), "user": user, model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(user.Locale), "user": user,
"ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version(), "ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version(),
"locales": i18n.GetLocalesNames(), "gofmts": util.Go.GetGoFormats(), "locales": i18n.GetLocalesNames(), "gofmts": util.Go.GetGoFormats(),
@ -85,7 +85,7 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
if nil != err { if nil != err {
logger.Error(err) logger.Error(err)
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
user.GoBuildArgsForLinux = tmpLinux user.GoBuildArgsForLinux = tmpLinux
user.GoBuildArgsForWindows = tmpWindows user.GoBuildArgsForWindows = tmpWindows
user.GoBuildArgsForDarwin = tmpDarwin user.GoBuildArgsForDarwin = tmpDarwin
@ -93,7 +93,7 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
} }
t.Execute(w, model) t.Execute(w, model)
user.GoBuildArgsForLinux = tmpLinux user.GoBuildArgsForLinux = tmpLinux
user.GoBuildArgsForWindows = tmpWindows user.GoBuildArgsForWindows = tmpWindows
user.GoBuildArgsForDarwin = tmpDarwin user.GoBuildArgsForDarwin = tmpDarwin
@ -308,13 +308,7 @@ func FixedTimeSave() {
defer util.Recover() defer util.Recover()
for _ = range time.Tick(time.Minute) { for _ = range time.Tick(time.Minute) {
users := getOnlineUsers() SaveOnlineUsers()
for _, u := range users {
if u.Save() {
logger.Tracef("Saved online user [%s]'s configurations", u.Name)
}
}
} }
}() }()
} }
@ -335,6 +329,16 @@ func CanAccess(username, path string) bool {
return false return false
} }
// SaveOnlineUsers saves online users' configurations at once.
func SaveOnlineUsers() {
users := getOnlineUsers()
for _, u := range users {
if u.Save() {
logger.Tracef("Saved online user [%s]'s configurations", u.Name)
}
}
}
func getOnlineUsers() []*conf.User { func getOnlineUsers() []*conf.User {
ret := []*conf.User{} ret := []*conf.User{}