Skip to content

Commit

Permalink
refactor: align code formatting and improve notification logic
Browse files Browse the repository at this point in the history
  • Loading branch information
revelaction committed Sep 21, 2024
1 parent 891ed9a commit 6f5e62b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 115 deletions.
20 changes: 10 additions & 10 deletions cmd/ical-git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ func tick(ctx context.Context, cancel context.CancelFunc, conf config.Config, sc

err := run(conf, sc)
if err != nil {
// sleep before canceling here, to allow interrupt signals to still work
time.Sleep(time.Minute)
// sleep before canceling here, to allow interrupt signals to still work
time.Sleep(time.Minute)
cancel()
slog.Error("Tick Error, canceling", "error", err)
return
Expand All @@ -190,8 +190,8 @@ func tick(ctx context.Context, cancel context.CancelFunc, conf config.Config, sc
err = run(conf, sc)
slog.Info("🔧 ending tick work")
if err != nil {
// sleep before canceling here, to allow interrupt signals to still work
time.Sleep(time.Minute)
// sleep before canceling here, to allow interrupt signals to still work
time.Sleep(time.Minute)
cancel()
slog.Error("Tick Error, canceling", "error", err)
return
Expand All @@ -205,17 +205,17 @@ func run(conf config.Config, sc *schedule.Scheduler) error {
slog.Info("🚀 starting run")

tickStart := time.Now()
tickStartInConfigLoc := tickStart.In(conf.Location.Location)
tickStartInConfigLoc := tickStart.In(conf.Location.Location)

layout := "2006-01-02 15:04:05 MST"
layout := "2006-01-02 15:04:05 MST"
slog.Info("⏰ Tick start time", "start_local_location", tickStart.Format(layout), "start_config_location", tickStartInConfigLoc.Format(layout))

var f fetch.Fetcher
if conf.IsFetcherGit() {
slog.Info("🧲 Fetch: git")
slog.Info("🧲 Fetch: git")
f = git.New(conf.FetcherGit.Url, conf.FetcherGit.PrivateKeyPath)
} else {
slog.Info("🧲 Fetch: filesystem")
slog.Info("🧲 Fetch: filesystem")
f = filesystem.New(conf.FetcherFilesystem.Directory)
}

Expand All @@ -224,12 +224,12 @@ func run(conf config.Config, sc *schedule.Scheduler) error {
p := ical.NewParser(conf, tickStart)
for f := range ch {
if f.Error != nil {
slog.Error("🧲 Fetch:", "🚨 Error:", f.Error)
slog.Error("🧲 Fetch:", "🚨 Error:", f.Error)
return fmt.Errorf("error: %w", f.Error)
}
err := p.Parse(f)
if err != nil {
slog.Info("👀 Parse: error. Skipping", "error", err)
slog.Info("👀 Parse: error. Skipping", "error", err)
}
}

Expand Down
10 changes: 7 additions & 3 deletions ical/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func (p *Parser) Parse(f fetch.File) error {
slog.Info("📅 Event", "📁", filepath.Base(f.Path), "📌", eventTime.Format("2006-01-02 15:04:05 MST"), "🔖", in)

// Event Alarms
for _, a := range getEventAlarms(event, p.conf.NotifierTypes) {
eventAlarms := getEventAlarms(event, p.conf.NotifierTypes)

for _, a := range eventAlarms {
slog.Info(" : 🔔", "action", a.Action, "durIso", a.DurIso8601, "dur", a.Dur)
if !a.InTickPeriod(eventTime, p.start, p.conf.DaemonTick) {
continue
Expand All @@ -75,8 +77,10 @@ func (p *Parser) Parse(f fetch.File) error {
alarms = append(alarms, a)
}

// Only if there are no event alarms, consider config alarms
if len(alarms) == 0 {
// Only if there are no DEFINED event alarms (it does not matter if
// they are not trigered in this tick period), only then consider
// config alarms
if len(eventAlarms) == 0 {
// Config Alarms
for _, a := range p.conf.Alarms {

Expand Down
95 changes: 3 additions & 92 deletions ical/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,10 @@ END:VCALENDAR
t.Fatalf("Unexpected error: %v", err)
}

// Check the notifications
// only event alarms considered, neither will trigger
notifications := parser.Notifications()
if len(notifications) != 1 {
t.Fatalf("Expected 1 notification, got %d", len(notifications))
}

notification := notifications[0]
if notification.DurIso8601 != "-PT1H" {
t.Errorf("Expected duration', got '%s'", notification.DurIso8601)
}

if notification.Source != "config" {
t.Errorf("Unexpected Source', got '%s'", notification.Source)
if len(notifications) != 0 {
t.Fatalf("Expected 0 notification, got %d", len(notifications))
}
}

Expand Down Expand Up @@ -350,83 +341,3 @@ END:VCALENDAR
t.Errorf("Unexpected Source', got '%s'", notification.Source)
}
}

func TestParseTwoConfigAlarmTriggered(t *testing.T) {

configData := []byte(`
timezone = "Europe/Berlin"
tick = "24h"
notifiers = ["desktop"]
alarms = [
{type = "desktop", when = "-PT1H"},
{type = "desktop", when = "-PT2H"},
]
`)

conf, err := config.Load(configData)
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}

start := time.Date(2024, 12, 01, 0, 0, 0, 0, time.UTC)
parser := NewParser(conf, start)

// Test data
icalData := []byte(`
BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
SUMMARY:Event with Alarms
DTSTART:20241201T100000Z
BEGIN:VALARM
TRIGGER:-P1D
ACTION:DISPLAY
DESCRIPTION:Reminder 1 day before
END:VALARM
BEGIN:VALARM
TRIGGER:-P2D
ACTION:DISPLAY
DESCRIPTION:Reminder 1 hour before
END:VALARM
END:VEVENT
END:VCALENDAR
`)

// Parse the iCal data
file := fetch.File{
Path: "",
Content: icalData,
Error: nil,
}
err = parser.Parse(file)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

// Check the notifications
notifications := parser.Notifications()
if len(notifications) != 2 {
t.Fatalf("Expected 1 notification, got %d", len(notifications))
}

notification := notifications[0]
if notification.DurIso8601 != "-PT1H" {
t.Errorf("Expected duration', got '%s'", notification.DurIso8601)
}

if notification.Source != "config" {
t.Errorf("Unexpected Source', got '%s'", notification.Source)
}

notification = notifications[1]
if notification.DurIso8601 != "-PT2H" {
t.Errorf("Expected duration', got '%s'", notification.DurIso8601)
}

if notification.Source != "config" {
t.Errorf("Unexpected Source', got '%s'", notification.Source)
}
}
20 changes: 10 additions & 10 deletions notify/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func (s *Scheduler) Schedule(notifications []notify.Notification, tickStart time

s.initializeNotifiers()

slog.Info("🚦 Schedule:", "num_notifications", len(notifications))
slog.Info("🚦 Schedule:", "num_notifications", len(notifications))

for _, n := range notifications {

dur := n.Time.Sub(tickStart)
slog.Info("🚦 Schedule: 🔔", "📁", filepath.Base(n.EventPath), "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "🔖", dur.Truncate(1*time.Second), "durIso", n.DurIso8601, "type", n.Type, "source", n.Source)
slog.Info("🚦 Schedule: 🔔", "📁", filepath.Base(n.EventPath), "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "🔖", dur.Truncate(1*time.Second), "durIso", n.DurIso8601, "type", n.Type, "source", n.Source)
//dur = 3 * time.Second // Hack

f, err := s.getNotifyFunc(n)
Expand All @@ -52,7 +52,7 @@ func (s *Scheduler) Schedule(notifications []notify.Notification, tickStart time
s.timers = append(s.timers, timer)
}

slog.Info("🚦 Schedule:", "num_timers", len(s.timers))
slog.Info("🚦 Schedule:", "num_timers", len(s.timers))

return nil
}
Expand All @@ -70,21 +70,21 @@ func (s *Scheduler) getNotifyFunc(n notify.Notification) (func(), error) {
f = func() {
err := s.telegram.Notify(n)
if err != nil {
slog.Error("🚚 Notification:", "Send?", "🛑", "📁", filepath.Base(n.EventPath), "error", err, "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "type", n.Type, "source", n.Source)
slog.Error("🚚 Notification:", "Send?", "🛑", "📁", filepath.Base(n.EventPath), "error", err, "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "type", n.Type, "source", n.Source)
fmt.Printf("Could not deliver telegram notfication: %s", err)
return
return
}
slog.Info("🚚 Notification:", "Send?", "✅", "📁", filepath.Base(n.EventPath), "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "type", n.Type, "source", n.Source)
slog.Info("🚚 Notification:", "Send?", "✅", "📁", filepath.Base(n.EventPath), "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "type", n.Type, "source", n.Source)
}

case "desktop":
f = func() {
err := s.desktop.Notify(n)
if err != nil {
slog.Error("🚚 Notification:", "Send?", "🛑", "📁", filepath.Base(n.EventPath), "error", err, "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "type", n.Type, "source", n.Source)
return
slog.Error("🚚 Notification:", "Send?", "🛑", "📁", filepath.Base(n.EventPath), "error", err, "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "type", n.Type, "source", n.Source)
return
}
slog.Info("🚚 Notification:", "Send?", "✅", "📁", filepath.Base(n.EventPath), "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "type", n.Type, "source", n.Source)
slog.Info("🚚 Notification:", "Send?", "✅", "📁", filepath.Base(n.EventPath), "📌", n.Time.Format("2006-01-02 15:04:05 MST"), "type", n.Type, "source", n.Source)
}

}
Expand All @@ -98,7 +98,7 @@ func (s *Scheduler) StopTimers() {
tmr.Stop()
}

s.timers = []*time.Timer{}
s.timers = []*time.Timer{}
}

func (s *Scheduler) initializeNotifiers() {
Expand Down

0 comments on commit 6f5e62b

Please sign in to comment.