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"
|
|
|
|
)
|
|
|
|
|
2024-06-08 16:03:27 +00:00
|
|
|
// Delivery task delivery with ack and nack
|
2024-06-07 23:55:03 +00:00
|
|
|
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
|
2024-06-08 01:57:35 +00:00
|
|
|
node string
|
2024-06-08 00:56:50 +00:00
|
|
|
aliveCancel func()
|
2024-06-07 23:55:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-08 16:03:27 +00:00
|
|
|
// NewDelivery create the task delivery
|
2024-06-08 01:57:35 +00:00
|
|
|
func NewDelivery(ctx context.Context, client *clientv3.Client, key string, node string) (Delivery, error) {
|
2024-06-08 00:56:50 +00:00
|
|
|
d := &deliver{
|
|
|
|
ctx: ctx,
|
|
|
|
client: client,
|
|
|
|
key: key,
|
2024-06-08 01:57:35 +00:00
|
|
|
node: node,
|
2024-06-08 00:56:50 +00:00
|
|
|
}
|
|
|
|
|
2024-06-08 01:57:35 +00:00
|
|
|
if err := d.assign(key, node); err != nil {
|
2024-06-08 00:56:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2024-06-08 01:57:35 +00:00
|
|
|
func (d *deliver) assign(key string, node string) error {
|
2024-06-08 00:56:50 +00:00
|
|
|
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 15:50:48 +00:00
|
|
|
keyExist := clientv3.Compare(clientv3.CreateRevision(key), ">", 0)
|
|
|
|
assignNotExist := clientv3.Compare(clientv3.CreateRevision(assignKey), "=", 0)
|
2024-06-08 01:57:35 +00:00
|
|
|
putReq := clientv3.OpPut(assignKey, node, clientv3.WithLease(grantResp.ID))
|
2024-06-08 00:56:50 +00:00
|
|
|
getReq := clientv3.OpGet(key)
|
2024-06-08 15:50:48 +00:00
|
|
|
resp, err := d.client.Txn(ctx).If(keyExist, assignNotExist).Then(putReq, getReq).Commit()
|
2024-06-08 00:56:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !resp.Succeeded {
|
2024-06-08 16:03:27 +00:00
|
|
|
return fmt.Errorf("task %s not exist or already assign", key)
|
2024-06-08 00:56:50 +00:00
|
|
|
}
|
|
|
|
|
2024-06-08 15:50:48 +00:00
|
|
|
if len(resp.Responses) < 2 {
|
2024-06-08 16:03:27 +00:00
|
|
|
return fmt.Errorf("task %s tnx resp invalid, count=%d", key, len(resp.Responses))
|
2024-06-08 15:50:48 +00:00
|
|
|
}
|
2024-06-08 00:56:50 +00:00
|
|
|
getResp := resp.Responses[1].GetResponseRange()
|
2024-06-08 16:03:27 +00:00
|
|
|
if len(getResp.Kvs) == 0 || len(getResp.Kvs[0].Value) == 0 {
|
|
|
|
return fmt.Errorf("task %s have no body", key)
|
2024-06-08 00:56:50 +00:00
|
|
|
}
|
|
|
|
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
|
2024-06-08 16:03:27 +00:00
|
|
|
d.value = kv.Value
|
2024-06-08 00:56:50 +00:00
|
|
|
return nil
|
2024-06-07 23:55:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-08 16:03:27 +00:00
|
|
|
// Ack acknowledged the task is done
|
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 {
|
2024-06-08 16:03:27 +00:00
|
|
|
log.ERROR.Printf("ack task %s err: %s", d.key, err)
|
2024-06-07 23:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 16:03:27 +00:00
|
|
|
// Nack negatively acknowledge the delivery of task should handle again
|
2024-06-07 23:55:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 16:03:27 +00:00
|
|
|
// Signature return the task Signature
|
2024-06-07 23:55:03 +00:00
|
|
|
func (d *deliver) Signature() *tasks.Signature {
|
|
|
|
return d.signature
|
|
|
|
}
|
|
|
|
|
2024-06-08 16:03:27 +00:00
|
|
|
// Body return the task body
|
2024-06-08 00:56:50 +00:00
|
|
|
func (d *deliver) Body() []byte {
|
2024-06-07 23:55:03 +00:00
|
|
|
return d.value
|
|
|
|
}
|