hscloud/hswaw/site/events.go
Serge Bazanski 717aad4ac6 hswaw/site: wip new layout
Change-Id: I4da3a668429dee42c7292accb9e24b93703f1538
2021-08-30 21:00:59 +02:00

45 lines
737 B
Go

package main
import (
"context"
"time"
"code.hackerspace.pl/hscloud/hswaw/site/calendar"
"github.com/golang/glog"
)
func (s *service) eventsWorker(ctx context.Context) {
get := func() {
events, err := calendar.GetUpcomingEvents(ctx, time.Now())
if err != nil {
glog.Errorf("Geting events failed: %v", err)
return
}
s.eventsMu.Lock()
s.events = events
s.eventsMu.Unlock()
}
// Perform initial fetch.
get()
// .. and update very minute.
t := time.NewTicker(time.Minute)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
get()
}
}
}
func (s *service) getEvents() []*calendar.UpcomingEvent {
s.eventsMu.RLock()
events := s.events
s.eventsMu.RUnlock()
return events
}