Skip to content

Commit fb57ce7

Browse files
committed
Add standalone debt cmd
1 parent c614a36 commit fb57ce7

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

cmd/debts/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Gearbox monitoring
3+
* Copyright (c) 2021. Harsh Jain
4+
*
5+
*/
6+
7+
package main
8+
9+
import (
10+
"context"
11+
"github.com/Gearbox-protocol/third-eye/config"
12+
"github.com/Gearbox-protocol/third-eye/core"
13+
"github.com/Gearbox-protocol/third-eye/debts"
14+
"github.com/Gearbox-protocol/third-eye/ethclient"
15+
"github.com/Gearbox-protocol/third-eye/log"
16+
"github.com/Gearbox-protocol/third-eye/repository"
17+
"github.com/Gearbox-protocol/third-eye/services"
18+
_ "github.com/heroku/x/hmetrics/onload"
19+
"go.uber.org/fx"
20+
"time"
21+
)
22+
23+
func StartServer(lc fx.Lifecycle, debtEng core.DebtEngineI, shutdowner fx.Shutdowner) {
24+
25+
// Starting server
26+
lc.Append(fx.Hook{
27+
// To mitigate the impact of deadlocks in application startup and
28+
// shutdown, Fx imposes a time limit on OnStart and OnStop hooks. By
29+
// default, hooks have a total of 15 seconds to complete. Timeouts are
30+
// passed via Go's usual context.Context.
31+
OnStart: func(context.Context) error {
32+
// In production, we'd want to separate the Listen and Serve phases for
33+
// better error-handling.
34+
go func() {
35+
debtEng.ProcessBackLogs()
36+
shutdowner.Shutdown()
37+
}()
38+
return nil
39+
},
40+
})
41+
}
42+
43+
func main() {
44+
app := fx.New(
45+
ethclient.Module,
46+
config.Module,
47+
repository.Module,
48+
services.Module,
49+
fx.NopLogger,
50+
debts.Module,
51+
fx.Invoke(StartServer),
52+
)
53+
startCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
54+
defer cancel()
55+
if err := app.Start(startCtx); err != nil {
56+
log.Fatal(err)
57+
}
58+
<-app.Done()
59+
}

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Gearbox monitoring
3-
* Copyright (c) 2021. Mikael Lazarev
3+
* Copyright (c) 2021. Harsh Jain
44
*
55
*/
66

core/debt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (ProfileTable) TableName() string {
6565

6666
type DebtEngineI interface {
6767
Clear()
68-
Init()
68+
ProcessBackLogs()
6969
CalculateDebtAndClear()
7070
}
7171

debts/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewDebtEngine(db *gorm.DB, client *ethclient.Client, config *config.Config,
3838
}
3939
}
4040

41-
func (eng *DebtEngine) Init() {
41+
func (eng *DebtEngine) ProcessBackLogs() {
4242
// NOTE: while syncing from scratch for some adapter disable the debt engine
4343
// as it might happen that some of the components for calculating debt are missing
4444
// check if adapters are synchronised.

engine/index.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ func NewEngine(config *config.Config,
2525
ec *ethclient.Client,
2626
debtEng core.DebtEngineI,
2727
repo core.RepositoryI) core.EngineI {
28-
return &Engine{
28+
eng := &Engine{
2929
debtEng: debtEng,
3030
config: config,
3131
repo: repo,
3232
Node: &core.Node{
3333
Client: ec,
3434
},
3535
}
36+
eng.init()
37+
return eng
3638
}
3739

3840
func (e *Engine) init() {
@@ -52,11 +54,10 @@ func (e *Engine) init() {
5254
e.currentlySyncedTill = e.repo.LoadLastAdapterSync()
5355
}
5456
// debt engine initialisation
55-
e.debtEng.Init()
57+
e.debtEng.ProcessBackLogs()
5658
}
5759

5860
func (e *Engine) SyncHandler() {
59-
e.init()
6061
latestBlockNum := e.GetLatestBlockNumber()
6162
e.syncLoop(latestBlockNum)
6263
for {

engine/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Gearbox monitoring
3-
* Copyright (c) 2021. Mikael Lazarev
3+
* Copyright (c) 2021. Harsh Jain
44
*
55
*/
66

0 commit comments

Comments
 (0)