-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
82 lines (67 loc) · 2.43 KB
/
scheduler.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
package pubsub
import (
"context"
"fmt"
"time"
)
type Scheduler interface {
Publisher
Schedule(ctx context.Context, dueDate time.Time, topic string, messages ...*Message) error
Delay(ctx context.Context, delay time.Duration, topic string, messages ...*Message) error
}
type DueMessage struct {
Topic string
Envelope *Envelope
Err error
}
type SchedulerStorage interface {
// Schedule schedules a message to be published in the future.
Schedule(ctx context.Context, dueDate time.Time, topic string, messages ...*Envelope) error
// ConsumeDue returns a channel that should receive the next due messages or an error.
// It should feed the next messages while the context is valid, once the context terminates
// it should make any cleaning task and close the channel to signal a proper cleanup.
ConsumeDue(ctx context.Context) (<-chan DueMessage, error)
// Published indicates to the storage that the message has been published.
Published(ctx context.Context, message DueMessage) error
}
var _ Scheduler = &SchedulerPublisher{}
type SchedulerPublisher struct {
*MarshallerPublisher
storage SchedulerStorage
now func() time.Time
}
func NewSchedulerPublisher(pub *MarshallerPublisher, storage SchedulerStorage) *SchedulerPublisher {
return &SchedulerPublisher{
MarshallerPublisher: pub,
storage: storage,
now: time.Now,
}
}
func (p *SchedulerPublisher) Delay(ctx context.Context, duration time.Duration, topic string, messages ...*Message) error {
return p.Schedule(ctx, p.now().Add(duration), topic, messages...)
}
func (p *SchedulerPublisher) Schedule(ctx context.Context, dueDate time.Time, topic string, messages ...*Message) error {
envelopes, err := marshallMessages(p.marshaller, messages...)
if err != nil {
return err
}
return p.storage.Schedule(ctx, dueDate, topic, envelopes...)
}
func (p *SchedulerPublisher) PublishDue(ctx context.Context) error {
feed, err := p.storage.ConsumeDue(ctx)
if err != nil {
return fmt.Errorf("cannot start consuming due messages: %w", err)
}
for next := range feed {
if err := next.Err; err != nil {
return fmt.Errorf("cannot receive next due message: %w", err)
}
if err := p.publisher.Publish(ctx, next.Topic, next.Envelope); err != nil {
return fmt.Errorf("cannot publish due message: %w", err)
}
if err := p.storage.Published(ctx, next); err != nil {
return fmt.Errorf("cannot notify message publication: %w", err)
}
}
return nil
}