60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package etcd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/RichardKnop/machinery/v2/log"
|
|
"github.com/RichardKnop/machinery/v2/tasks"
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
|
)
|
|
|
|
type Delivery interface {
|
|
Ack()
|
|
Nack()
|
|
Body() string
|
|
Signature() *tasks.Signature
|
|
}
|
|
|
|
type deliver struct {
|
|
ctx context.Context
|
|
client *clientv3.Client
|
|
signature *tasks.Signature
|
|
value string
|
|
}
|
|
|
|
func NewDelivery(ctx context.Context, key string) (Delivery, error) {
|
|
return &deliver{}, nil
|
|
}
|
|
|
|
func (d *deliver) Ack() {
|
|
keyPrefix := fmt.Sprintf("/machinery/v2/broker/pending_tasks/%s/%s", d.signature.RoutingKey, d.signature.UUID)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
defer cancel()
|
|
|
|
_, err := d.client.Delete(ctx, keyPrefix, clientv3.WithPrefix())
|
|
if err != nil {
|
|
log.ERROR.Printf("ack task %s err: %s", d.value, err)
|
|
}
|
|
}
|
|
|
|
func (d *deliver) Nack() {
|
|
keyPrefix := fmt.Sprintf("/machinery/v2/broker/pending_tasks/%s/%s/state", d.signature.RoutingKey, d.signature.UUID)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
defer cancel()
|
|
|
|
_, err := d.client.Delete(ctx, keyPrefix)
|
|
if err != nil {
|
|
log.ERROR.Printf("nack task %s err: %s", d.value, err)
|
|
}
|
|
}
|
|
|
|
func (d *deliver) Signature() *tasks.Signature {
|
|
return d.signature
|
|
}
|
|
|
|
func (d *deliver) Body() string {
|
|
return d.value
|
|
}
|