machinery-plugins/brokers/etcd/delivery.go

123 lines
2.6 KiB
Go
Raw Normal View History

2024-06-07 23:55:03 +00:00
package etcd
import (
2024-06-08 00:56:50 +00:00
"bytes"
2024-06-07 23:55:03 +00:00
"context"
2024-06-08 00:56:50 +00:00
"encoding/json"
2024-06-07 23:55:03 +00:00
"fmt"
"time"
2024-06-08 00:56:50 +00:00
"github.com/RichardKnop/machinery/v2/brokers/errs"
2024-06-07 23:55:03 +00:00
"github.com/RichardKnop/machinery/v2/log"
"github.com/RichardKnop/machinery/v2/tasks"
clientv3 "go.etcd.io/etcd/client/v3"
)
type Delivery interface {
Ack()
Nack()
2024-06-08 00:56:50 +00:00
Body() []byte
2024-06-07 23:55:03 +00:00
Signature() *tasks.Signature
}
type deliver struct {
2024-06-08 00:56:50 +00:00
ctx context.Context
client *clientv3.Client
signature *tasks.Signature
value []byte
key string
aliveCancel func()
2024-06-07 23:55:03 +00:00
}
2024-06-08 00:56:50 +00:00
func NewDelivery(ctx context.Context, client *clientv3.Client, key string) (Delivery, error) {
d := &deliver{
ctx: ctx,
client: client,
key: key,
}
if err := d.assign(key); err != nil {
return nil, err
}
return d, nil
}
func (d *deliver) assign(key string) error {
assignKey := fmt.Sprintf("%s/assign", key)
ctx, cancel := context.WithTimeout(d.ctx, time.Second*5)
defer cancel()
grantResp, err := d.client.Grant(ctx, 30)
if err != nil {
return err
}
2024-06-08 01:51:10 +00:00
cmp := clientv3.Compare(clientv3.CreateRevision(assignKey), "=", 0)
putReq := clientv3.OpPut(assignKey, "node", clientv3.WithLease(grantResp.ID))
2024-06-08 00:56:50 +00:00
getReq := clientv3.OpGet(key)
resp, err := d.client.Txn(ctx).If(cmp).Then(putReq, getReq).Commit()
if err != nil {
return err
}
if !resp.Succeeded {
return fmt.Errorf("key %s already assign", key)
}
getResp := resp.Responses[1].GetResponseRange()
if len(getResp.Kvs) == 0 {
return fmt.Errorf("have no task %s", key)
}
kv := getResp.Kvs[0]
signature := new(tasks.Signature)
decoder := json.NewDecoder(bytes.NewReader(kv.Value))
decoder.UseNumber()
if err = decoder.Decode(signature); err != nil {
return errs.NewErrCouldNotUnmarshalTaskSignature(kv.Value, err)
}
aliveCtx, aliveCancel := context.WithCancel(d.ctx)
if _, err = d.client.KeepAlive(aliveCtx, grantResp.ID); err != nil {
aliveCancel()
return err
}
d.aliveCancel = aliveCancel
d.signature = signature
return nil
2024-06-07 23:55:03 +00:00
}
func (d *deliver) Ack() {
2024-06-08 00:56:50 +00:00
defer d.aliveCancel()
ctx, cancel := context.WithTimeout(d.ctx, time.Second*2)
2024-06-07 23:55:03 +00:00
defer cancel()
2024-06-08 00:56:50 +00:00
_, err := d.client.Delete(ctx, d.key, clientv3.WithPrefix())
2024-06-07 23:55:03 +00:00
if err != nil {
log.ERROR.Printf("ack task %s err: %s", d.value, err)
}
}
func (d *deliver) Nack() {
2024-06-08 00:56:50 +00:00
defer d.aliveCancel()
assignKey := fmt.Sprintf("%s/assign", d.key)
ctx, cancel := context.WithTimeout(d.ctx, time.Second*2)
2024-06-07 23:55:03 +00:00
defer cancel()
2024-06-08 00:56:50 +00:00
_, err := d.client.Delete(ctx, assignKey)
2024-06-07 23:55:03 +00:00
if err != nil {
log.ERROR.Printf("nack task %s err: %s", d.value, err)
}
}
func (d *deliver) Signature() *tasks.Signature {
return d.signature
}
2024-06-08 00:56:50 +00:00
func (d *deliver) Body() []byte {
2024-06-07 23:55:03 +00:00
return d.value
}