71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
package zset
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
// ProducerState is a small internal read view, not a new Run wire format.
|
|
// ActiveCount includes inflight; never add InflightCount to capacity usage.
|
|
type ProducerState struct {
|
|
Status string
|
|
ProducerDone bool
|
|
CurrentRound int
|
|
ActiveCount int64
|
|
Blocked bool
|
|
}
|
|
|
|
func (s *Store) ReadProducerState(ctx context.Context, runID, chanKey string) (ProducerState, error) {
|
|
key, err := s.RunMetaKey(runID)
|
|
if err != nil {
|
|
return ProducerState{}, err
|
|
}
|
|
result, err := s.redis.Do(ctx, "HMGET", key, "runId", "status", "producerDone", "currentRound", "activeCount", "blocked:"+chanKey)
|
|
if err != nil {
|
|
return ProducerState{}, gerror.Wrap(err, "read producer state")
|
|
}
|
|
values := result.Strings()
|
|
if len(values) != 6 || values[0] == "" {
|
|
return ProducerState{}, gerror.Wrapf(ErrRunNotFound, "run %q", runID)
|
|
}
|
|
state := ProducerState{Status: values[1], ProducerDone: parseBool(values[2]), CurrentRound: parseInt(values[3]), ActiveCount: parseInt64(values[4]), Blocked: parseBool(values[5])}
|
|
if state.CurrentRound <= 0 || state.ActiveCount < 0 {
|
|
return ProducerState{}, gerror.New("invalid producer run counters")
|
|
}
|
|
return state, nil
|
|
}
|
|
|
|
// ExistingRunTasks uses one bounded HMGET for a producer page. All task states
|
|
// (queued, leased, waiting, success and failed) count as already admitted.
|
|
func (s *Store) ExistingRunTasks(ctx context.Context, runID string, tasks []Task) ([]bool, error) {
|
|
if len(tasks) == 0 {
|
|
return nil, nil
|
|
}
|
|
key, err := s.TaskStateKey(runID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
args := make([]any, 1, len(tasks)+1)
|
|
args[0] = key
|
|
for _, task := range tasks {
|
|
if task.TaskID == "" || task.RunID != runID {
|
|
return nil, gerror.New("invalid task identity in producer state read")
|
|
}
|
|
args = append(args, task.TaskID)
|
|
}
|
|
result, err := s.redis.Do(ctx, "HMGET", args...)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "read existing run tasks")
|
|
}
|
|
values := result.Strings()
|
|
if len(values) != len(tasks) {
|
|
return nil, gerror.New("incomplete HMGET task state response")
|
|
}
|
|
exists := make([]bool, len(tasks))
|
|
for i, value := range values {
|
|
exists[i] = value != ""
|
|
}
|
|
return exists, nil
|
|
}
|