-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
211 lines (189 loc) · 5.88 KB
/
app.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"context"
"fmt"
"github.com/avast/retry-go"
"github.com/getsentry/sentry-go"
"github.com/go-co-op/gocron/v2"
"github.com/samgozman/fin-thread/archivist"
"github.com/samgozman/fin-thread/composer"
"github.com/samgozman/fin-thread/internal/utils"
"github.com/samgozman/fin-thread/jobs"
"github.com/samgozman/fin-thread/journalist"
"github.com/samgozman/fin-thread/publisher"
"github.com/samgozman/fin-thread/scavenger"
"github.com/samgozman/fin-thread/scavenger/stocks"
"log/slog"
"time"
)
type App struct {
cnf *Config // App configuration
}
func (a *App) start() {
telegramPublisher, err := publisher.NewTelegramPublisher(
a.cnf.env.TelegramChannelID,
a.cnf.env.TelegramBotToken,
a.cnf.env.ShouldPublish,
)
if err != nil {
slog.Default().Error("[main] Error creating Telegram telegramPublisher:", err)
panic(err)
}
archivistEntity, err := archivist.NewArchivist(a.cnf.env.PostgresDSN)
if err != nil {
slog.Default().Error("[main] Error creating Archivist:", err)
panic(err)
}
composerEntity := composer.NewComposer(a.cnf.env.OpenAiToken, a.cnf.env.TogetherAIToken, a.cnf.env.GoogleGeminiToken)
marketJournalist := journalist.NewJournalist("MarketNews", a.cnf.rssProviders.marketJournalists).
FlagByKeys(a.cnf.suspiciousKeywords).
Limit(2)
broadNews := journalist.NewJournalist("BroadNews", a.cnf.rssProviders.broadJournalists).
FlagByKeys(a.cnf.suspiciousKeywords).
Limit(1)
// get all stockMap and pass as a parameter to jobs
scv := scavenger.Scavenger{}
var stockMap *stocks.StockMap
err = retry.Do(func() error {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
stockMap, err = scv.Screener.FetchFromNasdaq(ctx)
return fmt.Errorf("error fetching stockMap: %w", err)
}, retry.Attempts(2), retry.Delay(5*time.Second))
if err != nil {
slog.Default().Error("[main] Error fetching stockMap:", err)
// TODO: Find a reliable API source for this sorts of data
// try to fill the gaps with static data
stockMap = scv.Screener.FetchFromString(a.cnf.env.StockSymbols)
if stockMap == nil {
slog.Default().Error("[main] Error fetching stockMap from env")
}
}
marketJob := jobs.NewJob(composerEntity, telegramPublisher, archivistEntity, marketJournalist, stockMap).
FetchUntil(time.Now().Add(-60 * time.Second)).
OmitSuspicious().
OmitIfAllKeysEmpty().
OmitUnlistedStocks().
RemoveClones().
ComposeText().
SaveToDB()
broadJob := jobs.NewJob(composerEntity, telegramPublisher, archivistEntity, broadNews, stockMap).
FetchUntil(time.Now().Add(-4 * time.Minute)).
OmitSuspicious().
OmitEmptyMeta(jobs.MetaTickers).
OmitUnlistedStocks().
RemoveClones().
ComposeText().
SaveToDB()
// Sentry hub for fatal errors
hub := sentry.CurrentHub().Clone()
hub.ConfigureScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelFatal)
})
defer hub.Flush(2 * time.Second)
defer hub.Recover(nil)
s, err := gocron.NewScheduler()
if err != nil {
hub.AddBreadcrumb(&sentry.Breadcrumb{
Category: "scheduler",
Message: "Error creating scheduler",
Level: sentry.LevelFatal,
}, nil)
utils.CaptureSentryException("createSchedulerError", hub, err)
panic(err)
}
_, err = s.NewJob(
gocron.DurationJob(60*time.Second),
gocron.NewTask(marketJob.Run()),
gocron.WithSingletonMode(gocron.LimitModeReschedule), // for often jobs
gocron.WithName("scheduler for Market news"),
)
if err != nil {
hub.AddBreadcrumb(&sentry.Breadcrumb{
Category: "scheduler",
Message: "Error scheduling job for Market news",
Level: sentry.LevelFatal,
}, nil)
utils.CaptureSentryException("createScheduleJobError", hub, err)
panic(err)
}
_, err = s.NewJob(
gocron.DurationJob(4*time.Minute),
gocron.NewTask(broadJob.Run()),
gocron.WithName("scheduler for Broad market news"),
)
if err != nil {
hub.AddBreadcrumb(&sentry.Breadcrumb{
Category: "scheduler",
Message: "Error scheduling job for Broad news",
Level: sentry.LevelFatal,
}, nil)
utils.CaptureSentryException("createScheduleJobError", hub, err)
panic(err)
}
// Calendar job
calJob := jobs.NewCalendarJob(
scv.EconomicCalendar,
telegramPublisher,
archivistEntity,
"mql5-calendar",
)
_, err = s.NewJob(
gocron.CronJob("0 4 * * 1-5", false), // every weekday at 4:00 UTC
gocron.NewTask(calJob.RunDailyCalendarJob()),
gocron.WithName("scheduler for Calendar"),
)
if err != nil {
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Category: "scheduler",
Message: "Error scheduling job for Calendar",
Level: sentry.LevelFatal,
})
utils.CaptureSentryException("createScheduleJobError", hub, err)
panic(err)
}
_, err = s.NewJob(
gocron.DurationJob(90*time.Second),
gocron.NewTask(calJob.RunCalendarUpdatesJob()),
gocron.WithName("scheduler for Calendar updates"),
)
if err != nil {
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Category: "scheduler",
Message: "Error scheduling job for Calendar updates",
Level: sentry.LevelFatal,
})
utils.CaptureSentryException("createScheduleJobError", hub, err)
panic(err)
}
// Before market open job
bmoJob := jobs.NewSummaryJob(
composerEntity,
telegramPublisher,
archivistEntity,
)
_, err = s.NewJob(
// TODO: Use holidays calendar to avoid unnecessary runs
gocron.CronJob("0 14 * * 1-5", false), // every weekday at 14:00 UTC (market opens at 14:30 UTC)
gocron.NewTask(bmoJob.Run(time.Now().Truncate(24*time.Hour))),
gocron.WithName("scheduler for Before Market Open summary job"),
)
if err != nil {
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Category: "scheduler",
Message: "Error scheduling job for Before Market Open",
Level: sentry.LevelFatal,
})
utils.CaptureSentryException("createScheduleJobError", hub, err)
panic(err)
}
defer func(s gocron.Scheduler) {
err := s.Shutdown()
if err != nil {
panic(err)
}
}(s)
s.Start()
slog.Default().Info("Started fin-thread successfully")
select {}
}