]> git.giacomolli.uk - onechat/blobdiff - schedule.go
Order model and chat declarations
[onechat] / schedule.go
index 28a5a767777bd341e880b046dde2fac9e8d7f675..5fa35da63cb00d6cbbb13aaf6aba6ae055120a23 100644 (file)
@@ -48,6 +48,40 @@ const (
        scheduleLateLine = "This run is late: the daemon was stopped at that time."
 )
 
        scheduleLateLine = "This run is late: the daemon was stopped at that time."
 )
 
+// Schedules holds the schedules of the agent: tasks that start a turn at one
+// time or at the times of a cron expression. Each change writes the file whole
+// before it returns, so a schedule survives a restart. The mutex protects the
+// schedules, because the tools change them from a turn while Run reads them.
+// changed tells Run that a set or a remove changed the times.
+type Schedules struct {
+       path    string
+       owner   string
+       clock   func() time.Time
+       logger  *slog.Logger
+       changed chan struct{}
+
+       mutex sync.Mutex
+       file  ScheduleFile
+}
+
+// ScheduleFile is the content of the file of the schedules: the ID of the last
+// schedule, so that an ID is never used again, and the schedules.
+type ScheduleFile struct {
+       Next      int        `json:"next"`
+       Schedules []Schedule `json:"schedules"`
+}
+
+// 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 {
+       Owner string    `json:"owner"`
+       ID    int       `json:"id"`
+       Text  string    `json:"text"`
+       Cron  string    `json:"cron"`
+       Due   time.Time `json:"due"`
+}
+
 // openSchedules reads the schedules of the file at path. A missing file holds
 // 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
 // openSchedules reads the schedules of the file at path. A missing file holds
 // 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
@@ -107,22 +141,6 @@ func openSchedules(path, owner string, clock func() time.Time, logger *slog.Logg
        return s, nil
 }
 
        return s, nil
 }
 
-// Schedules holds the schedules of the agent: tasks that start a turn at one
-// time or at the times of a cron expression. Each change writes the file whole
-// before it returns, so a schedule survives a restart. The mutex protects the
-// schedules, because the tools change them from a turn while Run reads them.
-// changed tells Run that a set or a remove changed the times.
-type Schedules struct {
-       path    string
-       owner   string
-       clock   func() time.Time
-       logger  *slog.Logger
-       changed chan struct{}
-
-       mutex sync.Mutex
-       file  ScheduleFile
-}
-
 // Run starts the runs of the due schedules in l until ctx ends. It waits until
 // the first due time, at most scheduleClockWaitTimeout at a time, and also
 // wakes at a set or a remove. So a schedule that came due while the daemon was
 // Run starts the runs of the due schedules in l until ctx ends. It waits until
 // the first due time, at most scheduleClockWaitTimeout at a time, and also
 // wakes at a set or a remove. So a schedule that came due while the daemon was
@@ -420,24 +438,6 @@ func (s *Schedules) save() error {
        return nil
 }
 
        return nil
 }
 
-// ScheduleFile is the content of the file of the schedules: the ID of the last
-// schedule, so that an ID is never used again, and the schedules.
-type ScheduleFile struct {
-       Next      int        `json:"next"`
-       Schedules []Schedule `json:"schedules"`
-}
-
-// 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 {
-       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
 // location of now. The first line starts with [Schedule ID] and gives the due
 // time and once or the cron expression. Then come the text, the line about a
 // Message returns the text of a run at now for the log, with the times in the
 // location of now. The first line starts with [Schedule ID] and gives the due
 // time and once or the cron expression. Then come the text, the line about a