-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
113 lines (87 loc) · 2.62 KB
/
entry.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cron
import (
"context"
"time"
)
// EntryID identifies an entry within a Cron instance
type EntryID int
// Entry consists of a schedule and the func to execute on that schedule.
type Entry struct {
// id is the cron-assigned id of this entry, which may be used to look up a
// snapshot or remove it.
id EntryID
// schedule on which this job should be run.
schedule Schedule
// next time the job will run, or the zero time if Cron has not been
// started or this entry's schedule is unsatisfiable
next time.Time
// prev is the last time this job was run, or the zero time if never.
prev time.Time
// wrappedJob is the thing to run when the schedule is activated.
wrappedJob Job
// job is the thing that was submitted to cron.
// It is kept around so that user code that needs to get at the job later,
// e.g. via Entries() can do so.
job Job
// middlewares are the list of middlewares to apply to the job.
middlewares []Middleware
}
type entryOption func(*Entry)
func withEntryMiddlewares(middlewares ...Middleware) entryOption {
return func(e *Entry) {
e.middlewares = middlewares
}
}
// newEntry creates a new entry with the given schedule and job.
func newEntry(id EntryID, schedule Schedule, job Job, opts ...entryOption) *Entry {
entry := &Entry{
id: id,
schedule: schedule,
job: job,
}
for _, opt := range opts {
opt(entry)
}
// Wrap the job with the entry context.
middlewares := append([]Middleware{
func(job Job) Job {
return JobFunc(func(ctx context.Context) error {
return job.Run(WithEntryContext(ctx, entry))
})
},
}, entry.middlewares...)
// Wrap the job with the middlewares.
entry.wrappedJob = Chain(middlewares...)(entry.job)
return entry
}
func (e *Entry) ID() EntryID {
return e.id
}
// Valid returns true if this is not the zero entry.
func (e *Entry) Valid() bool { return e.id != 0 }
func (e *Entry) Schedule() Schedule {
return e.schedule
}
func (e *Entry) Next() time.Time {
return e.next
}
func (e *Entry) Prev() time.Time {
return e.prev
}
func (e *Entry) WrappedJob() Job {
return e.wrappedJob
}
func (e *Entry) Job() Job {
return e.job
}
// ------------------------------------ Entry Context ------------------------------------
type entryContextKey struct{}
// WithEntryContext returns a new context with the given EntryID.
func WithEntryContext(ctx context.Context, entry *Entry) context.Context {
return context.WithValue(ctx, entryContextKey{}, entry)
}
// EntryFromContext returns the EntryID from the context.
func EntryFromContext(ctx context.Context) (*Entry, bool) {
entry, ok := ctx.Value(entryContextKey{}).(*Entry)
return entry, ok
}