Skip to content

Commit

Permalink
add appropriate logs for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Oct 30, 2024
1 parent d94e507 commit 722c5c7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
36 changes: 25 additions & 11 deletions controllers/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os/exec"
"strings"
Expand Down Expand Up @@ -44,7 +45,7 @@ func D2DJobHandler(storeInstance *store.Store) func(http.ResponseWriter, *http.R

func ExtJsJobRunHandler(storeInstance *store.Store) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
response := JobConfigResponse{}
response := JobRunResponse{}
if r.Method != http.MethodPost {
http.Error(w, "Invalid HTTP method", http.StatusBadRequest)
}
Expand All @@ -58,6 +59,8 @@ func ExtJsJobRunHandler(storeInstance *store.Store) func(http.ResponseWriter, *h
return
}

fmt.Printf("Job started: %s\n", job.ID)

target, err := storeInstance.GetTarget(job.Target)
if err != nil {
response.Message = err.Error()
Expand All @@ -67,9 +70,11 @@ func ExtJsJobRunHandler(storeInstance *store.Store) func(http.ResponseWriter, *h
return
}

fmt.Printf("Target found: %s\n", target.Name)

cmdBuffer := new(bytes.Buffer)
cmd := exec.Command(
"proxmox-backup-client",
"/usr/bin/proxmox-backup-client",
"backup",
fmt.Sprintf("%s.pxar:%s", strings.ReplaceAll(job.Target, " ", "-"), target.Path),
"--repository",
Expand All @@ -81,6 +86,8 @@ func ExtJsJobRunHandler(storeInstance *store.Store) func(http.ResponseWriter, *h
cmd.Stdout = cmdBuffer
cmd.Stderr = cmdBuffer

fmt.Printf("Command composed: %s\n", cmd.String())

err = cmd.Start()
if err != nil {
response.Message = err.Error()
Expand All @@ -91,37 +98,43 @@ func ExtJsJobRunHandler(storeInstance *store.Store) func(http.ResponseWriter, *h
}

task := logging.Initialize(cmd.Process.Pid, job.Target, "", "")
log, err := task.GetLogger()
if err != nil {
response.Message = err.Error()
response.Status = http.StatusBadGateway
response.Success = false
json.NewEncoder(w).Encode(response)
return
}
fmt.Printf("Task initialized: %s\n", task.UPID)

job.LastRunUpid = &task.UPID
job.LastRunState = &task.Status

response.Data = task.UPID

err = storeInstance.UpdateJob(*job)
if err != nil {
fmt.Printf("error updating job: %v\n", err)
}
fmt.Printf("Updated job: %s\n", job.ID)

go func() {
fmt.Printf("Logger buffer to log writer goroutine started\n")
log, err := task.GetLogger()
if err != nil {
fmt.Printf("%s\n", err)
return
}

writer := log.Writer()
_, err := io.Copy(writer, cmdBuffer)
_, err = io.Copy(writer, cmdBuffer)
if err != nil {
fmt.Printf("log writer err: %v\n", err)
}
}()

go func() {
fmt.Printf("cmd wait goroutine started\n")
err = cmd.Wait()
if err != nil {
log.Printf("%s\n", err)
}

fmt.Printf("done waiting, closing task\n")

err = task.Close()
if err != nil {
fmt.Printf("%s\n", err)
Expand All @@ -132,6 +145,7 @@ func ExtJsJobRunHandler(storeInstance *store.Store) func(http.ResponseWriter, *h
job.LastRunState = &task.Status
job.LastRunEndtime = &endTimeUnix

fmt.Printf("Updated job: %s\n", job.ID)
err = storeInstance.UpdateJob(*job)
if err != nil {
fmt.Printf("error updating job: %v\n", err)
Expand Down
8 changes: 8 additions & 0 deletions controllers/jobs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ type JobConfigResponse struct {
Status int `json:"status"`
Success bool `json:"success"`
}

type JobRunResponse struct {
Errors map[string]string `json:"errors"`
Message string `json:"message"`
Data string `json:"data"`
Status int `json:"status"`
Success bool `json:"success"`
}
2 changes: 1 addition & 1 deletion logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (task *Task) Close() error {

activePath := filepath.Join(task.BaseDir, "active")

cmd := exec.Command("sed", "-i", fmt.Sprintf("/%s/d", task.UPID), activePath)
cmd := exec.Command("/usr/bin/sed", "-i", fmt.Sprintf("/%s/d", task.UPID), activePath)
_, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to update active: %w", err)
Expand Down

0 comments on commit 722c5c7

Please sign in to comment.