Browse Source

feat: 优化计划任务脚本执行输出 (#2637)

pull/2642/head
ssongliu 1 year ago committed by GitHub
parent
commit
e8ece0dfd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      backend/i18n/lang/en.yaml
  2. 2
      backend/i18n/lang/zh-Hant.yaml
  3. 2
      backend/i18n/lang/zh.yaml
  4. 16
      backend/utils/cmd/cmd.go

2
backend/i18n/lang/en.yaml

@ -131,4 +131,4 @@ ErrUserFindErr: "Failed to find user {{ .name }} {{ .err }}"
ErrFirewall: "No firewalld or ufw service is detected. Please check and try again!"
#cronjob
ErrBashExecute: "Script execution error, please check stderr-related information in the task output text field."
ErrBashExecute: "Script execution error, please check the specific information in the task output text area."

2
backend/i18n/lang/zh-Hant.yaml

@ -131,4 +131,4 @@ ErrUserFindErr: "用戶 {{ .name }} 查找失敗 {{ .err }}"
ErrFirewall: "當前未檢測到系統 firewalld 或 ufw 服務,請檢查後重試!"
#cronjob
ErrBashExecute: "腳本執行錯誤,請在任務輸出文本區域中查看 stderr 相關信息。"
ErrBashExecute: "腳本執行錯誤,請在任務輸出文本域中查看具體信息。"

2
backend/i18n/lang/zh.yaml

@ -131,4 +131,4 @@ ErrUserFindErr: "用户 {{ .name }} 查找失败 {{ .err }}"
ErrFirewall: "当前未检测到系统 firewalld 或 ufw 服务,请检查后重试!"
#cronjob
ErrBashExecute: "脚本执行错误,请在任务输出文本域中查看 stderr 相关信息。"
ErrBashExecute: "脚本执行错误,请在任务输出文本域中查看具体信息。"

16
backend/utils/cmd/cmd.go

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
"strings"
"time"
@ -81,24 +82,19 @@ func ExecCronjobWithTimeOut(cmdStr string, workdir string, timeout time.Duration
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
output := new(bytes.Buffer)
cmd.Stdout = io.MultiWriter(output, cmd.Stdout)
cmd.Stderr = io.MultiWriter(output, cmd.Stderr)
err := cmd.Run()
if ctx.Err() == context.DeadlineExceeded {
return "", buserr.New(constant.ErrCmdTimeout)
}
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr:\n%s", stderr.String())
err = buserr.New(constant.ErrBashExecute)
}
if len(stdout.String()) != 0 {
if len(errMsg) != 0 {
errMsg = fmt.Sprintf("%s \n\n stdout:\n%s", errMsg, stdout.String())
} else {
errMsg = fmt.Sprintf("stdout:\n %s", stdout.String())
}
}
return errMsg, err
return output.String(), err
}
func Execf(cmdStr string, a ...interface{}) (string, error) {

Loading…
Cancel
Save