package catalogue import ( "fmt" "sort" "strings" "time" ) // BusyField is the resource field. const BusyField = "busy" // The bounds of one ask (ADR 0335), the node-engine's own, held here near the author. const ( BusyTimeoutDefault = 10 * time.Second BusyTimeoutCeiling = time.Minute ) // Busy is one container's busy, read. type Busy struct { Command string Timeout time.Duration } // ReadBusy reads a resource's `busy` field, its default applied. False when it carries none. func ReadBusy(r map[string]any) (Busy, bool, []string) { raw, present := r[BusyField] if !present { return Busy{}, false, nil } id := fmt.Sprint(r["id"]) fields, ok := raw.(map[string]any) if !ok { return Busy{}, true, []string{fmt.Sprintf("%s: busy is %T; it is an object with a command", id, raw)} } var problems []string keys := make([]string, 0, len(fields)) for k := range fields { keys = append(keys, k) } sort.Strings(keys) for _, k := range keys { if k != "command" && k != "timeout" { problems = append(problems, fmt.Sprintf("%s: its busy says %q; busy has a command and a timeout", id, k)) } } b := Busy{Timeout: BusyTimeoutDefault} if v, has := fields["command"]; has { s, ok := v.(string) if !ok { problems = append(problems, fmt.Sprintf("%s: its busy's command is %T; it is text", id, v)) } b.Command = strings.TrimSpace(s) } if v, has := fields["timeout"]; has { s, _ := v.(string) d, err := time.ParseDuration(strings.TrimSpace(s)) switch { case err != nil: problems = append(problems, fmt.Sprintf("%s: its busy's timeout is %v; it is a duration such as \"10s\"", id, v)) case d <= 0 || d > BusyTimeoutCeiling: problems = append(problems, fmt.Sprintf("%s: an ask of its busy takes more than nothing and at most %s, not %s", id, BusyTimeoutCeiling, d)) default: b.Timeout = d } } return b, true, problems }