-
Notifications
You must be signed in to change notification settings - Fork 4
/
base.go
144 lines (112 loc) · 2.88 KB
/
base.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package nq
import (
"time"
"github.com/nats-io/nats.go"
)
// NatsClientOpt represent NATS connection configuration option.
type NatsClientOpt struct {
// nats server address
Addr string
// Name for key-value store used to store task metadata
//
// Defaults to nq
DBName string
// ReconnectWait is an Option to set the wait time between reconnect attempts.
//
// Defaults to 10 seconds
ReconnectWait time.Duration
// MaxReconnects is an Option to set the maximum number of reconnect attempts.
//
// Defaults to 100
MaxReconnects int
}
type CancelPayload string
type TaskPayload struct {
ID string
Payload []byte
}
// Possible task statuses
const (
// waiting for task to be received by worker
Pending = iota
// task is being processed by a worker
Processing
// taskFN returns an error
Failed
// successfully processed
Completed
// cancelled by user
Cancelled
// deleted before being run
Deleted
)
type TaskMessage struct {
// Sequence indicates sequence number of message in nats jetstream
Sequence uint64
// ID is a unique identifier for each task, used for cancellation.
ID string
// . Autofilled
StreamName string
//
Queue string
// Payload holds data needed to process the task.
Payload []byte
// Status indicated status of task execution
Status int
// Timeout specifies timeout in seconds.
// Use zero to indicate no deadline.
Timeout int64
// Deadline specifies the deadline for the task in Unix time.
// Use zero to indicate no deadline.
Deadline int64
// CompletedAt is the time the task was processed successfully in Unix time,
// the number of seconds elapsed since January 1, 1970 UTC.
//
// Negative value indicated cancelled.
// Use zero to indicate no value.
CompletedAt int64
// PubAck is an ack received after successfully publishing a message.
// NatsAck nats.PubAck
// Current retry count
//
// autofilled
CurrentRetry int
// Total number of retries possible for this task
MaxRetry int
// Function to acknowledge this TaskMessage when received as a subscription
ackFN func(opts ...nats.AckOpt) error
}
func (msg *TaskMessage) GetStatus() string {
switch msg.Status {
case Pending:
return "pending"
case Processing:
return "processing"
case Failed:
return "failed"
case Completed:
return "completed"
case Cancelled:
return "cancelled"
case Deleted:
return "deleted"
default:
return "invalid state"
}
}
type TaskCancellationMessage struct {
// ID corresponds to task's ID
ID string
// StreamName is the name of stream whose subject is handled by this task
StreamName string
}
type ClientOptionType int
// Internal representation of options for nats-server connection
type ClientOption struct {
Timeout time.Duration //todo
AuthenticationType ClientOptionType
AuthenticationObject interface{}
NatsOption []nats.Option
// Defaults to false
ShutdownOnNatsDisconnect bool
}