Skip to content

Commit

Permalink
Merge pull request #6 from sonroyaalmerol/bugfix/task-log
Browse files Browse the repository at this point in the history
use simple array for client log instead
  • Loading branch information
sonroyaalmerol authored Dec 24, 2024
2 parents 07f6aeb + 6e143ae commit c2d381f
Showing 1 changed file with 47 additions and 68 deletions.
115 changes: 47 additions & 68 deletions internal/backend/backup/jobrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func RunBackup(job *store.Job, storeInstance *store.Store, waitChan chan struct{
srcPath = filepath.Join(srcPath, job.Subpath)

taskChan := make(chan store.Task)
logChan := make(chan string, 100)

watchCtx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -158,12 +157,25 @@ func RunBackup(job *store.Job, storeInstance *store.Store, waitChan chan struct{
return nil, fmt.Errorf("RunBackup: error creating stderr pipe -> %w", err)
}

clientLogs := []string{}
hasError := false
clientLogCtx, clientLogCancel := context.WithCancel(context.Background())

go func() {
defer close(logChan)
defer clientLogCancel()

readers := io.MultiReader(cmdStdout, cmdStderr)
scanner := bufio.NewScanner(readers)
for scanner.Scan() {
logChan <- scanner.Text()
formattedTime := time.Now().Format(time.RFC3339)
logLine := scanner.Text()
if strings.Contains(logLine, "Error: upload failed:") {
logLine = strings.Replace(logLine, "Error:", "TASK ERROR:", 1)
hasError = true
return
}

clientLogs = append(clientLogs, fmt.Sprintf("%s: %s", formattedTime, logLine))
}
if err := scanner.Err(); err != nil {
log.Printf("Log reader error: %v", err)
Expand Down Expand Up @@ -201,76 +213,11 @@ func RunBackup(job *store.Job, storeInstance *store.Store, waitChan chan struct{
return nil, fmt.Errorf("RunBackup: task not found")
}

logCtx, logCtxCancel := context.WithCancel(context.Background())
go func(ctx context.Context, task *store.Task) {
logFilePath := utils.GetTaskLogPath(task.UPID)

logFile, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Log file for task %s does not exist or cannot be opened: %v", task.UPID, err)
return
}
defer logFile.Close()
writer := bufio.NewWriter(logFile)

taskError := ""
hasLogs := false

_, err = writer.WriteString("--- proxmox-backup-client log starts here ---\n")
if err != nil {
log.Printf("Failed to write logs for task %s: %v", task.UPID, err)
return
}
writer.Flush()

for {
formattedTime := time.Now().Format(time.RFC3339)

select {
case <-ctx.Done():
if taskError == "" && hasLogs {
_, err := writer.WriteString(formattedTime + ": TASK OK")
if err != nil {
log.Printf("Failed to write logs for task %s: %v", task.UPID, err)
return
}
} else if taskError != "" {
_, err := writer.WriteString(formattedTime + ": " + taskError)
if err != nil {
log.Printf("Failed to write logs for task %s: %v", task.UPID, err)
return
}
}
writer.Flush()
return
case logLine, ok := <-logChan:
if !ok {
continue
}

hasLogs = true

if strings.Contains(logLine, "Error: upload failed:") {
taskError = strings.Replace(logLine, "Error:", "TASK ERROR:", 1)
continue
}

_, err := writer.WriteString(formattedTime + ": " + logLine + "\n")
if err != nil {
log.Printf("Failed to write logs for task %s: %v", task.UPID, err)
return
}
writer.Flush()
}
}
}(logCtx, task)

go func(currJob *store.Job, currTask *store.Task) {
defer func() {
if waitChan != nil {
close(waitChan)
}
logCtxCancel()
}()

syslogger, err := syslog.InitializeLogger()
Expand Down Expand Up @@ -313,6 +260,38 @@ func RunBackup(job *store.Job, storeInstance *store.Store, waitChan chan struct{
syslogger.Errorf("RunBackup (goroutine): unable to update job -> %v", err)
return
}

syslogger.Info("Waiting for client logging goroutine to finish...")
<-clientLogCtx.Done()

logFilePath := utils.GetTaskLogPath(currTask.UPID)

logFile, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
syslogger.Errorf("Log file for task %s does not exist or cannot be opened: %v", task.UPID, err)
return
}
defer logFile.Close()
writer := bufio.NewWriter(logFile)

_, err = writer.WriteString("--- proxmox-backup-client log starts here ---\n")
if err != nil {
syslogger.Errorf("Failed to write logs for task %s: %v", task.UPID, err)
return
}

if !hasError {
formattedTime := time.Now().Format(time.RFC3339)
clientLogs = append(clientLogs, fmt.Sprintf("%s: TASK OK", formattedTime))
}

_, err = writer.WriteString(strings.Join(clientLogs, "\n"))
if err != nil {
syslogger.Errorf("Failed to write logs for task %s: %v", task.UPID, err)
return
}

writer.Flush()
}(job, task)

job.LastRunUpid = &task.UPID
Expand Down

0 comments on commit c2d381f

Please sign in to comment.