18 // scheduleFileName is the file of the schedules in the directory of the
20 scheduleFileName = "schedules.json"
22 // scheduleTextBytes is the most bytes of the text of a schedule.
23 scheduleTextBytes = 4096
25 // scheduleAtLayout is the layout of the time of a one-time schedule.
26 scheduleAtLayout = "2006-01-02 15:04"
28 // scheduleTimeLayout is the layout of the times in the answers of the
29 // tools and in the log.
30 scheduleTimeLayout = "Monday 2006-01-02 15:04 -07:00"
32 // scheduleSource is the source of the work events of the runs.
33 scheduleSource = "schedule"
35 // scheduleCheck is the longest wait of Run before it looks at the times
36 // again, so that it sees a suspend of the machine or a change of the
38 scheduleCheck = time.Minute
40 // scheduleLate is how long after its time a run is late.
41 scheduleLate = time.Minute
43 // scheduleEmptyReply is the last line of the message of a run.
44 scheduleEmptyReply = "If nothing in this task needs the user, end your turn with an empty reply."
46 // scheduleLateLine tells the agent that a run is late.
47 scheduleLateLine = "This run is late: the daemon was stopped at that time."
50 // openSchedules reads the schedules of the file at path. A missing file holds
51 // none. The clock gives the time and the time zone of the schedules.
52 func openSchedules(path string, clock func() time.Time) (*Schedules, error) {
57 changed: make(chan struct{}, 1),
60 data, err := os.ReadFile(path)
61 if errors.Is(err, os.ErrNotExist) {
66 return nil, fmt.Errorf("read schedules: %w", err)
69 err = json.Unmarshal(data, &s.file)
71 return nil, fmt.Errorf("%s: %w", path, err)
74 for _, schedule := range s.file.Schedules {
75 if schedule.Cron == "" {
79 _, err = scheduleParseCron(schedule.Cron)
81 return nil, fmt.Errorf("%s: schedule %d: %w", path, schedule.ID, err)
88 // Schedules holds the schedules of the agent: tasks that start a turn at one
89 // time or at the times of a cron expression. Each change writes the file whole
90 // before it returns, so a schedule survives a restart. The mutex protects the
91 // schedules, because the tools change them from a turn while Run reads them.
92 // changed tells Run that a set or a remove changed the times.
93 type Schedules struct {
95 clock func() time.Time
103 // Run starts the runs of the due schedules in l until ctx ends. It waits until
104 // the first due time, at most check at a time, and also wakes at a set or a
105 // remove. So a schedule that came due while the daemon was stopped runs at the
106 // start. It returns the error of a write of the log or of the file.
107 func (s *Schedules) Run(ctx context.Context, l *TurnLoop) error {
109 err := s.deliver(ctx, l)
114 var timer *time.Timer
115 var timeout <-chan time.Time
116 wait, found := s.Wait(s.clock())
118 timer = time.NewTimer(min(wait, s.check))
135 // Set adds a schedule with text and returns the answer of the tool. Exactly one
136 // of at and cron is not nil. at is a local time in scheduleAtLayout after now,
137 // and it goes through scheduleWallTime, so a time in the hour that the clock
138 // repeats is its first occurrence. cron is an expression of scheduleParseCron.
139 func (s *Schedules) Set(text string, at, cron *string) (string, error) {
140 text = strings.TrimSpace(text)
141 if text == "" || len(text) > scheduleTextBytes {
142 return "", fmt.Errorf("the text must have 1 to %d bytes", scheduleTextBytes)
146 schedule := Schedule{
151 case (at == nil) == (cron == nil):
152 return "", errors.New("give either at or cron, and null for the other")
154 parsed, err := time.Parse(scheduleAtLayout, *at)
156 return "", fmt.Errorf("at must be a local time such as %s", now.Format(scheduleAtLayout))
159 schedule.Due = scheduleWallTime(parsed.Year(), parsed.Month(), parsed.Day(), parsed.Hour(), parsed.Minute(), now.Location())
160 if !schedule.Due.After(now) {
161 return "", fmt.Errorf("the time %s is not after now, %s", schedule.Due.Format(scheduleTimeLayout), now.Format(scheduleTimeLayout))
164 parsed, err := scheduleParseCron(*cron)
169 next, found := parsed.Next(now)
171 return "", fmt.Errorf("the cron expression %q matches no time in %d years", *cron, scheduleCronYears)
174 schedule.Cron = parsed.String()
179 defer s.mutex.Unlock()
182 schedule.ID = s.file.Next
183 s.file.Schedules = append(s.file.Schedules, schedule)
191 return fmt.Sprintf("Schedule %d set. Next: %s.", schedule.ID, schedule.Due.In(now.Location()).Format(scheduleTimeLayout)), nil
194 // List returns the answer of the tool: one line for each schedule, by due time,
195 // with its ID, its next time, its cron expression or once, and its text on one
197 func (s *Schedules) List() string {
198 location := s.clock().Location()
199 schedules := s.sorted()
200 if len(schedules) == 0 {
201 return "No schedules."
204 lines := make([]string, 0, len(schedules))
205 for _, schedule := range schedules {
206 lines = append(lines, strconv.Itoa(schedule.ID)+" | "+schedule.Due.In(location).Format(scheduleTimeLayout)+" | "+cmp.Or(schedule.Cron, "once")+" | "+strings.Join(strings.Fields(schedule.Text), " "))
209 return strings.Join(lines, "\n")
212 // Remove removes the schedule id and returns the answer of the tool.
213 func (s *Schedules) Remove(id int) (string, error) {
215 defer s.mutex.Unlock()
217 index := slices.IndexFunc(s.file.Schedules, func(schedule Schedule) bool {
218 return schedule.ID == id
222 return "", fmt.Errorf("no schedule %d", id)
225 s.file.Schedules = slices.Delete(s.file.Schedules, index, index+1)
233 return fmt.Sprintf("Schedule %d removed.", id), nil
236 // deliver logs the message of each due schedule in l, in due order, as a work
237 // message of scheduleSource, and wakes the loop once with all of them. A
238 // schedule ends its run with Done after its message is durable in the log. A
239 // crash between the two makes the run repeat once.
240 func (s *Schedules) deliver(ctx context.Context, l *TurnLoop) error {
243 for _, schedule := range s.Due(now) {
244 message, err := l.Work(scheduleSource, schedule.Message(now), "")
246 return fmt.Errorf("log a schedule: %w", err)
249 // A stop of the daemon during the flush is no failure: the
250 // schedule stays due and runs at the next start.
252 if ctx.Err() != nil {
257 return fmt.Errorf("log a schedule: %w", err)
260 err = s.Done(schedule, now)
265 turns = append(turns, message)
273 // Due returns the schedules whose time is not after now, by due time.
274 func (s *Schedules) Due(now time.Time) []Schedule {
276 for _, schedule := range s.sorted() {
277 if !schedule.Due.After(now) {
278 due = append(due, schedule)
285 // Done ends the run of schedule at now: a one-time schedule goes, and a
286 // repeating schedule gets the next time after now. So a repeating schedule that
287 // missed several times runs once. When a remove took the schedule first, Done
289 func (s *Schedules) Done(schedule Schedule, now time.Time) error {
291 defer s.mutex.Unlock()
293 index := slices.IndexFunc(s.file.Schedules, func(held Schedule) bool {
294 return held.ID == schedule.ID
301 if schedule.Cron == "" {
302 s.file.Schedules = slices.Delete(s.file.Schedules, index, index+1)
307 cron, err := scheduleParseCron(schedule.Cron)
309 return fmt.Errorf("schedule %d: %w", schedule.ID, err)
312 next, found := cron.Next(now)
314 s.file.Schedules = slices.Delete(s.file.Schedules, index, index+1)
319 s.file.Schedules[index].Due = next
324 // Wait returns how long after now the first schedule is due, or false when
325 // there is no schedule.
326 func (s *Schedules) Wait(now time.Time) (time.Duration, bool) {
327 schedules := s.sorted()
328 if len(schedules) == 0 {
332 return max(schedules[0].Due.Sub(now), 0), true
335 // change wakes Run after a set or a remove.
336 func (s *Schedules) change() {
338 case s.changed <- struct{}{}:
343 // sorted returns a copy of the schedules by due time, then by ID.
344 func (s *Schedules) sorted() []Schedule {
346 schedules := slices.Clone(s.file.Schedules)
349 slices.SortFunc(schedules, func(a, b Schedule) int {
350 return cmp.Or(a.Due.Compare(b.Due), cmp.Compare(a.ID, b.ID))
356 // save writes the file whole, with mode 0600. The caller holds the mutex.
357 func (s *Schedules) save() error {
358 data, err := json.Marshal(s.file)
360 err = writeFileAtomic(s.path, append(data, '\n'))
364 return fmt.Errorf("save schedules: %w", err)
370 // ScheduleFile is the content of the file of the schedules: the ID of the last
371 // schedule, so that an ID is never used again, and the schedules.
372 type ScheduleFile struct {
373 Next int `json:"next"`
374 Schedules []Schedule `json:"schedules"`
377 // Schedule is one schedule: its ID, the task for the agent, the cron expression
378 // of a repeating schedule, empty for a one-time schedule, and its next time.
379 type Schedule struct {
381 Text string `json:"text"`
382 Cron string `json:"cron"`
383 Due time.Time `json:"due"`
386 // Message returns the text of a run at now for the log, with the times in the
387 // location of now. The first line starts with [Schedule ID] and gives the due
388 // time and once or the cron expression. Then come the text, the line about a
389 // late run when now is more than scheduleLate after the due time, and the line
390 // about the empty reply.
391 func (s Schedule) Message(now time.Time) string {
393 "[Schedule " + strconv.Itoa(s.ID) + "] " + s.Due.In(now.Location()).Format(scheduleTimeLayout) + ", " + s.repeats(),
397 if now.Sub(s.Due) > scheduleLate {
398 lines = append(lines, scheduleLateLine)
401 return strings.Join(append(lines, scheduleEmptyReply), "\n")
404 // repeats returns once for a one-time schedule, or else repeats and the cron
406 func (s Schedule) repeats() string {
411 return "repeats " + s.Cron