]> git.giacomolli.uk - onechat/blobdiff - schedule.go
Rewrite the README as a user guide
[onechat] / schedule.go
index d2f53e095887749949320f3af79252f556ba3db8..b3a65ea94193a6431693313ca6f5833d836ccefb 100644 (file)
@@ -6,7 +6,7 @@ import (
        "encoding/json"
        "errors"
        "fmt"
        "encoding/json"
        "errors"
        "fmt"
-       "log"
+       "log/slog"
        "os"
        "slices"
        "strconv"
        "os"
        "slices"
        "strconv"
@@ -49,11 +49,13 @@ const (
 )
 
 // openSchedules reads the schedules of the file at path. A missing file holds
 )
 
 // openSchedules reads the schedules of the file at path. A missing file holds
-// none. The clock gives the time and the time zone of the schedules, and logger
-// gets one line for each run.
-func openSchedules(path string, clock func() time.Time, logger *log.Logger) (*Schedules, error) {
+// none. The owner names its agent. Schedules without an owner take this name. A
+// different stored owner is an error. The clock gives the time and the time
+// zone of the schedules, and logger gets one line for each run.
+func openSchedules(path, owner string, clock func() time.Time, logger *slog.Logger) (*Schedules, error) {
        s := &Schedules{
                path:    path,
        s := &Schedules{
                path:    path,
+               owner:   owner,
                clock:   clock,
                logger:  logger,
                check:   scheduleCheck,
                clock:   clock,
                logger:  logger,
                check:   scheduleCheck,
@@ -74,7 +76,18 @@ func openSchedules(path string, clock func() time.Time, logger *log.Logger) (*Sc
                return nil, fmt.Errorf("%s: %w", path, err)
        }
 
                return nil, fmt.Errorf("%s: %w", path, err)
        }
 
-       for _, schedule := range s.file.Schedules {
+       changed := false
+       for i := range s.file.Schedules {
+               schedule := &s.file.Schedules[i]
+               if schedule.Owner == "" {
+                       schedule.Owner = owner
+                       changed = true
+               }
+
+               if schedule.Owner != owner {
+                       return nil, fmt.Errorf("schedule %d belongs to %q, not %q", schedule.ID, schedule.Owner, owner)
+               }
+
                if schedule.Cron == "" {
                        continue
                }
                if schedule.Cron == "" {
                        continue
                }
@@ -85,6 +98,13 @@ func openSchedules(path string, clock func() time.Time, logger *log.Logger) (*Sc
                }
        }
 
                }
        }
 
+       if changed {
+               err = s.save()
+               if err != nil {
+                       return nil, fmt.Errorf("record schedule owner: %w", err)
+               }
+       }
+
        return s, nil
 }
 
        return s, nil
 }
 
@@ -95,8 +115,9 @@ func openSchedules(path string, clock func() time.Time, logger *log.Logger) (*Sc
 // changed tells Run that a set or a remove changed the times.
 type Schedules struct {
        path    string
 // changed tells Run that a set or a remove changed the times.
 type Schedules struct {
        path    string
+       owner   string
        clock   func() time.Time
        clock   func() time.Time
-       logger  *log.Logger
+       logger  *slog.Logger
        check   time.Duration
        changed chan struct{}
 
        check   time.Duration
        changed chan struct{}
 
@@ -151,7 +172,8 @@ func (s *Schedules) Set(text string, at, cron *string) (string, error) {
 
        now := s.clock()
        schedule := Schedule{
 
        now := s.clock()
        schedule := Schedule{
-               Text: text,
+               Text:  text,
+               Owner: s.owner,
        }
 
        switch {
        }
 
        switch {
@@ -255,6 +277,10 @@ func (s *Schedules) Remove(id int) (string, error) {
 // the ID of the schedule and, for a late run, how late it is. The line holds no
 // text of the schedule.
 func (s *Schedules) deliver(ctx context.Context, l *TurnLoop) error {
 // the ID of the schedule and, for a late run, how late it is. The line holds no
 // text of the schedule.
 func (s *Schedules) deliver(ctx context.Context, l *TurnLoop) error {
+       if l.agent.config.Name != s.owner {
+               return fmt.Errorf("schedule owner %q does not match agent %q", s.owner, l.agent.config.Name)
+       }
+
        now := s.clock()
 
        var turns []Message
        now := s.clock()
 
        var turns []Message
@@ -281,12 +307,13 @@ func (s *Schedules) deliver(ctx context.Context, l *TurnLoop) error {
                        return err
                }
 
                        return err
                }
 
+               attrs := []any{"id", schedule.ID}
                if schedule.late(now) {
                if schedule.late(now) {
-                       s.logger.Printf("schedule %d ran %s late", schedule.ID, durationText(now.Sub(schedule.Due)))
-               } else {
-                       s.logger.Printf("schedule %d ran", schedule.ID)
+                       attrs = append(attrs, "late", now.Sub(schedule.Due))
                }
 
                }
 
+               s.logger.Info("schedule ran", attrs...)
+
                turns = append(turns, message)
        }
 
                turns = append(turns, message)
        }
 
@@ -401,13 +428,15 @@ type ScheduleFile struct {
        Schedules []Schedule `json:"schedules"`
 }
 
        Schedules []Schedule `json:"schedules"`
 }
 
-// Schedule is one schedule: its ID, the task for the agent, the cron expression
-// of a repeating schedule, empty for a one-time schedule, and its next time.
+// Schedule is one schedule: its owner, its ID, the task for the agent, the cron
+// expression of a repeating schedule, empty for a one-time schedule, and its
+// next time.
 type Schedule struct {
 type Schedule struct {
-       ID   int       `json:"id"`
-       Text string    `json:"text"`
-       Cron string    `json:"cron"`
-       Due  time.Time `json:"due"`
+       Owner string    `json:"owner"`
+       ID    int       `json:"id"`
+       Text  string    `json:"text"`
+       Cron  string    `json:"cron"`
+       Due   time.Time `json:"due"`
 }
 
 // Message returns the text of a run at now for the log, with the times in the
 }
 
 // Message returns the text of a run at now for the log, with the times in the