Skip to content

Commit 28a1e11

Browse files
committed
docs
1 parent 9c96e13 commit 28a1e11

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

queue.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ type PublishOptions struct {
6666
Tries int
6767
}
6868

69+
// NewPublishOptions creates a new PublishOptions with default settings.
6970
func NewPublishOptions() *PublishOptions {
7071
return &PublishOptions{
7172
MaxTries: 0,
7273
Tries: -1,
7374
}
7475
}
7576

77+
// SetMaxTries sets the maximum number of retry attempts for publishing. Returns the updated PublishOptions instance.
7678
func (p *PublishOptions) SetMaxTries(maxTries uint) *PublishOptions {
7779
p.MaxTries = maxTries
7880
return p
@@ -128,6 +130,7 @@ func (q *Queue) Publish(topic string, payload any, opts ...*PublishOptions) (*Ta
128130
return &t, nil
129131
}
130132

133+
// GetNext retrieves the next item from the queue for the given topic, marks it as running, and increments its tries count.
131134
func (q *Queue) GetNext(topic string) (*Task, error) {
132135
t := Task{}
133136
res := q.db.FindOneAndUpdate(bson.M{
@@ -153,6 +156,7 @@ func (q *Queue) GetNext(topic string) (*Task, error) {
153156
return &t, nil
154157
}
155158

159+
// GetNextById retrieves the next pending task by its ID, transitions it to the running state, and increments its tries count.
156160
func (q *Queue) GetNextById(id primitive.ObjectID) (*Task, error) {
157161
t := Task{}
158162
res := q.db.FindOneAndUpdate(bson.M{
@@ -178,12 +182,15 @@ func (q *Queue) GetNextById(id primitive.ObjectID) (*Task, error) {
178182
return &t, nil
179183
}
180184

185+
// Reschedule republishes a task to the queue, retaining its topic, payload, tries, and maxTries settings.
181186
func (q *Queue) Reschedule(task *Task) (*Task, error) {
182187
return q.Publish(task.Topic, task.Payload, NewPublishOptions().setTries(task.Tries).SetMaxTries(task.MaxTries))
183188
}
184189

185190
type Callback func(t Task)
186191

192+
// Subscribe listens for new tasks on a given topic and calls the provided callback when a new task is available.
193+
// It processes unprocessed tasks scheduled before starting the watch and continuously monitors for new tasks.
187194
func (q *Queue) Subscribe(topic string, cb Callback) error {
188195
pipeline := bson.D{{"$match", bson.D{
189196
{"operationType", "insert"},
@@ -240,6 +247,7 @@ func (q *Queue) Subscribe(topic string, cb Callback) error {
240247
return nil
241248
}
242249

250+
// Ack acknowledges a task completion by its ID, updating its state to "completed" and setting the completion timestamp.
243251
func (q *Queue) Ack(id string) error {
244252
oId, err := primitive.ObjectIDFromHex(id)
245253
if err != nil {
@@ -254,6 +262,7 @@ func (q *Queue) Ack(id string) error {
254262
}})
255263
}
256264

265+
// Err updates the state of a task to "error" by its ID, setting the completion time and storing the error message.
257266
func (q *Queue) Err(id string, err error) error {
258267
oId, e := primitive.ObjectIDFromHex(id)
259268
if e != nil {
@@ -269,6 +278,9 @@ func (q *Queue) Err(id string, err error) error {
269278
})
270279
}
271280

281+
// Selfcare re-schedules long-running tasks and sets tasks exceeding max tries to error state.
282+
// It updates tasks in an ongoing state that haven't been acknowledged within a specific timeout period.
283+
// If timeout is zero, the default timeout value is used. Optionally, tasks can be filtered by topic.
272284
func (q *Queue) Selfcare(topic string, timeout time.Duration) error {
273285
// re-schedule long-running tasks
274286
// this only happens if the processor could not ack the task, i.e. the application crashed
@@ -319,6 +331,7 @@ func (q *Queue) Selfcare(topic string, timeout time.Duration) error {
319331
return nil
320332
}
321333

334+
// CreateIndexes creates MongoDB indexes for the task collection to improve query performance and manage TTL for completed tasks.
322335
func (q *Queue) CreateIndexes() error {
323336
err := q.db.CreateIndexes([]mongo.IndexModel{{
324337
Keys: bson.D{{"topic", 1}, {"state", 1}},

0 commit comments

Comments
 (0)