X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/75b9579355e1adcdbb1255a7e8e4582e0a431f3d..786c2b0551517d4befaa35be2c7743f65bd5f1c4:/schedule.go?ds=inline diff --git a/schedule.go b/schedule.go index f3dcfe2..3b4a4ee 100644 --- a/schedule.go +++ b/schedule.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "os" "slices" "strconv" @@ -48,11 +49,13 @@ const ( ) // 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. -func openSchedules(path string, clock func() time.Time) (*Schedules, error) { +// 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) { s := &Schedules{ path: path, clock: clock, + logger: logger, check: scheduleCheck, changed: make(chan struct{}, 1), } @@ -93,6 +96,7 @@ func openSchedules(path string, clock func() time.Time) (*Schedules, error) { type Schedules struct { path string clock func() time.Time + logger *log.Logger check time.Duration changed chan struct{} @@ -244,7 +248,9 @@ func (s *Schedules) Remove(id int) (string, error) { // deliver logs the message of each due schedule in l, in due order, as a work // message of scheduleSource, and wakes the loop once with all of them. A // schedule ends its run with Done after its message is durable in the log. A -// crash between the two makes the run repeat once. +// crash between the two makes the run repeat once. Each run logs one line with +// 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 { now := s.clock() var turns []Message @@ -270,6 +276,12 @@ func (s *Schedules) deliver(ctx context.Context, l *TurnLoop) error { return err } + 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) + } + turns = append(turns, message) } @@ -402,13 +414,19 @@ func (s Schedule) Message(now time.Time) string { s.Text, } - if now.Sub(s.Due) > scheduleLate { + if s.late(now) { lines = append(lines, scheduleLateLine) } return strings.Join(append(lines, scheduleEmptyReply), "\n") } +// late tells if a run at now is late: more than scheduleLate after the due +// time. +func (s Schedule) late(now time.Time) bool { + return now.Sub(s.Due) > scheduleLate +} + // repeats returns once for a one-time schedule, or else repeats and the cron // expression. func (s Schedule) repeats() string {