Mercurial > code > home > repos > gcalendarwatch
annotate calsync/main.go @ 56:635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
author | drewp@bigasterisk.com |
---|---|
date | Thu, 05 Sep 2024 13:50:40 -0700 |
parents | 627c815f83bb |
children | 24f662799710 |
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" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
27 "github.com/gorilla/mux" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
28 ) |
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 func main() { |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
31 _ = http.StripPrefix |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
32 _ = mux.NewRouter |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
33 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
34 ctx := context.Background() |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
35 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
36 log.SetFlags(log.LstdFlags | log.Lshortfile) |
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 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
49 // todo: if a cal is deleted, nothing touches its db events ever again. |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
50 // err = updateMongoCalsToMatchGoogleOnce(mc, gc) |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
51 // if err != nil { |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
52 // log.Fatal(err) |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
53 // } |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
54 |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
55 err = updateMongoEventsToMatchGoogleForever(mc, gc, |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
56 time.Duration(7*24)*time.Hour, |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
57 time.Duration(14*24)*time.Hour) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
58 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
59 log.Fatal(err) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
60 } |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
61 log.Fatal("err was nil: updateMongoEventsToMatchGoogleForever shouldn't have returned") |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
62 |
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 ------------------ |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
65 connect to mongodb with these ops: |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
66 save cals list |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
67 get/set incremental token |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
68 add/edit/del events |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
69 collection.find({"startTime": {"$gte": t1, "$lt": t2}}).sort([("startTime", 1)]) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
70 collection.find({"startTime": {"$lte": now}, "endTime": {"$gte": now}})) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
71 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
72 connect to https://github.com/googleapis/google-api-go-client/tree/main/calendar/v3 and: |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
73 get all my cals |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
74 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
75 subscribe to events |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
76 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
77 get cal event changes from incremental token |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
78 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
79 get cal events in range, for initial fill? |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
80 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
81 write add/edit/del changes to mongo |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
82 */ |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
83 |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
84 // r := mux.NewRouter() |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
85 // http.Handle("/", r) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
86 |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
87 // home := func(w http.ResponseWriter, r *http.Request) { |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
88 // w.Write([]byte("calsync service for calendar updates")) |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
89 // } |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
90 // r.HandleFunc("/", home) |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
91 // r.HandleFunc("/gcalendarwatch", home) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
92 |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
93 // notificationHandler := func(w http.ResponseWriter, r *http.Request) { |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
94 // } |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
95 // r.HandleFunc("/gcalendarwatch/notification", notificationHandler).Methods("POST") |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
96 // log.Println(("serving /gcalendarwatch/notification on :8080")) |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
49
diff
changeset
|
97 // log.Fatal(http.ListenAndServe(":8080", nil)) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
98 } |