@@ -4,19 +4,44 @@ import (
44 "context"
55 "encoding/json"
66 "log"
7-
8- "github.com/nats-io/nats.go"
97)
108
119// Signature for function executed by a worker.
1210// `ProcessingFunc` type are be registered to subjects, process messages published by client
1311type ProcessingFunc func (context.Context , * TaskPayload ) error
1412
13+ type ListenUpdates struct {
14+ m map [string ]chan string
15+ }
16+
17+ type Inspector struct {
18+ broker * NatsBroker
19+ listeners * ListenUpdates
20+ }
21+
22+ func NewInspector (broker * NatsBroker ) * Inspector {
23+ return & Inspector {
24+ broker : broker ,
25+ listeners : & ListenUpdates {make (map [string ]chan string )},
26+ }
27+ }
28+
29+ func (i * Inspector ) AddAnother (queue string , sendUpdatesTo chan string ) error {
30+ return nil
31+ }
32+ func (i * Inspector ) Servers () {}
33+ func (i * Inspector ) Queues () {}
34+
35+ // Client responsible for interaction with nq tasks
36+ //
37+ // Client is used to enqueue / cancel tasks or fetch metadata for tasks
1538type PublishClient struct {
1639 broker * NatsBroker
1740 kv ResultHandlerIFACE
1841}
1942
43+ // NewPublishClient returns a new Client instance, given nats connection options, to interact with nq tasks
44+ //
2045func NewPublishClient (config NatsClientOpt , opts ... ClientConnectionOption ) * PublishClient {
2146 opt , err := withDefaultClientOptions (opts ... )
2247 if err != nil {
@@ -40,12 +65,6 @@ func NewPublishClient(config NatsClientOpt, opts ...ClientConnectionOption) *Pub
4065 return & PublishClient {broker : broker , kv : kv }
4166}
4267
43- type PackagePubAck struct {
44- // ID assigned to published message
45- ID string
46- * nats.PubAck
47- }
48-
4968func (p * PublishClient ) Stats (queue string ) error {
5069 return p .broker .Stats (NewQueue (queue ))
5170}
@@ -68,8 +87,7 @@ func (p *PublishClient) publishMessage(msg *TaskMessage) (*TaskMessage, error) {
6887 }
6988}
7089
71- // Publish a TaskMessage into a stream
72- func (p * PublishClient ) PublishToSubject (task * Task , opts ... TaskOption ) (* TaskMessage , error ) {
90+ func (p * PublishClient ) publishToSubject (task * Task , opts ... TaskOption ) (* TaskMessage , error ) {
7391 opts = append (task .opts , opts ... )
7492 opt , err := withDefaultOptions (opts ... )
7593 if err != nil {
@@ -98,14 +116,29 @@ func (p *PublishClient) PublishToSubject(task *Task, opts ...TaskOption) (*TaskM
98116 return p .publishMessage (taskMessage )
99117}
100118
119+ // GetUpdates can be used get changes to a task's metadata
120+ //
121+ // Returns error if failed to start watching for changes
122+ // Channel is closed, once task reaches terminal state
123+ func (p * PublishClient ) GetUpdates (taskID string ) (chan * TaskMessage , error ) {
124+ if status , err := p .kv .Watch (taskID ); err != nil {
125+ return nil , err
126+ } else {
127+ return status , err
128+ }
129+ }
130+
131+ // Enqueue can be used to enqueu given task to a queue
132+ //
133+ // Returns TaskMessage and nil error is enqueued successfully, else non-nill error
101134func (p * PublishClient ) Enqueue (task * Task , opts ... TaskOption ) (* TaskMessage , error ) {
102135 q := NewQueue (task .queue )
103136 p .broker .ConnectoQueue (q )
104137
105- return p .PublishToSubject (task , opts ... )
138+ return p .publishToSubject (task , opts ... )
106139}
107140
108- // Fetch qname from kv store instead
141+ // Cancel sends `cancel` request for given task to workers
109142func (p * PublishClient ) Cancel (id string ) error {
110143 if taskInfo , err := p .kv .Get (id ); err != nil {
111144 return ErrTaskNotFound
@@ -140,22 +173,31 @@ func (p *PublishClient) CancelInQueue(id string, qname string) error {
140173 return p .cancelInStream (id , q )
141174}
142175
143- func (p * PublishClient ) createStream (streamName , subject string , policy nats.RetentionPolicy ) error {
144- if err := p .broker .AddStream (nats.StreamConfig {
145- Name : streamName ,
146- Subjects : []string {subject },
147- Retention : policy ,
148- }); err != nil {
149- return err
150- }
151- return nil
152- }
153-
176+ // Delete a queue
177+ //
178+ // Deletes underlying nats stream assosociated with a queue
154179func (p * PublishClient ) DeleteQueue (qname string ) {
155180 q := NewQueue (qname )
181+ // delete task stream
156182 if err := p .broker .DeleteStream (q .stream ); err != nil {
157183 log .Printf ("error deleting stream=%s" , q .stream )
158184 }
185+ // delete cancellation stream
186+ if err := p .broker .DeleteStream (q .cancelStream ); err != nil {
187+ log .Printf ("error deleting stream=%s" , q .stream )
188+ }
189+ }
190+
191+ // Fetch fetches TaskMessage for given task
192+ //
193+ func (p * PublishClient ) Fetch (id string ) (* TaskMessage , error ) {
194+ return p .kv .Get (id )
195+ }
196+
197+ // Close closes the connection with nats
198+ func (p * PublishClient ) Close () error {
199+ defer p .broker .Close ()
200+ return nil
159201}
160202
161203func (p * PublishClient ) cancelInStream (id string , q * Queue ) error {
@@ -174,13 +216,3 @@ func (p *PublishClient) cancelInStream(id string, q *Queue) error {
174216 return nil
175217 }
176218}
177-
178- func (p * PublishClient ) Fetch (id string ) (* TaskMessage , error ) {
179- return p .kv .Get (id )
180- }
181-
182- // Also delete stream for cleanup
183- func (p * PublishClient ) Close () error {
184- defer p .broker .Close ()
185- return nil
186- }
0 commit comments