File: jobs.go

package info (click to toggle)
golang-github-mimuret-golang-iij-dpf 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: makefile: 55
file content (57 lines) | stat: -rw-r--r-- 1,439 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package apiutils

import (
	"context"
	"fmt"
	"net/url"
	"os"
	"os/signal"
	"path"
	"strconv"
	"time"

	"github.com/mimuret/golang-iij-dpf/pkg/api"
	"github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/core"
)

func WaitJob(ctx context.Context, c api.ClientInterface, jobID string, interval time.Duration) (*core.Job, error) {
	job := &core.Job{
		RequestID: jobID,
	}
	if _, err := c.Read(ctx, job); err != nil {
		return nil, fmt.Errorf("failed to read Job: %w", err)
	}
	ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
	defer stop()
	for job.Status == core.JobStatusRunning {
		job.RequestID = jobID
		if err := c.WatchRead(ctx, interval, job); err != nil {
			return nil, err
		}
	}
	if job.Status == core.JobStatusFailed {
		return job, fmt.Errorf("JobID %s job failed: type: %s msg: %s", jobID, job.ErrorType, job.ErrorMessage)
	}
	return job, nil
}

func ParseeResourceSystemID(job *core.Job) (string, error) {
	u, err := url.Parse(job.ResourceUrl)
	if err != nil {
		return "", fmt.Errorf("failed to parse resource-url: %s , %w", job.ResourceUrl, err)
	}
	_, id := path.Split(u.Path)
	return id, nil
}

func ParseeResourceID(job *core.Job) (int64, error) {
	idStr, err := ParseeResourceSystemID(job)
	if err != nil {
		return 0, err
	}
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		return 0, fmt.Errorf("failed to convert to int64 resource-url: %s id: %s, %w", job.ResourceUrl, idStr, err)
	}
	return id, nil
}