forked from riverqueue/river
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
91 lines (73 loc) · 2.94 KB
/
event.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
package river
import (
"time"
"github.com/riverqueue/river/internal/jobstats"
"github.com/riverqueue/river/rivertype"
)
// EventKind is a kind of event to subscribe to from a client.
type EventKind string
const (
// EventKindJobCancelled occurs when a job is cancelled.
EventKindJobCancelled EventKind = "job_cancelled"
// EventKindJobCompleted occurs when a job is completed.
EventKindJobCompleted EventKind = "job_completed"
// EventKindJobFailed occurs when a job fails. Occurs both when a job fails
// and will be retried and when a job fails for the last time and will be
// discarded. Callers can use job fields like `Attempt` and `State` to
// differentiate each type of occurrence.
EventKindJobFailed EventKind = "job_failed"
// EventKindJobSnoozed occurs when a job is snoozed.
EventKindJobSnoozed EventKind = "job_snoozed"
// EventKindQueuePaused occurs when a queue is paused.
EventKindQueuePaused EventKind = "queue_paused"
// EventKindQueueResumed occurs when a queue is resumed.
EventKindQueueResumed EventKind = "queue_resumed"
)
// All known event kinds, used to validate incoming kinds. This is purposely not
// exported because end users should have no way of subscribing to all known
// kinds for forward compatibility reasons.
var allKinds = map[EventKind]struct{}{ //nolint:gochecknoglobals
EventKindJobCancelled: {},
EventKindJobCompleted: {},
EventKindJobFailed: {},
EventKindJobSnoozed: {},
EventKindQueuePaused: {},
EventKindQueueResumed: {},
}
// Event wraps an event that occurred within a River client, like a job being
// completed.
type Event struct {
// Kind is the kind of event. Receivers should read this field and respond
// accordingly. Subscriptions will only receive event kinds that they
// requested when creating a subscription with Subscribe.
Kind EventKind
// Job contains job-related information.
Job *rivertype.JobRow
// JobStats are statistics about the run of a job.
JobStats *JobStatistics
// Queue contains queue-related information.
Queue *rivertype.Queue
}
// JobStatistics contains information about a single execution of a job.
type JobStatistics struct {
CompleteDuration time.Duration // Time it took to set the job completed, discarded, or errored.
QueueWaitDuration time.Duration // Time the job spent waiting in available state before starting execution.
RunDuration time.Duration // Time job spent running (measured around job worker.)
}
func jobStatisticsFromInternal(stats *jobstats.JobStatistics) *JobStatistics {
return &JobStatistics{
CompleteDuration: stats.CompleteDuration,
QueueWaitDuration: stats.QueueWaitDuration,
RunDuration: stats.RunDuration,
}
}
// eventSubscription is an active subscription for events being produced by a
// client, created with Client.Subscribe.
type eventSubscription struct {
Chan chan *Event
Kinds map[EventKind]struct{}
}
func (s *eventSubscription) ListensFor(kind EventKind) bool {
_, ok := s.Kinds[kind]
return ok
}