Skip to content

Commit 0579164

Browse files
initialize dbPuller.startFrom in pullerProvider
1 parent 895dda3 commit 0579164

1 file changed

Lines changed: 26 additions & 33 deletions

File tree

examples/clustermode/server/puller.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 20\d\d The A2A Authors
1+
// Copyright 2026 The A2A Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,18 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Licensed under the Apache License, Version 2.0 (the "License");
16-
// you may not use this file except in compliance with the License.
17-
// You may obtain a copy of the License at
18-
19-
// http://www.apache.org/licenses/LICENSE-2.0
20-
21-
// Unless required by applicable law or agreed to in writing, software
22-
// distributed under the License is distributed on an "AS IS" BASIS,
23-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24-
// See the License for the specific language governing permissions and
25-
// limitations under the License.
26-
2715
package main
2816

2917
import (
@@ -42,8 +30,9 @@ import (
4230
var _ eventqueue.Puller = (*dbPuller)(nil)
4331

4432
type dbPuller struct {
45-
db *sql.DB
46-
store taskstore.Store
33+
db *sql.DB
34+
store taskstore.Store
35+
startFrom string
4736
}
4837

4938
func (p *dbPuller) Pull(ctx context.Context, taskID a2a.TaskID, pullCursor eventqueue.PullCursor) (*eventqueue.PullResponse, error) {
@@ -54,22 +43,15 @@ func (p *dbPuller) Pull(ctx context.Context, taskID a2a.TaskID, pullCursor event
5443
)
5544

5645
if cursor == "" {
57-
rows, err = p.db.QueryContext(ctx, `
58-
SELECT event_json, task_version, id
59-
FROM task_event
60-
WHERE task_id = ?
61-
ORDER BY id ASC
62-
LIMIT 10
63-
`, taskID)
64-
} else {
65-
rows, err = p.db.QueryContext(ctx, `
66-
SELECT event_json, task_version, id
67-
FROM task_event
68-
WHERE task_id = ? AND id > ?
69-
ORDER BY id ASC
70-
LIMIT 10
71-
`, taskID, cursor)
46+
cursor = p.startFrom
7247
}
48+
rows, err = p.db.QueryContext(ctx, `
49+
SELECT event_json, task_version, id
50+
FROM task_event
51+
WHERE task_id = ? AND id > ?
52+
ORDER BY id ASC
53+
LIMIT 10
54+
`, taskID, cursor)
7355
if err != nil {
7456
return nil, fmt.Errorf("failed to query events: %w", err)
7557
}
@@ -91,6 +73,9 @@ func (p *dbPuller) Pull(ctx context.Context, taskID a2a.TaskID, pullCursor event
9173
messages = append(messages, &msg)
9274
nextCursor = id
9375
}
76+
if err := rows.Err(); err != nil {
77+
return nil, fmt.Errorf("failed to query events: %w", err)
78+
}
9479
return &eventqueue.PullResponse{
9580
Messages: messages,
9681
Cursor: nextCursor,
@@ -101,8 +86,8 @@ func (p *dbPuller) Close(ctx context.Context) error {
10186
return nil
10287
}
10388

104-
func newDBPuller(db *sql.DB, store taskstore.Store) *dbPuller {
105-
return &dbPuller{db: db, store: store}
89+
func newDBPuller(db *sql.DB, store taskstore.Store, startFrom string) *dbPuller {
90+
return &dbPuller{db: db, store: store, startFrom: startFrom}
10691
}
10792

10893
func newPullQueueManager(db *sql.DB, store taskstore.Store) eventqueue.Manager {
@@ -115,6 +100,14 @@ func newPullQueueManager(db *sql.DB, store taskstore.Store) eventqueue.Manager {
115100

116101
func newPullerProvider(db *sql.DB, store taskstore.Store) eventqueue.PullerProvider {
117102
return func(ctx context.Context, taskID a2a.TaskID) (eventqueue.Puller, error) {
118-
return newDBPuller(db, store), nil
103+
var startFrom sql.NullString
104+
if err := db.QueryRowContext(ctx, `
105+
SELECT COALESCE(MAX(id), "")
106+
FROM task_event
107+
WHERE task_id = ?
108+
`, taskID).Scan(&startFrom); err != nil {
109+
return nil, fmt.Errorf("failed to query task: %w", err)
110+
}
111+
return newDBPuller(db, store, startFrom.String), nil
119112
}
120113
}

0 commit comments

Comments
 (0)