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 // Len returns the number of schedules.
213 func (s *Schedules) Len() int {
215 defer s.mutex.Unlock()
217 return len(s.file.Schedules)
220 // Remove removes the schedule id and returns the answer of the tool.
221 func (s *Schedules) Remove(id int) (string, error) {
223 defer s.mutex.Unlock()
225 index := slices.IndexFunc(s.file.Schedules, func(schedule Schedule) bool {
226 return schedule.ID == id
230 return "", fmt.Errorf("no schedule %d", id)
233 s.file.Schedules = slices.Delete(s.file.Schedules, index, index+1)
241 return fmt.Sprintf("Schedule %d removed.", id), nil
244 // deliver logs the message of each due schedule in l, in due order, as a work
245 // message of scheduleSource, and wakes the loop once with all of them. A
246 // schedule ends its run with Done after its message is durable in the log. A
247 // crash between the two makes the run repeat once.
248 func (s *Schedules) deliver(ctx context.Context, l *TurnLoop) error {
251 for _, schedule := range s.Due(now) {
252 message, err := l.Work(scheduleSource, schedule.Message(now), "")
254 return fmt.Errorf("log a schedule: %w", err)
257 // A stop of the daemon during the flush is no failure: the
258 // schedule stays due and runs at the next start.
260 if ctx.Err() != nil {
265 return fmt.Errorf("log a schedule: %w", err)
268 err = s.Done(schedule, now)
273 turns = append(turns, message)
281 // Due returns the schedules whose time is not after now, by due time.
282 func (s *Schedules) Due(now time.Time) []Schedule {
284 for _, schedule := range s.sorted() {
285 if !schedule.Due.After(now) {
286 due = append(due, schedule)
293 // Done ends the run of schedule at now: a one-time schedule goes, and a
294 // repeating schedule gets the next time after now. So a repeating schedule that
295 // missed several times runs once. When a remove took the schedule first, Done
297 func (s *Schedules) Done(schedule Schedule, now time.Time) error {
299 defer s.mutex.Unlock()
301 index := slices.IndexFunc(s.file.Schedules, func(held Schedule) bool {
302 return held.ID == schedule.ID
309 if schedule.Cron == "" {
310 s.file.Schedules = slices.Delete(s.file.Schedules, index, index+1)
315 cron, err := scheduleParseCron(schedule.Cron)
317 return fmt.Errorf("schedule %d: %w", schedule.ID, err)
320 next, found := cron.Next(now)
322 s.file.Schedules = slices.Delete(s.file.Schedules, index, index+1)
327 s.file.Schedules[index].Due = next
332 // Wait returns how long after now the first schedule is due, or false when
333 // there is no schedule.
334 func (s *Schedules) Wait(now time.Time) (time.Duration, bool) {
335 schedules := s.sorted()
336 if len(schedules) == 0 {
340 return max(schedules[0].Due.Sub(now), 0), true
343 // change wakes Run after a set or a remove.
344 func (s *Schedules) change() {
346 case s.changed <- struct{}{}:
351 // sorted returns a copy of the schedules by due time, then by ID.
352 func (s *Schedules) sorted() []Schedule {
354 schedules := slices.Clone(s.file.Schedules)
357 slices.SortFunc(schedules, func(a, b Schedule) int {
358 return cmp.Or(a.Due.Compare(b.Due), cmp.Compare(a.ID, b.ID))
364 // save writes the file whole, with mode 0600. The caller holds the mutex.
365 func (s *Schedules) save() error {
366 data, err := json.Marshal(s.file)
368 err = writeFileAtomic(s.path, append(data, '\n'))
372 return fmt.Errorf("save schedules: %w", err)
378 // ScheduleFile is the content of the file of the schedules: the ID of the last
379 // schedule, so that an ID is never used again, and the schedules.
380 type ScheduleFile struct {
381 Next int `json:"next"`
382 Schedules []Schedule `json:"schedules"`
385 // Schedule is one schedule: its ID, the task for the agent, the cron expression
386 // of a repeating schedule, empty for a one-time schedule, and its next time.
387 type Schedule struct {
389 Text string `json:"text"`
390 Cron string `json:"cron"`
391 Due time.Time `json:"due"`
394 // Message returns the text of a run at now for the log, with the times in the
395 // location of now. The first line starts with [Schedule ID] and gives the due
396 // time and once or the cron expression. Then come the text, the line about a
397 // late run when now is more than scheduleLate after the due time, and the line
398 // about the empty reply.
399 func (s Schedule) Message(now time.Time) string {
401 "[Schedule " + strconv.Itoa(s.ID) + "] " + s.Due.In(now.Location()).Format(scheduleTimeLayout) + ", " + s.repeats(),
405 if now.Sub(s.Due) > scheduleLate {
406 lines = append(lines, scheduleLateLine)
409 return strings.Join(append(lines, scheduleEmptyReply), "\n")
412 // repeats returns once for a one-time schedule, or else repeats and the cron
414 func (s Schedule) repeats() string {
419 return "repeats " + s.Cron