generated from keboola/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_job.go
66 lines (57 loc) · 1.5 KB
/
queue_job.go
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
58
59
60
61
62
63
64
65
66
package keboola
import (
jsonLib "encoding/json"
"fmt"
"github.com/relvacode/iso8601"
)
// JobID is an ID of a component job.
type JobID string
func (j JobID) String() string {
return string(j)
}
func (j *JobID) UnmarshalJSON(b []byte) error {
var asString string
err := jsonLib.Unmarshal(b, &asString)
if err != nil {
var asInt int
err = jsonLib.Unmarshal(b, &asInt)
if err != nil {
return fmt.Errorf("failed to unmarshal int or string")
}
asString = fmt.Sprint(asInt)
}
*j = JobID(asString)
return nil
}
// JobKey is a unique identifier of QueueJob.
type JobKey struct {
ID JobID `json:"id"`
}
type JobResult struct {
Error map[string]any `json:"error,omitempty"`
Message string `json:"message,omitempty"`
}
// UnmarshalJSON implements JSON decoding.
func (r *JobResult) UnmarshalJSON(data []byte) (err error) {
if string(data) == "[]" {
*r = JobResult{
Error: nil,
Message: "",
}
return nil
}
// see https://stackoverflow.com/questions/43176625/call-json-unmarshal-inside-unmarshaljson-function-without-causing-stack-overflow
type _r JobResult
return jsonLib.Unmarshal(data, (*_r)(r))
}
// QueueJob is a component job.
type QueueJob struct {
JobKey
Status string `json:"status"`
IsFinished bool `json:"isFinished"`
URL string `json:"url"`
Result JobResult `json:"result,omitempty"`
CreateTime iso8601.Time `json:"createdTime"`
StartTime *iso8601.Time `json:"startTime"`
EndTime *iso8601.Time `json:"endTime"`
}