Skip to content

Commit

Permalink
get task end from array api
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Oct 30, 2024
1 parent 58c8997 commit a623f27
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 18 deletions.
20 changes: 2 additions & 18 deletions store/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"database/sql"
"errors"
"net/http"
"strconv"
"strings"
"time"

_ "github.com/mattn/go-sqlite3"
Expand Down Expand Up @@ -112,14 +110,7 @@ func (store *Store) GetJob(id string) (*Job, error) {
}

if task.Status == "stopped" {
idSplit := strings.Split(task.WID, "/")
endTimeHex := idSplit[len(idSplit)-1]
endTime, err := strconv.ParseInt(endTimeHex, 16, 64)
if err != nil {
endTime = time.Now().Unix()
}

job.LastRunEndtime = &endTime
job.LastRunEndtime = &task.EndTime
job.LastRunState = &task.ExitStatus

tmpDuration := task.EndTime - task.StartTime
Expand Down Expand Up @@ -211,14 +202,7 @@ func (store *Store) GetAllJobs() ([]Job, error) {
}

if task.Status == "stopped" {
idSplit := strings.Split(task.WID, "/")
endTimeHex := idSplit[len(idSplit)-1]
endTime, err := strconv.ParseInt(endTimeHex, 16, 64)
if err != nil {
endTime = time.Now().Unix()
}

job.LastRunEndtime = &endTime
job.LastRunEndtime = &task.EndTime
job.LastRunState = &task.ExitStatus

tmpDuration := task.EndTime - task.StartTime
Expand Down
68 changes: 68 additions & 0 deletions store/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,73 @@ func GetTaskByUPID(upid string, r *http.Request) (*Task, error) {
return nil, err
}

endTime, err := GetTaskEndTime(&taskStruct.Data, r)
if err != nil {
return nil, err
}

taskStruct.Data.EndTime = endTime

return &taskStruct.Data, nil
}

func GetTaskEndTime(task *Task, r *http.Request) (int64, error) {
nextPage := true
var tasksStruct TasksResponse

page := 1
for nextPage {
start := (page - 1) * 50
tasksReq, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf(
"%s/api2/json/nodes/localhost/tasks?typefilter=backup&running=false&start=%d&since=%d",
ProxyTargetURL,
start,
task.StartTime,
),
nil,
)
tasksReq.Header.Set("Csrfpreventiontoken", r.Header.Get("Csrfpreventiontoken"))
tasksReq.Header.Set("User-Agent", r.Header.Get("User-Agent"))

for _, cookie := range r.Cookies() {
tasksReq.AddCookie(cookie)
}

client := http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

tasksResp, err := client.Do(tasksReq)
if err != nil {
return -1, err
}

tasksBody, err := io.ReadAll(tasksResp.Body)
if err != nil {
return -1, err
}

err = json.Unmarshal(tasksBody, &tasksStruct)
if err != nil {
fmt.Println(tasksBody)
return -1, err
}

for _, taskStruct := range tasksStruct.Data {
if taskStruct.UPID == task.UPID {
return taskStruct.EndTime, nil
}
}

if tasksStruct.Total <= page*50 {
nextPage = false
}
}

return nil, fmt.Errorf("error getting tasks: not found")
}

0 comments on commit a623f27

Please sign in to comment.