代码拉取完成,页面将自动刷新
/*
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 config
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/admpub/goforever"
"github.com/admpub/log"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/library/cron"
"github.com/webx-top/com"
"github.com/webx-top/echo"
)
var (
Daemon = goforever.Default
DaemonDefaultHook = DaemonCommonHook
)
func init() {
Daemon.Name = `nging`
Daemon.Debug = true
}
func DaemonCommonHook(p *goforever.Process) {
/*
if p.Status == goforever.StatusRestarted {
return
}
*/
processM := &dbschema.ForeverProcess{}
err := processM.Get(nil, nil, `id`, p.Name)
if err != nil {
log.Errorf(`Not found ForeverProcess: %v (%v)`, p.Name, err)
return
}
switch p.Status {
case goforever.StatusStarted:
processM.Lastrun = uint(time.Now().Unix())
processM.Pid = p.Pid
case goforever.StatusStopped:
processM.Pid = 0
case goforever.StatusExited:
OnExitedDaemon(processM)
processM.Pid = 0
case goforever.StatusKilled:
processM.Pid = 0
}
processM.Status = p.Status
set := echo.H{
`status`: processM.Status,
}
if p.Error() != nil {
set[`error`] = p.Error().Error()
}
err = processM.SetFields(nil, set, `id`, p.Name)
if err != nil {
log.Errorf(`Update ForeverProcess: %v (%v)`, p.Name, err)
}
}
// RunDaemon 运行值守程序
func RunDaemon() {
if !IsInstalled() {
return
}
Daemon.SetHook(goforever.StatusStarted, DaemonDefaultHook)
Daemon.SetHook(goforever.StatusStopped, DaemonDefaultHook)
Daemon.SetHook(goforever.StatusRunning, DaemonDefaultHook)
Daemon.SetHook(goforever.StatusRestarted, DaemonDefaultHook)
Daemon.SetHook(goforever.StatusExited, DaemonDefaultHook)
Daemon.SetHook(goforever.StatusKilled, DaemonDefaultHook)
processM := &dbschema.ForeverProcess{}
_, err := processM.ListByOffset(nil, nil, 0, -1, `disabled`, `N`)
if err != nil {
log.Error(err)
return
}
for _, p := range processM.Objects() {
AddDaemon(p)
}
Daemon.Run()
}
func AddDaemon(p *dbschema.ForeverProcess, run ...bool) *goforever.Process {
name := fmt.Sprint(p.Id)
procs := goforever.NewProcess(name, p.Command, ParseArgsSlice(p.Args)...)
procs.Debug = p.Debug == `Y`
procs.Delay = p.Delay
procs.Ping = p.Ping
procs.Respawn = int(p.Respawn)
procs.Env = ParseEnvSlice(p.Env)
procs.Dir = p.Workdir
procs.Errfile = p.Errfile
procs.Logfile = p.Logfile
pidFile := filepath.Join(echo.Wd(), `data/pid/daemon`)
if !com.IsDir(pidFile) {
err := os.MkdirAll(pidFile, os.ModePerm)
if err != nil {
log.Error(err)
}
}
pidFile = filepath.Join(pidFile, fmt.Sprintf(`%d.pid`, p.Id))
procs.Pidfile = goforever.Pidfile(pidFile)
Daemon.Add(name, procs, run...)
return procs
}
func ParseEnvSlice(a string) []string {
var env []string
a = strings.TrimSpace(a)
if len(a) > 0 {
for _, row := range strings.Split(a, "\n") {
row = strings.TrimSpace(row)
if len(row) > 0 {
env = append(env, row)
}
}
}
return env
}
func ParseArgsSlice(a string) []string {
var args []string
a = strings.TrimSpace(a)
if len(a) > 0 {
for _, row := range strings.Split(a, "\n") {
row = strings.TrimSpace(row)
if len(row) > 0 {
args = append(args, row)
}
}
}
return args
}
// OnExitedDaemon 当值守程序达到最大重试次数退出时
func OnExitedDaemon(processM *dbschema.ForeverProcess) {
// 发送邮件通知
if processM.EnableNotify == 0 {
return
}
user := new(dbschema.User)
if processM.Uid > 0 {
user.Get(nil, `id`, processM.Uid)
}
var ccList []string
if len(processM.NotifyEmail) > 0 {
ccList = strings.Split(processM.NotifyEmail, "\n")
for index, email := range ccList {
email = strings.TrimSpace(email)
if len(email) == 0 {
continue
}
ccList[index] = email
}
}
if len(user.Email) == 0 {
if len(ccList) == 0 {
return
}
user.Email = ccList[0]
user.Username = strings.SplitN(user.Email, `@`, 2)[0]
if len(ccList) > 1 {
ccList = ccList[1:]
}
}
title := `[Nging][进程值守警报]进程[` + processM.Name + `]已经异常退出`
content := `<h1>进程值守警报</h1><p>进程<strong>` + processM.Name + `</strong>已经于<strong>` + time.Now().Local().Format(time.RFC3339) + `</strong>异常退出,请马上处理</p>`
if err := cron.SendMail(user.Email, user.Username, title, com.Str2bytes(content), ccList...); err != nil {
log.Errorf(`发送进程值守警报失败:%v`, err)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。