code.vegaprotocol.io/vega@v0.79.0/cmd/data-node/commands/postgres.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"os"
    22  	"os/signal"
    23  	"path/filepath"
    24  	"syscall"
    25  
    26  	"code.vegaprotocol.io/vega/datanode/config"
    27  	"code.vegaprotocol.io/vega/logging"
    28  	"code.vegaprotocol.io/vega/paths"
    29  
    30  	embeddedpostgres "github.com/fergusstrange/embedded-postgres"
    31  	"github.com/jessevdk/go-flags"
    32  	"gopkg.in/natefinch/lumberjack.v2"
    33  )
    34  
    35  type PostgresCmd struct {
    36  	Run PostgresRunCmd `command:"run"`
    37  }
    38  
    39  var postgresCmd PostgresCmd
    40  
    41  func Postgres(ctx context.Context, parser *flags.Parser) error {
    42  	postgresCmd = PostgresCmd{
    43  		Run: PostgresRunCmd{},
    44  	}
    45  
    46  	_, err := parser.AddCommand("postgres", "Embedded Postgres", "Embedded Postgres", &postgresCmd)
    47  	return err
    48  }
    49  
    50  type PostgresRunCmd struct {
    51  	config.VegaHomeFlag
    52  	config.Config
    53  }
    54  
    55  func (cmd *PostgresRunCmd) Execute(_ []string) error {
    56  	log := logging.NewLoggerFromConfig(
    57  		logging.NewDefaultConfig(),
    58  	)
    59  	ctx, cfunc := context.WithCancel(context.Background())
    60  	defer cfunc()
    61  	defer log.AtExit()
    62  
    63  	log.Info("Launching Postgres")
    64  
    65  	// we define this option to parse the cli args each time the config is
    66  	// loaded. So that we can respect the cli flag precedence.
    67  	parseFlagOpt := func(cfg *config.Config) error {
    68  		_, err := flags.NewParser(cfg, flags.Default|flags.IgnoreUnknown).Parse()
    69  		return err
    70  	}
    71  
    72  	vegaPaths := paths.New(cmd.VegaHome)
    73  
    74  	configWatcher, err := config.NewWatcher(ctx, log, vegaPaths, config.Use(parseFlagOpt))
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	cmd.Config = configWatcher.Get()
    80  
    81  	lumberjackLog := &lumberjack.Logger{
    82  		Filename: paths.StatePath(filepath.Join(paths.DataNodeLogsHome.String(), "embedded-postgres.log")).String(),
    83  		MaxSize:  cmd.Config.SQLStore.LogRotationConfig.MaxSize,
    84  		MaxAge:   cmd.Config.SQLStore.LogRotationConfig.MaxAge,
    85  		Compress: true,
    86  	}
    87  
    88  	dbConfig := embeddedpostgres.DefaultConfig().
    89  		Username(cmd.Config.SQLStore.ConnectionConfig.Username).
    90  		Password(cmd.Config.SQLStore.ConnectionConfig.Password).
    91  		Database(cmd.Config.SQLStore.ConnectionConfig.Database).
    92  		Port(uint32(cmd.Config.SQLStore.ConnectionConfig.Port)).
    93  		Logger(lumberjackLog).
    94  		RuntimePath(vegaPaths.StatePathFor(paths.DataNodeStorageSQLStoreHome)).
    95  		DataPath(vegaPaths.StatePathFor(paths.DataNodeStorageSQLStoreNodeDataHome))
    96  
    97  	db := embeddedpostgres.NewDatabase(dbConfig)
    98  	err = db.Start()
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	cmd.wait(ctx, log, cfunc)
   104  	return db.Stop()
   105  }
   106  
   107  func (cmd *PostgresRunCmd) wait(ctx context.Context, log *logging.Logger, cfunc func()) {
   108  	ch := make(chan os.Signal, 1)
   109  	signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
   110  	for {
   111  		select {
   112  		case sig := <-ch:
   113  			cfunc()
   114  			log.Info("Caught signal", logging.String("name", fmt.Sprintf("%+v", sig)))
   115  			return
   116  		case <-ctx.Done():
   117  			return
   118  		}
   119  	}
   120  }