annotate calsync/main.go @ 91:62caecb41dfd default tip

fix tag
author drewp@bigasterisk.com
date Fri, 03 Jan 2025 18:06:13 -0800
parents c2578b4a8d9d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
1 package main
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
2
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
3 /*
55
627c815f83bb wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents: 49
diff changeset
4 go build && ./gcalendarwatch
627c815f83bb wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents: 49
diff changeset
5
627c815f83bb wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents: 49
diff changeset
6
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
7 let python continue to serve these:
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
8 Route('/', getRoot),
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
9 Route('/graph/calendar/upcoming', StaticGraph(agendaGraph)),
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
10 use https://github.com/cayleygraph/quad
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
11 Route('/graph/calendar/upcoming/events', GraphEvents(agendaGraph)),
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
12 use https://github.com/tmaxmax/go-sse
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
13 Route('/graph/calendar/countdown', StaticGraph(countdownGraph)),
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
14 Route('/graph/calendar/countdown/events', GraphEvents(countdownGraph)),
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
15 Route('/graph/currentEvents', StaticGraph(currentEventsGraph)),
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
16 Route('/graph/currentEvents/events', GraphEvents(currentEventsGraph)),
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
17 */
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
18
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
19 import (
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
20 "context"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
21 "log"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
22 "net/http"
56
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 55
diff changeset
23 "time"
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
24
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
25 "bigasterisk.com/go/gcalendarwatch/gcalclient"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
26 "bigasterisk.com/go/gcalendarwatch/mongoclient"
61
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents: 60
diff changeset
27 "bigasterisk.com/go/gcalendarwatch/notificationrouter"
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
28 "github.com/gorilla/mux"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
29 )
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
30
82
0f0b15d8062c slow down startup
drewp@bigasterisk.com
parents: 80
diff changeset
31 const startupJitter = 60 * time.Second
80
e8164bd2f9a1 hoist startupJitter to main
drewp@bigasterisk.com
parents: 65
diff changeset
32
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
33 func main() {
60
3b0595c2bf03 logging and small refactors
drewp@bigasterisk.com
parents: 59
diff changeset
34 startLogging()
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
35 ctx := context.Background()
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
36
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
37 gc, err := gcalclient.New(ctx)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
38 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
39 log.Fatal(err)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
40 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
41 defer gc.Close()
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
42
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
43 mc, err := mongoclient.New(ctx)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
44 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
45 log.Fatal(err)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
46 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
47 defer mc.Close()
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
48
61
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents: 60
diff changeset
49 notifications := notificationrouter.New()
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents: 60
diff changeset
50
60
3b0595c2bf03 logging and small refactors
drewp@bigasterisk.com
parents: 59
diff changeset
51 // todo: if a cal is deleted, we don't clean up its events, even upon
3b0595c2bf03 logging and small refactors
drewp@bigasterisk.com
parents: 59
diff changeset
52 // restart.
57
24f662799710 WIP incremental sync now runs
drewp@bigasterisk.com
parents: 56
diff changeset
53 err = updateMongoCalsToMatchGoogleOnce(mc, gc)
24f662799710 WIP incremental sync now runs
drewp@bigasterisk.com
parents: 56
diff changeset
54 if err != nil {
24f662799710 WIP incremental sync now runs
drewp@bigasterisk.com
parents: 56
diff changeset
55 log.Fatal(err)
24f662799710 WIP incremental sync now runs
drewp@bigasterisk.com
parents: 56
diff changeset
56 }
56
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 55
diff changeset
57
61
8aee4f5c4bdd receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents: 60
diff changeset
58 initialSyncBack := (7 * 24) * time.Hour
86
c2578b4a8d9d calsync look 6mo ahead
drewp@bigasterisk.com
parents: 82
diff changeset
59 initialSyncAhead := (120 * 24) * time.Hour
80
e8164bd2f9a1 hoist startupJitter to main
drewp@bigasterisk.com
parents: 65
diff changeset
60 err = updateMongoEventsToMatchGoogleForever(mc, gc, initialSyncBack, initialSyncAhead, notifications, startupJitter)
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
61 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
62 log.Fatal(err)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
63 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
64
57
24f662799710 WIP incremental sync now runs
drewp@bigasterisk.com
parents: 56
diff changeset
65 r := mux.NewRouter()
24f662799710 WIP incremental sync now runs
drewp@bigasterisk.com
parents: 56
diff changeset
66 http.Handle("/", r)
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
67
59
1f8e66cb0108 deployment
drewp@bigasterisk.com
parents: 57
diff changeset
68 r.HandleFunc("/", homePage)
1f8e66cb0108 deployment
drewp@bigasterisk.com
parents: 57
diff changeset
69 r.HandleFunc("/gcalendarwatch", homePage)
1f8e66cb0108 deployment
drewp@bigasterisk.com
parents: 57
diff changeset
70 r.HandleFunc("/gcalendarwatch/notifications", notifications.NotificationHandler).Methods("POST")
65
329144c7e711 reformat
drewp@bigasterisk.com
parents: 61
diff changeset
71
329144c7e711 reformat
drewp@bigasterisk.com
parents: 61
diff changeset
72 addr := ":8080"
59
1f8e66cb0108 deployment
drewp@bigasterisk.com
parents: 57
diff changeset
73 log.Println("serving /gcalendarwatch/notifications on", addr)
1f8e66cb0108 deployment
drewp@bigasterisk.com
parents: 57
diff changeset
74 log.Fatal(http.ListenAndServe(addr, nil))
1f8e66cb0108 deployment
drewp@bigasterisk.com
parents: 57
diff changeset
75 }
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
76
60
3b0595c2bf03 logging and small refactors
drewp@bigasterisk.com
parents: 59
diff changeset
77 func startLogging() {
3b0595c2bf03 logging and small refactors
drewp@bigasterisk.com
parents: 59
diff changeset
78 log.SetFlags(log.LstdFlags | log.Lshortfile)
3b0595c2bf03 logging and small refactors
drewp@bigasterisk.com
parents: 59
diff changeset
79 log.Println(`
3b0595c2bf03 logging and small refactors
drewp@bigasterisk.com
parents: 59
diff changeset
80 ==================================================
3b0595c2bf03 logging and small refactors
drewp@bigasterisk.com
parents: 59
diff changeset
81 calsync
65
329144c7e711 reformat
drewp@bigasterisk.com
parents: 61
diff changeset
82 ----------------------------------------------`)
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
83 }
59
1f8e66cb0108 deployment
drewp@bigasterisk.com
parents: 57
diff changeset
84
1f8e66cb0108 deployment
drewp@bigasterisk.com
parents: 57
diff changeset
85 func homePage(w http.ResponseWriter, r *http.Request) {
65
329144c7e711 reformat
drewp@bigasterisk.com
parents: 61
diff changeset
86 w.Write([]byte("calsync service for calendar updates. See https://console.cloud.google.com/apis/api/calendar-json.googleapis.com/metrics?project=bigasterisk-910"))
329144c7e711 reformat
drewp@bigasterisk.com
parents: 61
diff changeset
87 }