65 Star 395 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
daemon.go 5.88 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2019-11-15 22:45 . update
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package server
import (
"fmt"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/admpub/goforever"
"github.com/admpub/nging/application/handler"
"github.com/admpub/nging/application/library/common"
"github.com/admpub/nging/application/library/config"
"github.com/admpub/nging/application/model"
)
func DaemonIndex(ctx echo.Context) error {
m := model.NewForeverProcess(ctx)
cond := db.Cond{}
_, err := handler.PagingWithListerCond(ctx, m, cond)
ret := handler.Err(ctx, err)
configs := m.Objects()
for _, c := range configs {
if c.Disabled == `N` {
p := config.Daemon.Child(fmt.Sprint(c.Id))
if p != nil {
c.Status = p.Status
if len(c.Error) == 0 && p.Error() != nil {
c.Error = p.Error().Error()
}
}
}
}
ctx.Set(`listData`, configs)
return ctx.Render(`server/daemon_index`, ret)
}
func DaemonAdd(ctx echo.Context) error {
user := handler.User(ctx)
var err error
m := model.NewForeverProcess(ctx)
if ctx.IsPost() {
name := ctx.Form(`name`)
if len(name) == 0 {
err = ctx.E(`名称不能为空`)
} else if y, e := m.Exists(name); e != nil {
err = e
} else if y {
err = ctx.E(`名称已经存在`)
} else {
err = ctx.MustBind(m.ForeverProcess)
}
if err == nil {
m.Uid = user.Id
_, err = m.Add()
if err == nil {
if m.Disabled == `N` {
config.AddDaemon(m.ForeverProcess, true)
}
}
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
return ctx.Redirect(handler.URLFor(`/server/daemon_index`))
}
}
} else {
id := ctx.Formx(`copyId`).Uint()
if id > 0 {
err = m.Get(nil, `id`, id)
if err == nil {
echo.StructToForm(ctx, m.ForeverProcess, ``, func(topName, fieldName string) string {
return echo.LowerCaseFirstLetter(topName, fieldName)
})
ctx.Request().Form().Set(`id`, `0`)
}
}
}
ctx.Set(`activeURL`, `/server/daemon_index`)
return ctx.Render(`server/daemon_edit`, err)
}
func DaemonEdit(ctx echo.Context) error {
var err error
id := ctx.Formx(`id`).Uint()
m := model.NewForeverProcess(ctx)
err = m.Get(nil, db.Cond{`id`: id})
disabled := m.Disabled
oldName := m.Name
if ctx.IsPost() {
name := ctx.Form(`name`)
if len(name) == 0 {
err = ctx.E(`名称不能为空`)
} else if y, e := m.Exists2(name, id); e != nil {
err = e
} else if y {
err = ctx.E(`名称已经存在`)
} else {
err = ctx.MustBind(m.ForeverProcess, echo.ExcludeFieldName(`created`, `uid`, `lastrun`))
}
if err == nil {
m.Id = id
err = m.Edit(nil, db.Cond{`id`: id})
if err == nil {
if oldName != m.Name {
config.Daemon.StopChild(m.Name)
config.AddDaemon(m.ForeverProcess, true)
} else if disabled != m.Disabled {
if m.Disabled == `N` {
config.AddDaemon(m.ForeverProcess, true)
} else {
config.Daemon.StopChild(fmt.Sprint(m.Id))
}
}
}
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
return ctx.Redirect(handler.URLFor(`/server/daemon_index`))
}
}
} else if ctx.IsAjax() {
setDisabled := ctx.Query(`disabled`)
if len(setDisabled) > 0 {
m.Disabled = setDisabled
data := ctx.Data()
err = m.Edit(nil, db.Cond{`id`: id})
if err != nil {
data.SetError(err)
return ctx.JSON(data)
}
procsName := fmt.Sprint(m.Id)
if disabled != m.Disabled {
if m.Disabled == `N` {
procs := config.AddDaemon(m.ForeverProcess)
<-goforever.RunProcess(procsName, procs)
err = procs.Error()
if err != nil {
return ctx.JSON(data.SetError(err))
}
} else {
config.Daemon.StopChild(procsName)
}
}
data.SetInfo(ctx.T(`操作成功`))
procs := config.Daemon.Child(procsName)
if procs != nil {
data.SetData(procs.Status)
} else {
data.SetData(`idle`)
}
return ctx.JSON(data)
}
}
if err == nil {
echo.StructToForm(ctx, m.ForeverProcess, ``, func(topName, fieldName string) string {
return echo.LowerCaseFirstLetter(topName, fieldName)
})
}
ctx.Set(`activeURL`, `/server/daemon_index`)
return ctx.Render(`server/daemon_edit`, err)
}
func DaemonDelete(ctx echo.Context) error {
id := ctx.Formx(`id`).Uint()
m := model.NewForeverProcess(ctx)
err := m.Get(nil, db.Cond{`id`: id})
if err == nil {
err = m.Delete(nil, db.Cond{`id`: id})
}
if err == nil {
config.Daemon.StopChild(fmt.Sprint(m.Id))
}
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
} else {
handler.SendFail(ctx, err.Error())
}
return ctx.Redirect(handler.URLFor(`/server/daemon_index`))
}
func DaemonRestart(ctx echo.Context) error {
config.RestartDaemon()
data := ctx.Data()
data.SetInfo(ctx.T(`操作成功`))
return ctx.JSON(data)
}
func DaemonLog(ctx echo.Context) error {
typ := ctx.Form(`type`)
id := ctx.Formx(`id`).Uint()
if id < 1 {
return ctx.JSON(ctx.Data().SetError(ctx.E(`id无效`)))
}
var err error
m := model.NewForeverProcess(ctx)
err = m.Get(nil, db.Cond{`id`: id})
if err != nil {
if err == db.ErrNoMoreRows {
err = ctx.E(`不存在id为%d的任务`)
}
return ctx.JSON(ctx.Data().SetError(err))
}
var logFile string
switch typ {
case `error`:
logFile = m.Errfile
default:
logFile = m.Logfile
}
return common.LogShow(ctx, logFile, echo.H{`title`: m.Name})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.1.1

搜索帮助