forked from hswaw/hscloud
45 lines
737 B
Go
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
|
|
}
|