7
7
"time"
8
8
9
9
"github.com/rs/zerolog/log"
10
+ config "github.com/thirdweb-dev/indexer/configs"
10
11
"github.com/thirdweb-dev/indexer/internal/metrics"
11
12
"github.com/thirdweb-dev/indexer/internal/rpc"
12
13
"github.com/thirdweb-dev/indexer/internal/storage"
@@ -15,26 +16,39 @@ import (
15
16
type WorkMode string
16
17
17
18
const (
18
- WORK_MODE_CHECK_INTERVAL = 10 * time . Minute
19
- WORK_MODE_BACKFILL_THRESHOLD = 500
20
- WorkModeLive WorkMode = "live"
21
- WorkModeBackfill WorkMode = "backfill"
19
+ DEFAULT_WORK_MODE_CHECK_INTERVAL = 10
20
+ DEFAULT_LIVE_MODE_THRESHOLD = 500
21
+ WorkModeLive WorkMode = "live"
22
+ WorkModeBackfill WorkMode = "backfill"
22
23
)
23
24
24
25
type WorkModeMonitor struct {
25
- rpc rpc.IRPCClient
26
- storage storage.IStorage
27
- workModeChannels map [chan WorkMode ]struct {}
28
- channelsMutex sync.RWMutex
29
- currentMode WorkMode
26
+ rpc rpc.IRPCClient
27
+ storage storage.IStorage
28
+ workModeChannels map [chan WorkMode ]struct {}
29
+ channelsMutex sync.RWMutex
30
+ currentMode WorkMode
31
+ checkInterval time.Duration
32
+ liveModeThreshold * big.Int
30
33
}
31
34
32
35
func NewWorkModeMonitor (rpc rpc.IRPCClient , storage storage.IStorage ) * WorkModeMonitor {
36
+ checkInterval := config .Cfg .WorkMode .CheckIntervalMinutes
37
+ if checkInterval < 1 {
38
+ checkInterval = DEFAULT_WORK_MODE_CHECK_INTERVAL
39
+ }
40
+ liveModeThreshold := config .Cfg .WorkMode .LiveModeThreshold
41
+ if liveModeThreshold < 1 {
42
+ liveModeThreshold = DEFAULT_LIVE_MODE_THRESHOLD
43
+ }
44
+ log .Info ().Msgf ("Work mode monitor initialized with check interval %d and live mode threshold %d" , checkInterval , liveModeThreshold )
33
45
return & WorkModeMonitor {
34
- rpc : rpc ,
35
- storage : storage ,
36
- workModeChannels : make (map [chan WorkMode ]struct {}),
37
- currentMode : "" ,
46
+ rpc : rpc ,
47
+ storage : storage ,
48
+ workModeChannels : make (map [chan WorkMode ]struct {}),
49
+ currentMode : "" ,
50
+ checkInterval : time .Duration (checkInterval ) * time .Minute ,
51
+ liveModeThreshold : big .NewInt (liveModeThreshold ),
38
52
}
39
53
}
40
54
@@ -83,7 +97,7 @@ func (m *WorkModeMonitor) Start(ctx context.Context) {
83
97
m .broadcastWorkMode (newMode )
84
98
}
85
99
86
- ticker := time .NewTicker (WORK_MODE_CHECK_INTERVAL )
100
+ ticker := time .NewTicker (m . checkInterval )
87
101
defer ticker .Stop ()
88
102
89
103
log .Info ().Msgf ("Work mode monitor started with initial mode: %s" , m .currentMode )
@@ -145,7 +159,7 @@ func (m *WorkModeMonitor) determineWorkMode(ctx context.Context) (WorkMode, erro
145
159
146
160
blockDiff := new (big.Int ).Sub (latestBlock , lastCommittedBlock )
147
161
log .Debug ().Msgf ("Committer is %d blocks behind the chain" , blockDiff .Int64 ())
148
- if blockDiff .Cmp (big . NewInt ( WORK_MODE_BACKFILL_THRESHOLD ) ) < 0 {
162
+ if blockDiff .Cmp (m . liveModeThreshold ) < 0 {
149
163
return WorkModeLive , nil
150
164
}
151
165
0 commit comments