-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminder.go
62 lines (46 loc) · 1.45 KB
/
reminder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package reminder
import (
"log/slog"
"time"
"github.com/adhocore/gronx"
"github.com/sfomuseum/iso8601duration"
)
// Reminder is a struct encapsulating details about a reminder message.
type Reminder struct {
// A unique identifier for the reminder.
Id int64 `json:"id"`
// A valid cron expression (that can be parsed by adhocore/gronx) for the scheduled event.
Schedule string `json:"schedule"`
// An ISO8601 duration string indicating the amount of time before the scheduled event is due to start sending reminders.
NotifyBefore string `json:"notify_before"`
// The message body of the reminder.
Message string `json:"message"`
// The address where the reminder should be delivered to.
DeliverTo string `json:deliver_to"`
// The address where the reminder should be delivered from.
DeliverFrom string `json:"deliver_from"`
}
// Return a boolean flag where the reminder is due to be dispatched.
func (r *Reminder) IsDue() (bool, error) {
next, err := gronx.NextTick(r.Schedule, true)
if err != nil {
return false, err
}
logger := slog.Default()
logger = logger.With("reminder", r.Id)
dur, err := duration.FromString(r.NotifyBefore)
if err != nil {
return false, err
}
d := dur.ToDuration()
now := time.Now()
logger.Debug("Now", "t", now)
logger.Debug("Next", "t", next)
trigger := next.Add(-d)
logger.Debug("Trigger", "t", trigger)
if trigger.After(now) {
return false, nil
}
logger.Debug("Reminder is due")
return true, nil
}