forked from mzrausyanfikri/autonotif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autonotif.go
89 lines (69 loc) · 1.76 KB
/
autonotif.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
package autonotif
import (
"context"
"fmt"
"log"
"strings"
"github.com/aimzeter/autonotif/config"
"github.com/aimzeter/autonotif/entity"
)
type Autonotif struct {
d *Dependencies
}
func (a *Autonotif) Terminate() error {
return nil
}
func (a *Autonotif) HealthCheck() error {
return nil
}
func (a *Autonotif) Run() error {
for chainType, chainConf := range a.d.conf.ChainList {
log.Printf("INFO | chain %s running...\n", strings.ToUpper(chainType))
err := a.notifyRecentProposal(chainType, chainConf)
if err != nil {
log.Printf("ERROR | chain %s runner.notifyRecentProposal: %s\n", strings.ToUpper(chainType), err)
continue
}
log.Printf("INFO | chain %s run successfully\n", strings.ToUpper(chainType))
}
return nil
}
func (a *Autonotif) notifyRecentProposal(chainType string, chainConf config.Chain) error {
ctx := context.Background()
lastID, err := a.d.dsStore.GetLastID(ctx, chainType)
if err != nil {
return fmt.Errorf("dsStore.GetLastID: %s", err.Error())
}
nextID := lastID + 1
p := &entity.Proposal{
ID: nextID,
ChainType: chainType,
ChainConfig: chainConf,
}
p, err = a.d.dsAPI.GetProposalDetail(ctx, p)
if err == entity.ErrProposalNotYetExistInDatasource {
return nil
}
if err != nil {
return fmt.Errorf("dsAPI.GetProposalDetail: %s", err.Error())
}
err = a.notify(ctx, p)
if err != nil {
return fmt.Errorf("notify: %s", err.Error())
}
err = a.d.dsStore.Set(ctx, p)
if err != nil {
return fmt.Errorf("dsStore.Set: %s", err.Error())
}
return nil
}
func (a *Autonotif) notify(ctx context.Context, p *entity.Proposal) error {
if !p.IsShouldNotify() {
return nil
}
err := a.d.notifier.SendMessage(ctx, p)
if err != nil {
return fmt.Errorf("notifier.SendMessage: %s", err.Error())
}
return nil
}