|
| 1 | +package task |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + fs "github.com/aos-dev/go-service-fs/v2" |
| 8 | + "github.com/aos-dev/go-storage/v3/types" |
| 9 | + "github.com/aos-dev/go-toolbox/zapcontext" |
| 10 | + "github.com/nats-io/nats.go" |
| 11 | + natsproto "github.com/nats-io/nats.go/encoders/protobuf" |
| 12 | + "go.uber.org/zap" |
| 13 | + |
| 14 | + "github.com/aos-dev/noah/proto" |
| 15 | +) |
| 16 | + |
| 17 | +type Agent struct { |
| 18 | + w *Worker |
| 19 | + t *proto.Task |
| 20 | + |
| 21 | + queue *nats.EncodedConn |
| 22 | + subject string |
| 23 | + storages []types.Storager |
| 24 | + |
| 25 | + log *zap.Logger |
| 26 | +} |
| 27 | + |
| 28 | +func NewAgent(w *Worker, t *proto.Task) *Agent { |
| 29 | + return &Agent{ |
| 30 | + w: w, |
| 31 | + t: t, |
| 32 | + |
| 33 | + log: w.log, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (a *Agent) Handle() (err error) { |
| 38 | + ctx := context.Background() |
| 39 | + |
| 40 | + reply, err := a.w.node.Upgrade(ctx, &proto.UpgradeRequest{ |
| 41 | + NodeId: a.w.id, |
| 42 | + TaskId: a.t.Id, |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("node upgrade: %v", err) |
| 46 | + } |
| 47 | + a.log.Info("receive upgrade", zap.String("reply", reply.String())) |
| 48 | + |
| 49 | + a.subject = reply.Subject |
| 50 | + |
| 51 | + err = a.parseStorage(ctx) |
| 52 | + if err != nil { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + if reply.NodeId == a.w.id { |
| 57 | + return a.handleServer(ctx, reply.Addr) |
| 58 | + } else { |
| 59 | + return a.handleClient(ctx, reply.Addr) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func (a *Agent) parseStorage(ctx context.Context) (err error) { |
| 64 | + for range a.t.Endpoints { |
| 65 | + a.storages = append(a.storages, &fs.Storage{}) |
| 66 | + } |
| 67 | + return |
| 68 | +} |
| 69 | + |
| 70 | +func (a *Agent) handleServer(ctx context.Context, addr string) (err error) { |
| 71 | + conn, err := nats.Connect(addr) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("nats connect: %w", err) |
| 74 | + } |
| 75 | + queue, err := nats.NewEncodedConn(conn, natsproto.PROTOBUF_ENCODER) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("nats encoded connect: %w", err) |
| 78 | + } |
| 79 | + a.queue = queue |
| 80 | + |
| 81 | + // FIXME: we need to maintain task running status instead of job's |
| 82 | + rn := NewRunner(a, a.t.Job) |
| 83 | + |
| 84 | + return rn.Async(ctx, a.t.Job) |
| 85 | +} |
| 86 | + |
| 87 | +func (a *Agent) handleClient(ctx context.Context, addr string) (err error) { |
| 88 | + log := zapcontext.From(ctx) |
| 89 | + |
| 90 | + log.Info("agent connect to job queue", zap.String("addr", addr)) |
| 91 | + |
| 92 | + conn, err := nats.Connect(addr) |
| 93 | + if err != nil { |
| 94 | + return |
| 95 | + } |
| 96 | + queue, err := nats.NewEncodedConn(conn, natsproto.PROTOBUF_ENCODER) |
| 97 | + if err != nil { |
| 98 | + return |
| 99 | + } |
| 100 | + a.queue = queue |
| 101 | + |
| 102 | + // FIXME: we need to handle the returning subscription. |
| 103 | + _, err = a.queue.QueueSubscribe(a.subject, a.subject, a.handleJob) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("nats subscribe: %w", err) |
| 106 | + } |
| 107 | + return |
| 108 | +} |
| 109 | + |
| 110 | +func (a *Agent) handleJob(subject, reply string, job *proto.Job) { |
| 111 | + NewRunner(a, job).Handle(subject, reply) |
| 112 | +} |
0 commit comments