/
job_document.go
58 lines (52 loc) · 1.73 KB
/
job_document.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
package scheduler
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// jobDocument represents a job document on the mongo database
type jobDocument struct {
ID *primitive.ObjectID `bson:"_id,omitempty"`
Name string `bson:"name"`
Data map[string]any `bson:"data"`
ScheduleType string `bson:"schedule_type"`
Status string `bson:"status"`
NextRunAt time.Time `bson:"next_run_at"`
LastRunAt *time.Time `bson:"last_run_at,omitempty"`
ScheduleString string `bson:"schedule_string,omitempty"`
ScheduleLimitDate *time.Time `bson:"schedule_limit_date,omitempty"`
}
// marshalJob marshals a job struct into a job document
func marshalJob(j Job) jobDocument {
var id *primitive.ObjectID
if j.ID != "" {
parsedID, err := primitive.ObjectIDFromHex(j.ID)
if err == nil {
id = &parsedID
}
}
return jobDocument{
ID: id,
Name: j.Name,
Data: j.Data,
ScheduleType: j.ScheduleType.String(),
Status: j.Status.String(),
NextRunAt: j.NextRunAt,
LastRunAt: j.LastRunAt,
ScheduleString: j.ScheduleString,
ScheduleLimitDate: j.ScheduleLimitDate,
}
}
// unmarshalJob marshals a job document into a job struct
func unmarshalJob(j jobDocument) Job {
return Job{
ID: j.ID.Hex(),
Name: j.Name,
Data: j.Data,
ScheduleType: ScheduleType(j.ScheduleType),
Status: ScheduleStatus(j.Status),
NextRunAt: j.NextRunAt,
LastRunAt: j.LastRunAt,
ScheduleString: j.ScheduleString,
ScheduleLimitDate: j.ScheduleLimitDate,
}
}