236 lines
6.8 KiB
Go
236 lines
6.8 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var weekdays = []string{"sun", "mon", "tue", "wed", "thu", "fri", "sat"}
|
|
|
|
// ParseDefinition validates new publications. Parse deliberately remains a
|
|
// historical reader for immutable versions containing deprecated settings.
|
|
func ParseDefinition(source string) (protocol.Graph, error) {
|
|
g, err := Parse(source)
|
|
if err == nil && g.Settings != nil {
|
|
err = fmt.Errorf("embedded schedules are read-only legacy definitions; save a separate schedule with a pipeline version and run information instead")
|
|
}
|
|
return g, err
|
|
}
|
|
|
|
func ValidateSchedule(rule protocol.Schedule) error {
|
|
text := ""
|
|
if len(rule.Days) > 0 {
|
|
text += "days=" + strings.Join(rule.Days, ",") + "\n"
|
|
}
|
|
if len(rule.Times) > 0 {
|
|
text += "times=" + strings.Join(rule.Times, ",") + "\n"
|
|
}
|
|
if rule.Every != "" {
|
|
text += "every=" + rule.Every + "\n"
|
|
}
|
|
if rule.Timezone != "" {
|
|
text += "timezone=" + rule.Timezone + "\n"
|
|
}
|
|
parsed, err := parseSchedule(text)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The structured form is not a second parser dialect: reject whitespace,
|
|
// embedded lines and everyday-as-an-array-element rather than validating a
|
|
// normalized rule and then executing the original, different one.
|
|
if strings.Join(parsed.Days, ",") != strings.Join(rule.Days, ",") || strings.Join(parsed.Times, ",") != strings.Join(rule.Times, ",") || parsed.Every != rule.Every || parsed.Timezone != rule.Timezone {
|
|
return fmt.Errorf("schedule rule must use exact weekday/time values; omit days for everyday")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parseSettings(source string) (string, *protocol.Settings, error) {
|
|
lines := strings.Split(strings.ReplaceAll(source, "\r\n", "\n"), "\n")
|
|
first := 0
|
|
for first < len(lines) && strings.TrimSpace(lines[first]) == "" {
|
|
first++
|
|
}
|
|
if first == len(lines) || strings.TrimSpace(lines[first]) != "# settings" {
|
|
return source, nil, nil
|
|
}
|
|
settings := &protocol.Settings{}
|
|
active := ""
|
|
var content []string
|
|
promptSeen := false
|
|
for i := first + 1; i < len(lines); i++ {
|
|
line := strings.TrimSpace(lines[i])
|
|
if active != "" {
|
|
if line != "```" {
|
|
content = append(content, lines[i])
|
|
continue
|
|
}
|
|
text := strings.Join(content, "\n")
|
|
if active == "prompt" {
|
|
settings.Prompt = text
|
|
} else {
|
|
schedule, err := parseSchedule(text)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
settings.Schedules = append(settings.Schedules, schedule)
|
|
}
|
|
active = ""
|
|
content = nil
|
|
continue
|
|
}
|
|
if strings.HasPrefix(line, "# stage ") {
|
|
if len(settings.Schedules) == 0 || strings.TrimSpace(settings.Prompt) == "" {
|
|
return "", nil, fmt.Errorf("settings require at least one schedule and a nonempty prompt")
|
|
}
|
|
return strings.Join(lines[i:], "\n"), settings, nil
|
|
}
|
|
switch line {
|
|
case "":
|
|
case "```schedule":
|
|
active = "schedule"
|
|
case "```prompt":
|
|
if promptSeen {
|
|
return "", nil, fmt.Errorf("duplicate settings prompt")
|
|
}
|
|
promptSeen = true
|
|
active = "prompt"
|
|
default:
|
|
return "", nil, fmt.Errorf("unexpected settings text %q", line)
|
|
}
|
|
}
|
|
return "", nil, fmt.Errorf("settings must precede a stage; all blocks must be closed")
|
|
}
|
|
|
|
func parseSchedule(text string) (protocol.Schedule, error) {
|
|
s := protocol.Schedule{}
|
|
values := map[string]string{}
|
|
for _, line := range strings.Split(text, "\n") {
|
|
if strings.TrimSpace(line) == "" {
|
|
continue
|
|
}
|
|
key, value, ok := strings.Cut(line, "=")
|
|
key = strings.TrimSpace(key)
|
|
value = strings.TrimSpace(value)
|
|
if !ok || value == "" {
|
|
return s, fmt.Errorf("invalid schedule setting %q", line)
|
|
}
|
|
if _, ok := values[key]; ok {
|
|
return s, fmt.Errorf("duplicate schedule setting %s", key)
|
|
}
|
|
values[key] = value
|
|
switch key {
|
|
case "days", "times", "every", "timezone":
|
|
default:
|
|
return s, fmt.Errorf("unknown schedule setting %s", key)
|
|
}
|
|
}
|
|
if (values["times"] != "") == (values["every"] != "") {
|
|
return s, fmt.Errorf("schedule requires exactly one of times or every (not both)")
|
|
}
|
|
if days := values["days"]; days != "" && days != "everyday" {
|
|
seen := map[string]bool{}
|
|
for _, day := range strings.Split(days, ",") {
|
|
day = strings.TrimSpace(day)
|
|
valid := false
|
|
for _, name := range weekdays {
|
|
if day == name {
|
|
valid = true
|
|
}
|
|
}
|
|
if !valid || seen[day] {
|
|
return s, fmt.Errorf("invalid or duplicate schedule day %q", day)
|
|
}
|
|
seen[day] = true
|
|
s.Days = append(s.Days, day)
|
|
}
|
|
}
|
|
if values["times"] != "" {
|
|
seen := map[string]bool{}
|
|
for _, clock := range strings.Split(values["times"], ",") {
|
|
clock = strings.TrimSpace(clock)
|
|
parsed, err := time.Parse("15:04", clock)
|
|
if err != nil || parsed.Format("15:04") != clock || seen[clock] {
|
|
return s, fmt.Errorf("invalid or duplicate schedule time %q", clock)
|
|
}
|
|
seen[clock] = true
|
|
s.Times = append(s.Times, clock)
|
|
}
|
|
}
|
|
s.Every = values["every"]
|
|
if s.Every != "" {
|
|
duration, err := time.ParseDuration(s.Every)
|
|
if err != nil || duration < time.Minute {
|
|
return s, fmt.Errorf("schedule every must be a duration of at least 1m")
|
|
}
|
|
}
|
|
s.Timezone = values["timezone"]
|
|
if s.Timezone != "" {
|
|
if _, err := time.LoadLocation(s.Timezone); err != nil {
|
|
return s, fmt.Errorf("invalid schedule timezone: %w", err)
|
|
}
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func ScheduleLocation(s protocol.Schedule) (*time.Location, error) {
|
|
if s.Timezone == "" {
|
|
return time.Local, nil
|
|
}
|
|
return time.LoadLocation(s.Timezone)
|
|
}
|
|
func ScheduleDay(s protocol.Schedule, t time.Time) bool {
|
|
if len(s.Days) == 0 {
|
|
return true
|
|
}
|
|
for _, day := range s.Days {
|
|
if day == weekdays[t.Weekday()] {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// NextSchedule preserves elapsed interval cadence. Weekday filtering occurs at
|
|
// dispatch, so disallowed days never reset the interval anchor.
|
|
func NextSchedule(s protocol.Schedule, after time.Time) (time.Time, error) {
|
|
if s.Every != "" {
|
|
d, err := time.ParseDuration(s.Every)
|
|
if err != nil || d < time.Minute {
|
|
return time.Time{}, fmt.Errorf("invalid interval")
|
|
}
|
|
return after.Add(d), nil
|
|
}
|
|
loc, err := ScheduleLocation(s)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
local := after.In(loc)
|
|
best := time.Time{}
|
|
for day := 0; day < 9; day++ {
|
|
date := time.Date(local.Year(), local.Month(), local.Day()+day, 12, 0, 0, 0, loc)
|
|
if !ScheduleDay(s, date) {
|
|
continue
|
|
}
|
|
for _, clock := range s.Times {
|
|
parsed, err := time.Parse("15:04", clock)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
candidate := time.Date(date.Year(), date.Month(), date.Day(), parsed.Hour(), parsed.Minute(), 0, 0, loc)
|
|
// Nonexistent spring-forward wall times are skipped. Fall-back times fire once.
|
|
if candidate.Format("15:04") != clock || !candidate.After(after) {
|
|
continue
|
|
}
|
|
if best.IsZero() || candidate.Before(best) {
|
|
best = candidate
|
|
}
|
|
}
|
|
}
|
|
if best.IsZero() {
|
|
return best, fmt.Errorf("schedule has no next occurrence")
|
|
}
|
|
return best, nil
|
|
}
|