-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.go
53 lines (46 loc) · 1.22 KB
/
scan.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
package block_scan
import (
"context"
"github.com/evolutionlandorg/block-scan/metrics"
"time"
"github.com/evolutionlandorg/block-scan/scan"
"github.com/evolutionlandorg/block-scan/services"
"github.com/evolutionlandorg/block-scan/subscribe"
"github.com/evolutionlandorg/block-scan/util"
"github.com/evolutionlandorg/block-scan/util/log"
)
type ScanType string
var (
SUBSCRIBE ScanType = "subscribe"
POLLING ScanType = "polling"
)
func StartScanChainEvents(ctx context.Context, scanType ScanType, opt services.ScanEventsOptions) error {
var instance services.Scan
switch scanType {
case SUBSCRIBE:
instance = new(subscribe.Subscribe)
case POLLING:
instance = new(scan.Polling)
default:
log.Panic("not implement '%s' type", scanType)
}
instance.SetMetrics(metrics.NewMetrics())
if err := opt.Check(); err != nil {
return err
}
if err := instance.Init(opt); err != nil {
return err
}
if opt.RunForever {
defer func() {
if err := recover(); err != nil {
log.Error("run %s WipeBlock error: %v. restarting...", opt.Chain, err)
time.Sleep(time.Second * 1)
_ = StartScanChainEvents(ctx, scanType, opt)
}
}()
util.Panic(instance.WipeBlock(ctx))
return nil
}
return instance.WipeBlock(ctx)
}