2022-05-18 16:12:54 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-04-19 21:09:59 +00:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-04-26 17:57:01 +00:00
|
|
|
"context"
|
2022-05-24 20:18:55 +00:00
|
|
|
"fmt"
|
2022-05-25 14:19:29 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2022-04-26 17:57:01 +00:00
|
|
|
|
2022-05-25 14:19:29 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2022-04-26 17:57:01 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-04-19 21:09:59 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-05-12 19:44:05 +00:00
|
|
|
"github.com/spf13/viper"
|
2022-06-09 21:32:46 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/internal/boot"
|
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/internal/shutdown"
|
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/loghelper"
|
2022-05-24 20:18:55 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-05-12 19:44:05 +00:00
|
|
|
)
|
|
|
|
|
2022-04-19 21:09:59 +00:00
|
|
|
// headCmd represents the head command
|
|
|
|
var headCmd = &cobra.Command{
|
|
|
|
Use: "head",
|
2022-04-20 13:25:47 +00:00
|
|
|
Short: "Capture only the blocks and state at head.",
|
|
|
|
Long: `Capture only the blocks and state at head.`,
|
2022-04-19 21:09:59 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-04-20 19:44:15 +00:00
|
|
|
startHeadTracking()
|
2022-04-19 21:09:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:27:54 +00:00
|
|
|
// Start the application to track at head.
|
2022-04-20 19:44:15 +00:00
|
|
|
func startHeadTracking() {
|
2022-04-26 17:57:01 +00:00
|
|
|
// Boot the application
|
|
|
|
log.Info("Starting the application in head tracking mode.")
|
2022-06-22 16:06:52 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2022-04-26 17:57:01 +00:00
|
|
|
|
2022-05-24 20:18:55 +00:00
|
|
|
Bc, Db, err := boot.BootApplicationWithRetry(ctx, viper.GetString("db.address"), viper.GetInt("db.port"), viper.GetString("db.name"), viper.GetString("db.username"), viper.GetString("db.password"), viper.GetString("db.driver"),
|
|
|
|
viper.GetString("bc.address"), viper.GetInt("bc.port"), viper.GetString("bc.connectionProtocol"), viper.GetString("bc.type"), viper.GetInt("bc.bootRetryInterval"), viper.GetInt("bc.bootMaxRetry"),
|
2022-06-06 13:02:43 +00:00
|
|
|
viper.GetInt("kg.increment"), "head", viper.GetBool("t.skipSync"), viper.GetInt("bc.uniqueNodeIdentifier"), viper.GetBool("bc.checkDb"))
|
2022-04-20 19:44:15 +00:00
|
|
|
if err != nil {
|
2022-05-24 20:18:55 +00:00
|
|
|
StopApplicationPreBoot(err, Db)
|
2022-04-20 19:44:15 +00:00
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
|
2022-05-25 14:19:29 +00:00
|
|
|
if viper.GetBool("pm.metrics") {
|
|
|
|
addr := viper.GetString("pm.address") + ":" + strconv.Itoa(viper.GetInt("pm.port"))
|
|
|
|
serveProm(addr)
|
|
|
|
}
|
|
|
|
|
2022-05-13 12:48:31 +00:00
|
|
|
log.Info("The Beacon Client has booted successfully!")
|
2022-04-26 17:57:01 +00:00
|
|
|
// Capture head blocks
|
2022-06-22 16:06:52 +00:00
|
|
|
go Bc.CaptureHead(ctx, viper.GetInt("bc.maxHeadProcessWorker"), false)
|
2022-06-21 18:34:10 +00:00
|
|
|
|
2022-05-24 20:18:55 +00:00
|
|
|
if viper.GetBool("kg.processKnownGaps") {
|
|
|
|
go func() {
|
|
|
|
errG := new(errgroup.Group)
|
|
|
|
errG.Go(func() error {
|
2022-06-22 16:06:52 +00:00
|
|
|
errs := Bc.ProcessKnownGaps(ctx, viper.GetInt("kg.maxKnownGapsWorker"))
|
2022-05-24 20:18:55 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
log.WithFields(log.Fields{"errs": errs}).Error("All errors when processing knownGaps")
|
|
|
|
return fmt.Errorf("Application ended because there were too many error when attempting to process knownGaps")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err := errG.Wait(); err != nil {
|
|
|
|
loghelper.LogError(err).Error("Error with knownGaps processing")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
|
2022-06-20 17:56:55 +00:00
|
|
|
if viper.GetBool("t.pprof") {
|
|
|
|
go func() {
|
|
|
|
log.Println(http.ListenAndServe(fmt.Sprint("localhost:"+strconv.Itoa(viper.GetInt("t.pprofPort"))), nil))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:57:01 +00:00
|
|
|
// Shutdown when the time is right.
|
2022-06-22 16:06:52 +00:00
|
|
|
err = shutdown.ShutdownHeadTracking(ctx, cancel, notifierCh, maxWaitSecondsShutdown, Db, Bc)
|
2022-04-28 18:50:29 +00:00
|
|
|
if err != nil {
|
2022-06-09 21:32:46 +00:00
|
|
|
loghelper.LogError(err).Error("Ungracefully Shutdown ipld-eth-beacon-indexer!")
|
2022-04-28 18:50:29 +00:00
|
|
|
} else {
|
2022-06-09 21:32:46 +00:00
|
|
|
log.Info("Gracefully shutdown ipld-eth-beacon-indexer")
|
2022-04-28 18:50:29 +00:00
|
|
|
}
|
2022-04-20 19:44:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 21:09:59 +00:00
|
|
|
func init() {
|
|
|
|
captureCmd.AddCommand(headCmd)
|
|
|
|
}
|
2022-05-25 14:19:29 +00:00
|
|
|
|
2022-06-03 16:47:13 +00:00
|
|
|
// Start prometheus server
|
2022-05-25 14:19:29 +00:00
|
|
|
func serveProm(addr string) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/metrics", promhttp.Handler())
|
|
|
|
|
|
|
|
srv := http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
|
|
loghelper.LogError(err).WithField("endpoint", addr).Error("Error with prometheus")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|