2022-04-20 19:44:15 +00:00
|
|
|
package boot
|
|
|
|
|
|
|
|
import (
|
2022-04-26 17:57:01 +00:00
|
|
|
"context"
|
2022-05-13 12:48:31 +00:00
|
|
|
"fmt"
|
2022-04-22 16:27:54 +00:00
|
|
|
"time"
|
2022-04-20 22:12:44 +00:00
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-04-25 15:32:46 +00:00
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient"
|
2022-04-22 17:02:14 +00:00
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
|
2022-04-20 19:44:15 +00:00
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql/postgres"
|
2022-04-20 22:12:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-04-25 15:32:46 +00:00
|
|
|
maxRetry = 5 // Max times to try to connect to the DB or BC at boot.
|
|
|
|
retryInterval = 30 // The time to wait between each try.
|
|
|
|
DB sql.Database = &postgres.DB{}
|
|
|
|
BC *beaconclient.BeaconClient = &beaconclient.BeaconClient{}
|
2022-04-20 19:44:15 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 22:12:44 +00:00
|
|
|
// This function will perform some boot operations. If any steps fail, the application will fail to start.
|
|
|
|
// Keep in mind that the DB connection can be lost later in the lifecycle of the application or
|
|
|
|
// it might not be able to connect to the beacon client.
|
|
|
|
//
|
|
|
|
// 1. Make sure the Beacon client is up.
|
|
|
|
//
|
2022-04-20 19:44:15 +00:00
|
|
|
// 2. Connect to the database.
|
2022-04-20 22:12:44 +00:00
|
|
|
//
|
2022-05-13 12:48:31 +00:00
|
|
|
// 3. Make sure the node is synced, unless disregardSync is true.
|
|
|
|
func BootApplication(ctx context.Context, dbHostname string, dbPort int, dbName string, dbUsername string, dbPassword string, driverName string, bcAddress string, bcPort int, bcConnectionProtocol string, disregardSync bool) (*beaconclient.BeaconClient, sql.Database, error) {
|
2022-04-22 16:27:54 +00:00
|
|
|
log.Info("Booting the Application")
|
|
|
|
|
2022-04-25 15:32:46 +00:00
|
|
|
log.Debug("Creating the Beacon Client")
|
2022-04-27 18:01:59 +00:00
|
|
|
BC = beaconclient.CreateBeaconClient(ctx, bcConnectionProtocol, bcAddress, bcPort)
|
2022-04-25 15:32:46 +00:00
|
|
|
|
2022-04-26 17:57:01 +00:00
|
|
|
log.Debug("Checking Beacon Client")
|
2022-04-25 15:32:46 +00:00
|
|
|
err := BC.CheckBeaconClient()
|
2022-04-20 22:12:44 +00:00
|
|
|
if err != nil {
|
2022-04-26 17:57:01 +00:00
|
|
|
return nil, nil, err
|
2022-04-20 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
log.Debug("Setting up DB connection")
|
2022-05-13 12:48:31 +00:00
|
|
|
DB, err = postgres.SetupPostgresDb(dbHostname, dbPort, dbName, dbUsername, dbPassword, driverName)
|
2022-04-20 19:44:15 +00:00
|
|
|
if err != nil {
|
2022-04-26 17:57:01 +00:00
|
|
|
return nil, nil, err
|
2022-04-20 19:44:15 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
|
|
|
|
BC.Db = DB
|
2022-05-13 12:48:31 +00:00
|
|
|
|
|
|
|
var status bool
|
|
|
|
if !disregardSync {
|
|
|
|
status, err = BC.CheckHeadSync()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to get the nodes sync status")
|
|
|
|
return BC, DB, err
|
|
|
|
}
|
|
|
|
if status {
|
|
|
|
log.Error("The node is still syncing..")
|
|
|
|
err = fmt.Errorf("The node is still syncing.")
|
|
|
|
return BC, DB, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warn("We are not checking to see if the node has synced to head.")
|
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
return BC, DB, nil
|
2022-04-20 19:44:15 +00:00
|
|
|
}
|
2022-04-22 16:27:54 +00:00
|
|
|
|
|
|
|
// Add retry logic to ensure that we are give the Beacon Client and the DB time to start.
|
2022-05-13 12:48:31 +00:00
|
|
|
func BootApplicationWithRetry(ctx context.Context, dbHostname string, dbPort int, dbName string, dbUsername string, dbPassword string, driverName string, bcAddress string, bcPort int, bcConnectionProtocol string, disregardSync bool) (*beaconclient.BeaconClient, sql.Database, error) {
|
2022-04-22 16:27:54 +00:00
|
|
|
var err error
|
|
|
|
for i := 0; i < maxRetry; i++ {
|
2022-05-13 12:48:31 +00:00
|
|
|
BC, DB, err = BootApplication(ctx, dbHostname, dbPort, dbName, dbUsername, dbPassword, driverName, bcAddress, bcPort, bcConnectionProtocol, disregardSync)
|
2022-04-22 16:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"retryNumber": i,
|
2022-05-13 12:48:31 +00:00
|
|
|
"err": err,
|
2022-04-22 16:27:54 +00:00
|
|
|
}).Warn("Unable to boot application. Going to try again")
|
|
|
|
time.Sleep(time.Duration(retryInterval) * time.Second)
|
2022-05-06 15:03:15 +00:00
|
|
|
continue
|
2022-04-22 16:27:54 +00:00
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
break
|
2022-04-22 16:27:54 +00:00
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
return BC, DB, err
|
2022-04-22 16:27:54 +00:00
|
|
|
}
|