From 210ffe9f842a0d03a3a9807a4524daca3f70f43c Mon Sep 17 00:00:00 2001 From: Son Roy Almerol Date: Mon, 4 Nov 2024 13:12:18 -0500 Subject: [PATCH] add since argument --- internal/backend/backup/jobrun.go | 35 +++++++++++-------------------- internal/store/tasks.go | 5 +++-- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/internal/backend/backup/jobrun.go b/internal/backend/backup/jobrun.go index 187e813..0fbdbbb 100644 --- a/internal/backend/backup/jobrun.go +++ b/internal/backend/backup/jobrun.go @@ -96,46 +96,35 @@ func RunBackup(job *store.Job, storeInstance *store.Store) (*store.Task, error) return nil, fmt.Errorf("RunBackup: proxmox-backup-client start error (%s) -> %w", cmd.String(), err) } + timeStarted := time.Now() + for { line, err := logBuffer.ReadString('\n') if err != nil && line != "" { return nil, fmt.Errorf("RunBackup: log buffer readString error -> %w", err) } - if strings.Contains(line, "Starting backup protocol") { + if strings.Contains(line, "Upload directory") { break } time.Sleep(time.Millisecond * 100) } - var task *store.Task - for { - task, err = storeInstance.GetMostRecentTask(job) - if err != nil { - _ = cmd.Process.Kill() - if agentMount != nil { - agentMount.Unmount() - } - - return nil, fmt.Errorf("RunBackup: unable to get most recent task -> %w", err) - } + time.Sleep(time.Millisecond * 500) - if task.WorkerType == "backup" { - break - } - - if cmd.ProcessState != nil { - break + task, err := storeInstance.GetMostRecentTask(job, &timeStarted) + if err != nil { + _ = cmd.Process.Kill() + if agentMount != nil { + agentMount.Unmount() } - time.Sleep(time.Millisecond * 500) + return nil, fmt.Errorf("RunBackup: unable to get most recent task -> %w", err) } - if task != nil { - job.LastRunUpid = &task.UPID - job.LastRunState = &task.Status - } + job.LastRunUpid = &task.UPID + job.LastRunState = &task.Status err = storeInstance.UpdateJob(*job) if err != nil { diff --git a/internal/store/tasks.go b/internal/store/tasks.go index 8a48771..101a182 100644 --- a/internal/store/tasks.go +++ b/internal/store/tasks.go @@ -3,6 +3,7 @@ package store import ( "fmt" "net/http" + "time" ) type TasksResponse struct { @@ -29,11 +30,11 @@ type Task struct { ExitStatus string `json:"exitstatus"` } -func (storeInstance *Store) GetMostRecentTask(job *Job) (*Task, error) { +func (storeInstance *Store) GetMostRecentTask(job *Job, since *time.Time) (*Task, error) { var resp TasksResponse err := storeInstance.ProxmoxHTTPRequest( http.MethodGet, - fmt.Sprintf("/api2/json/nodes/localhost/tasks?store=%s&typefilter=backup&limit=1", job.Store), + fmt.Sprintf("/api2/json/nodes/localhost/tasks?store=%s&typefilter=backup&limit=1&since=%d", job.Store, since.Unix()), nil, &resp, )