-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (132 loc) · 3.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime/pprof"
"time"
_ "github.com/joho/godotenv/autoload"
_ "github.com/mattn/go-sqlite3"
"github.com/bluesky-social/indigo/xrpc"
"github.com/orthanc/feedgenerator/database"
"github.com/orthanc/feedgenerator/following"
processor "github.com/orthanc/feedgenerator/processors"
"github.com/orthanc/feedgenerator/ratios"
"github.com/orthanc/feedgenerator/subscription"
"github.com/orthanc/feedgenerator/web"
)
func startProfile() (func(), error) {
cpuProfName := filepath.Join(os.Getenv("PROFILES_DIR"), fmt.Sprintf("feedgen-cpu-%s.prof", time.Now().UTC().Format(time.RFC3339)));
cpuF, err := os.Create(cpuProfName);
if err != nil {
return func () {}, err;
}
err = pprof.StartCPUProfile(cpuF);
if err != nil {
return func () {
cpuF.Close();
}, err
}
log.Printf("Started CPU profile %s\n", cpuProfName);
stopCpuProf := func () {
log.Printf("Finishing CPU profile %s\n", cpuProfName);
pprof.StopCPUProfile();
cpuF.Close()
}
return stopCpuProf, nil
}
func main() {
go func() {
stopProfile, err := startProfile();
defer func () {stopProfile();}()
if err != nil {
log.Printf("Unable to start profiler %e\n", err);
}
ticker := time.NewTicker(1 * time.Hour);
for range ticker.C {
stopProfile();
stopProfile, err = startProfile();
if err != nil {
log.Printf("Unable to start profiler %e\n", err);
}
}
}()
ctx := context.Background()
dbDown := flag.Bool("db-down", false, "migrate the database down one revision then exit")
dbUp := flag.Bool("db-up", false, "migrate the database up to the latest revision then exit")
flag.Parse()
database, err := database.NewDatabase(ctx)
if err != nil {
panic(err)
}
if *dbDown {
err := database.MigrateDown(ctx)
if err != nil {
panic(err)
}
return
}
err = database.MigrateUp(ctx)
if err != nil {
panic(err)
}
if *dbUp {
return
}
client := xrpc.Client{
Host: "https://bsky.social",
}
publicClient := xrpc.Client{
Host: "https://public.api.bsky.app",
}
followFarmersList := os.Getenv("FOLLOW_FARMERS_LIST")
allFollowing := following.NewAllFollowing(
database,
&client,
&publicClient,
followFarmersList,
)
ratioCalc := ratios.NewRatios(database)
go func() {
err := allFollowing.SyncList(ctx, followFarmersList)
if err != nil {
fmt.Println(err)
}
ticker := time.NewTicker(6 * time.Hour)
for range ticker.C {
err := allFollowing.SyncList(ctx, followFarmersList)
if err != nil {
fmt.Println(err)
}
}
}()
postersMadness := processor.NewPostersMadness(database);
processingStats := subscription.NewProcessingStats()
go web.StartServer(database, allFollowing, processingStats)
firehoseListeners := make(map[string]subscription.JetstreamEventListener)
firehoseListeners["app.bsky.graph.follow"] = (&processor.FollowProcessor{
Database: database,
AllFollowing: allFollowing,
}).Process
firehoseListeners["app.bsky.feed.post"] = (&processor.PostProcessor{
Database: database,
PostersMadness: postersMadness,
PublicClient: &publicClient,
}).Process
firehoseListeners["app.bsky.feed.like"] = (&processor.LikeProcessor{
Database: database,
PostersMadness: postersMadness,
}).Process
firehoseListeners["app.bsky.feed.repost"] = (&processor.RepostProcessor{
Database: database,
}).Process
firehoseListeners["app.bsky.graph.listitem"] = processor.NewListItemProcessor(database, followFarmersList ).Process
fmt.Println("Starting")
err = subscription.SubscribeJetstream(ctx, os.Getenv("JETSTREAM_SUBSCRIPTION_ENDPOINT"), database, firehoseListeners, ratioCalc.Pauser, processingStats)
if err != nil {
panic(fmt.Sprintf("subscribing to firehose failed (dialing): %s", err))
}
}