Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Jan 24, 2024
1 parent 1cebb20 commit 9da2b34
Show file tree
Hide file tree
Showing 26 changed files with 93 additions and 61 deletions.
4 changes: 2 additions & 2 deletions application/cmd/utils_signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func stopWebServer(i int, eng engine.Engine, exitCode int) {
if i > 0 {
err := eng.Stop()
if err != nil {
log.Error(err.Error())
log.Errorf(`failed to engine.Stop: %v`, err.Error())
}
if exitCode > 0 {
os.Exit(exitCode)
Expand All @@ -78,7 +78,7 @@ func stopWebServer(i int, eng engine.Engine, exitCode int) {
err := eng.Shutdown(context.Background())
exitedCode := exitCode
if err != nil {
log.Error(err.Error())
log.Errorf(`failed to engine.Shutdown: %v`, err.Error())
exitedCode = ExitCodeShutdownFailed
}
os.Exit(exitedCode)
Expand Down
2 changes: 1 addition & 1 deletion application/handler/cloud/backup_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func BackupConfigDelete(ctx echo.Context) error {
if err == nil {
if err = allBackupStop(m.Id); err == nil {
if rerr := cloudbackup.LevelDB().RemoveDB(m.Id); rerr != nil {
log.Error(rerr.Error())
log.Errorf(`failed to cloudbackup.LevelDB().RemoveDB(%v): %v`, m.Id, rerr.Error())
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions application/handler/cloud/backup_helper_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ func fullBackupStart(cfg dbschema.NgingCloudBackup, username string, msgType str
if fileSystem == nil {
seekReader, err = os.Open(ppath)
if err != nil {
log.Error(err)
log.Errorf(`%v: %s`, err, ppath)
return err
}
} else {
seekReader, err = fileSystem.Open(ppath)
if err != nil {
log.Error(err)
log.Errorf(`%v: %s`, err, ppath)
return err
}
}
Expand All @@ -267,7 +267,7 @@ func fullBackupStart(cfg dbschema.NgingCloudBackup, username string, msgType str
}
err = db.Put(dbKey, com.Str2bytes(strings.Join(parts, `||`)), nil)
if err != nil {
log.Error(err)
log.Errorf(`failed to db.Put(%q): %v`, dbKey, err)
}
}()
err = cloudbackup.RetryablePut(ctx, mgr, seekReader, objectName, info.Size())
Expand Down
4 changes: 2 additions & 2 deletions application/handler/cloud/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func init() {
m := model.NewCloudBackup(ctx)
_, err := m.ListByOffset(nil, nil, 0, -1, db.Cond{`disabled`: `N`})
if err != nil {
log.Error(err)
log.Errorf(`failed to query cloud_backup list: %v`, err)
return
}
for _, row := range m.Objects() {
err = monitorBackupStart(*row)
if err != nil && err != ErrNotSupportMonitor {
log.Error(err)
log.Errorf(`failed to monitorBackupStart(%+v): %v`, *row, err)
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion application/handler/manager/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func UploadByOwner(ctx echo.Context, ownerType string, ownerID uint64, readBefor
fileM := prepareData.MakeModel(ownerType, ownerID)
_, err = prepareData.SetMultiple(clientName == `default`).Save(fileM, clientName, client)
if err != nil {
log.Error(err.Error())
log.Errorf(`failed to prepareData.Save(%+v): %v`, fileM, err.Error())
return client.Response()
}
if len(pipe) > 0 {
Expand Down
8 changes: 4 additions & 4 deletions application/handler/manager/upload_crop.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,18 @@ func CropByOwner(ctx echo.Context, ownerType string, ownerID uint64) error {
defer reader.Close()
}
if err != nil {
log.Error(err)
log.Errorf(`%v: %s`, err, srcURL)
return ``
}
originMd5, err := checksum.MD5sumReader(reader)
if err != nil {
log.Error(err)
log.Errorf(`failed to checksum.MD5sumReader(avatar=%q): %v`, srcURL, err)
return ``
}
size := len(originMd5)
_, _, err = storer.Put(putFile, bytes.NewBufferString(originMd5), int64(size))
if err != nil {
log.Error(err)
log.Errorf(`failed to storer.Put(%q): %v`, putFile, err)
}
return originMd5
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func CropByOwner(ctx echo.Context, ownerType string, ownerID uint64) error {
size := len(originMd5)
_, _, err = storer.Put(putFile, bytes.NewBufferString(originMd5), int64(size))
if err != nil {
log.Error(err)
log.Errorf(`failed to storer.Put(%q): %v`, putFile, err)
}
return originMd5
}
Expand Down
2 changes: 1 addition & 1 deletion application/handler/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func Setup(ctx echo.Context) error {

// 升级
if err := Upgrade(); err != nil && os.ErrNotExist != err {
log.Error(err)
log.Errorf(`failed to Upgrade: %v`, err)
}

if ctx.IsAjax() {
Expand Down
4 changes: 2 additions & 2 deletions application/handler/user/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func init() {
userM := dbschema.NewNgingUser(ctx)
err := userM.UpdateField(nil, `online`, `Y`, `username`, user)
if err != nil {
log.Error(err)
log.Errorf(`failed to userM.UpdateField(online=Y,username=%q): %v`, user, err)
}
})
notice.OnClose(func(user string) {
ctx := defaults.NewMockContext()
userM := dbschema.NewNgingUser(ctx)
err := userM.UpdateField(nil, `online`, `N`, `username`, user)
if err != nil {
log.Error(err)
log.Errorf(`failed to userM.UpdateField(online=N,username=%q): %v`, user, err)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func profileHandler(w http.ResponseWriter, r *http.Request) {
}
token, err := Default.Server().ValidationBearerToken(r)
if err != nil {
log.Error(err)
log.Errorf(`failed to oauth2server.ValidationBearerToken: %v`, err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
Expand Down
2 changes: 1 addition & 1 deletion application/library/cloudbackup/cloudbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *Cloudbackup) OnCreate(file string) {
}
}
if err != nil {
log.Error(err)
log.Errorf(`cloudbackup onCreate: %v`, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion application/library/cloudbackup/file_chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func RecordLog(ctx echo.Context, err error, cfg *dbschema.NgingCloudBackup,
logM.Status = model.CloudBackupStatusSuccess
}
if _, err := logM.Add(); err != nil {
log.Error(err)
log.Errorf(`failed to cloudbackup.RecordLog(%+v): %v`, logM, err)
}
}
}
54 changes: 40 additions & 14 deletions application/library/config/cliconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package config

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -125,7 +126,7 @@ func (c *CLIConfig) ParseConfig() {
if IsInstalled() {
MustOK(err)
} else {
log.Error(err)
log.Errorf(`failed to ParseConfig: %v`, err)
}
}
c.ConfDir()
Expand All @@ -148,7 +149,7 @@ func (c *CLIConfig) RunStartup() {
if cm != nil {
startup.FireBefore(serverType)
if err := cm.Restart(); err != nil {
log.Error(err)
log.Errorf(`failed to %v.Restart: %v`, serverType, err)
}
startup.FireAfter(serverType)
continue
Expand All @@ -163,17 +164,16 @@ func (c *CLIConfig) CmdGroupStop(groupName string) error {
return nil
}
groupName += `.`
var err error
for key, cmd := range c.cmds {
if !strings.HasPrefix(key, groupName) {
continue
}
err := c.Kill(cmd)
if err != nil {
log.Error(err)
if err != nil && !errors.Is(err, os.ErrProcessDone) {
log.Errorf(`failed to %v.Kill: %v`, key, err)
}
}
return err
return nil
}

func (c *CLIConfig) CmdHasGroup(groupName string) bool {
Expand Down Expand Up @@ -214,15 +214,22 @@ func (c *CLIConfig) CmdStop(typeName string) error {
return nil
}
err := c.Kill(cmd)
if err != nil && errors.Is(err, os.ErrProcessDone) {
err = nil
if err != nil {
if errors.Is(err, os.ErrProcessDone) {
return nil
}
err = fmt.Errorf(`failed to cliconfig.CmdStop(%q): %v`, typeName, err)
}
return err
}

func (c *CLIConfig) CmdSendSignal(typeName string, sig os.Signal) error {
cmd := c.CmdGet(typeName)
return c.SendSignal(cmd, sig)
err := c.SendSignal(cmd, sig)
if err != nil {
err = fmt.Errorf(`failed to cliconfig.CmdSendSignal(%q, %v): %v`, typeName, sig, err)
}
return err
}

func (c *CLIConfig) SendSignal(cmd *exec.Cmd, sig os.Signal) error {
Expand All @@ -233,14 +240,25 @@ func (c *CLIConfig) SendSignal(cmd *exec.Cmd, sig os.Signal) error {
return nil
}
err := cmd.Process.Signal(sig)
if err != nil && (cmd.ProcessState == nil || cmd.ProcessState.Exited()) {
err = nil
if err != nil {
if errors.Is(err, os.ErrProcessDone) {
return nil
}
if cmd.ProcessState == nil || cmd.ProcessState.Exited() {
return nil
}
}
return err
}

func (c *CLIConfig) Kill(cmd *exec.Cmd) error {
return com.CloseProcessFromCmd(cmd)
err := com.CloseProcessFromCmd(cmd)
if err != nil {
if errors.Is(err, os.ErrProcessDone) {
return nil
}
}
return err
}

var ErrCmdNotRunning = errors.New(`command is not running`)
Expand Down Expand Up @@ -333,7 +351,11 @@ func (c *CLIConfig) Close() error {
for _, cmd := range c.cmds {
err := c.Kill(cmd)
if err != nil {
log.Error(err)
if cmd.Process != nil {
log.Errorf(`failed to cliconfig.Close(pid=%v): %v`, cmd.Process.Pid, err)
} else {
log.Errorf(`failed to cliconfig.Close(cmd=%q): %v`, cmd.String(), err)
}
}
}
return nil
Expand All @@ -343,7 +365,11 @@ func (c *CLIConfig) SendSignalToAllCmd(sig os.Signal) error {
for _, cmd := range c.cmds {
err := c.SendSignal(cmd, sig)
if err != nil {
log.Error(err)
if cmd.Process != nil {
log.Errorf(`failed to cliconfig.SendSignalToAllCmd(%v, pid=%v): %v`, sig, cmd.Process.Pid, err)
} else {
log.Errorf(`failed to cliconfig.SendSignalToAllCmd(%v, cmd=%q): %v`, sig, cmd.String(), err)
}
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions application/library/config/cliconfig_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ func (c *CLIConfig) WatchEnvConfig() {
log.Info(`No need to reload the env file: ` + file)
return
}
log.Error(err)
log.Errorf(`failed to cliconfig.InitEnviron: %v`, err)
},
}
for _, envFile := range c.envFiles {
err := c.envMonitor.AddFile(envFile)
if err != nil {
log.Error(err)
log.Errorf(`failed to envMonitor.AddFile(%q): %v`, envFile, err)
}
}
c.envMonitor.Watch()
Expand Down
4 changes: 2 additions & 2 deletions application/library/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (c *Config) Reload(newConfig *Config) error {
if newRd, ok := newExtConfig.(extend.Reloader); ok {
log.Debugf(`reload extend config(Reload): %v`, name)
if err := newRd.Reload(); err != nil {
log.Error(err)
log.Errorf(`failed to %s.Reload: %v`, name, err)
}
continue
}
Expand All @@ -245,7 +245,7 @@ func (c *Config) AsDefault() {
defaultConfigMu.Unlock()
err := c.settings.Init(nil)
if err != nil {
log.Error(err)
log.Errorf(`failed to config.settings.Init: %v`, err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions application/library/config/subconfig/ssystem/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func (sys *System) Init() {
if len(sys.MaxRequestBodySize) > 0 {
sys.maxRequestBodySizeBytes, err = ParseBytes(sys.MaxRequestBodySize)
if err != nil {
log.Error(err.Error())
log.Errorf(`failed to parse config.system.maxRequestBodySizeBytes(%q): %v`, sys.MaxRequestBodySize, err.Error())
}
}
if sys.editableFileMaxBytes < 1 && len(sys.EditableFileMaxSize) > 0 {
sys.editableFileMaxBytes, err = ParseBytes(sys.EditableFileMaxSize)
if err != nil {
log.Error(err.Error())
log.Errorf(`failed to parse config.system.editableFileMaxBytes(%q): %v`, sys.EditableFileMaxSize, err.Error())
}
}
sys.CmdTimeoutDuration = ParseTimeDuration(sys.CmdTimeout)
Expand Down
2 changes: 1 addition & 1 deletion application/library/config/subconfig/ssystem/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ParseTimeDuration(timeout string) time.Duration {
if len(timeout) > 0 {
if reNumeric.MatchString(timeout) {
if val, err := strconv.ParseUint(timeout, 10, 64); err != nil {
log.Error(err)
log.Errorf(`failed to ParseTimeDuration(%q): %v`, timeout, err)
} else {
timeoutDuration = time.Second * time.Duration(val)
}
Expand Down
5 changes: 3 additions & 2 deletions application/library/config/upgrade_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ func UpgradeDB() {
autoUpgradeDatabase()
echo.PanicIf(echo.FireByNameWithMap(`nging.upgrade.db.after`, eventParams))
installedSchemaVer = Version.DBSchema
err := os.WriteFile(filepath.Join(FromCLI().ConfDir(), LockFileName), []byte(installedTime.Format(`2006-01-02 15:04:05`)+"\n"+fmt.Sprint(Version.DBSchema)), os.ModePerm)
lockFile := filepath.Join(FromCLI().ConfDir(), LockFileName)
err := os.WriteFile(lockFile, []byte(installedTime.Format(`2006-01-02 15:04:05`)+"\n"+fmt.Sprint(Version.DBSchema)), os.ModePerm)
if err != nil {
log.Error(err)
log.Errorf(`%v: %s`, err, lockFile)
}
log.Info(`Database table upgrade completed`)
}
Expand Down
4 changes: 2 additions & 2 deletions application/library/config/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func WatchConfig(fn func(string) error) {
log.Info(`No need to reload the configuration file: ` + file)
return
}
log.Error(err)
log.Errorf(`WatchConfig.Modify: %v`, err)
},
}
me.Watch()
err := me.AddDir(FromCLI().ConfDir())
if err != nil {
log.Error(err)
log.Errorf(`WatchConfig.AddDir(%q): %v`, FromCLI().ConfDir(), err)
}
}
2 changes: 1 addition & 1 deletion application/library/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func AddJob(spec string, job *Job) bool {
}
_, err := MainCron(true).AddJob(spec, job)
if err != nil {
log.Error("AddJob: ", err.Error())
log.Errorf("failed to cron.AddJob(%q): %v", spec, err.Error())
return false
}
return true
Expand Down
Loading

0 comments on commit 9da2b34

Please sign in to comment.