comparison calsync/sync_event.go @ 77:7041fae9549f

rework some err-vs-fatal cases
author drewp@bigasterisk.com
date Fri, 06 Sep 2024 17:54:35 -0700
parents 470eacf452cb
children 1fca0cea3bfe
comparison
equal deleted inserted replaced
76:470eacf452cb 77:7041fae9549f
1 package main 1 package main
2 2
3 import ( 3 import (
4 "fmt"
4 "log" 5 "log"
5 "math/rand" 6 "math/rand"
6 "time" 7 "time"
7 8
8 "bigasterisk.com/go/gcalendarwatch/convert" 9 "bigasterisk.com/go/gcalendarwatch/convert"
39 } 40 }
40 log.Println("syncing events from", len(cals), "calendars") 41 log.Println("syncing events from", len(cals), "calendars")
41 42
42 for _, cal := range cals { 43 for _, cal := range cals {
43 rd := newCalEventsReader(mc, gc, router, cal, initialSyncT1, initialSyncT2) 44 rd := newCalEventsReader(mc, gc, router, cal, initialSyncT1, initialSyncT2)
44 go rd.watchForUpdates() 45 go func() {
46 err := rd.watchForUpdates()
47 if err != nil {
48 log.Println("ERROR", M.Prefix(rd.cal), "watchForUpdates: ", err, "(aborting this calendar)")
49 }
50 }()
45 } 51 }
46 return nil 52 return nil
47 } 53 }
48 54
49 type calEventsReader struct { 55 type calEventsReader struct {
68 func (r *calEventsReader) watchForUpdates() error { 74 func (r *calEventsReader) watchForUpdates() error {
69 r.watchForUpdatesJitteredStartup() 75 r.watchForUpdatesJitteredStartup()
70 76
71 err := r.updateInitialRange() 77 err := r.updateInitialRange()
72 if err != nil { 78 if err != nil {
73 log.Fatal(err) 79 return err
74 } 80 }
75 81
76 watchLifetime := time.Duration(3) * time.Minute 82 watchLifetime := time.Duration(3) * time.Minute
77 83
78 syncFunc := notificationrouter.SyncFunc(func() { 84 syncFunc := notificationrouter.SyncFunc(func() {
79 err := r.sync() 85 err := r.sync()
80 if err != nil { 86 if err != nil {
81 log.Fatal(err) 87 log.Fatalln("ERROR", M.Prefix(r.cal), "sync: ", err)
82 } 88 }
83 }) 89 })
84 go r.renewWatchEventsForever(watchLifetime, syncFunc) 90 go r.renewWatchEventsForever(watchLifetime, syncFunc)
91 return nil
85 } 92 }
86 93
87 func (r *calEventsReader) watchForUpdatesJitteredStartup() { 94 func (r *calEventsReader) watchForUpdatesJitteredStartup() {
88 jitteredStartup := time.Duration(rand.Int()%30) * time.Second 95 jitteredStartup := time.Duration(rand.Int()%30) * time.Second
89 log.Println(M.Prefix(r.cal), "watchForEvents starting in", jitteredStartup) 96 log.Println(M.Prefix(r.cal), "watchForEvents starting in", jitteredStartup)
124 } 131 }
125 132
126 func (r *calEventsReader) sync() error { 133 func (r *calEventsReader) sync() error {
127 events, nextSyncToken, err := r.gc.ListEventUpdates(r.cal, r.syncToken) 134 events, nextSyncToken, err := r.gc.ListEventUpdates(r.cal, r.syncToken)
128 if err != nil { 135 if err != nil {
129 return err 136 return fmt.Errorf("ListEventUpdates: %v", err)
130 } 137 }
131 r.syncToken = nextSyncToken 138 r.syncToken = nextSyncToken
132 for _, ev := range events { 139 for _, ev := range events {
133 r.mc.UpsertOneEvent(convert.MongoEventFromGoogleEvent(&ev, time.Now() /*todo*/)) 140 r.mc.UpsertOneEvent(convert.MongoEventFromGoogleEvent(&ev, time.Now() /*todo*/))
134 } 141 }