-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
93 lines (81 loc) · 2.53 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/getAlby/lndhub.go/db"
"github.com/getAlby/lndhub.go/lib"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/getAlby/lndhub.go/lnd"
"github.com/getsentry/sentry-go"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/labstack/echo/v4"
)
// script to reconcile pending payments between the backup node and the database
// normally, this reconciliation should happen through rabbitmq but there are
// cases where it doesn't happen and in that case this script can be run as a a
// cron job as a redundant reconcilation mechanism.
func main() {
c := &service.Config{}
// Load configruation from environment variables
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Failed to load .env file")
}
err = envconfig.Process("", c)
if err != nil {
log.Fatalf("Error loading environment variables: %v", err)
}
// Setup logging to STDOUT or a configrued log file
logger := lib.Logger(c.LogFilePath)
// Open a DB connection based on the configured DATABASE_URI
dbConn, err := db.Open(c)
if err != nil {
logger.Fatalf("Error initializing db connection: %v", err)
}
// Migrate the DB
//Todo: use timeout for startupcontext
startupCtx := context.Background()
// New Echo app
e := echo.New()
// Init new LND client
lnCfg, err := lnd.LoadConfig()
if err != nil {
logger.Fatalf("Failed to load lnd config %v", err)
}
lndClient, err := lnd.NewLNDclient(lnd.LNDoptions{
Address: lnCfg.LNDAddress,
MacaroonFile: lnCfg.LNDMacaroonFile,
MacaroonHex: lnCfg.LNDMacaroonHex,
CertFile: lnCfg.LNDCertFile,
CertHex: lnCfg.LNDCertHex,
}, startupCtx)
if err != nil {
e.Logger.Fatalf("Error initializing the LND connection: %v", err)
}
logger.Infof("Connected to LND: %s ", lndClient.GetMainPubkey())
svc := &service.LndhubService{
Config: c,
DB: dbConn,
LndClient: lndClient,
Logger: logger,
InvoicePubSub: service.NewPubsub(),
}
//for this job, we only search for payments older than a day to avoid current in-flight payments
ts := time.Now().Add(-1 * 24 * time.Hour)
pending, err := svc.GetPendingPaymentsUntil(startupCtx, ts)
if err != nil {
sentry.CaptureException(err)
svc.Logger.Fatal(err)
}
svc.Logger.Infof("Found %d pending payments", len(pending))
startupCtx, cancel := context.WithTimeout(startupCtx, 2*time.Minute)
defer cancel()
err = svc.CheckPendingOutgoingPayments(startupCtx, pending)
if err != nil {
sentry.CaptureException(err)
svc.Logger.Fatal(err)
}
}