code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/faucet/run.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 faucet 17 18 import ( 19 "context" 20 "fmt" 21 "os" 22 "os/signal" 23 "syscall" 24 25 "code.vegaprotocol.io/vega/core/config" 26 "code.vegaprotocol.io/vega/core/faucet" 27 "code.vegaprotocol.io/vega/logging" 28 "code.vegaprotocol.io/vega/paths" 29 30 "github.com/jessevdk/go-flags" 31 ) 32 33 type faucetRun struct { 34 ctx context.Context 35 36 config.VegaHomeFlag 37 config.PassphraseFlag 38 faucet.Config 39 } 40 41 func (opts *faucetRun) Execute(_ []string) error { 42 log := logging.NewLoggerFromConfig( 43 logging.NewDefaultConfig(), 44 ) 45 defer log.AtExit() 46 47 pass, err := opts.PassphraseFile.Get("faucet wallet", false) 48 if err != nil { 49 return err 50 } 51 52 vegaPaths := paths.New(opts.VegaHome) 53 54 faucetCfgLoader, err := faucet.InitialiseConfigLoader(vegaPaths) 55 if err != nil { 56 return fmt.Errorf("couldn't initialise faucet configuration loader: %w", err) 57 } 58 59 faucetCfg, err := faucetCfgLoader.GetConfig() 60 if err != nil { 61 return fmt.Errorf("couldn't get faucet configuration: %w", err) 62 } 63 64 if _, err := flags.NewParser(faucetCfg, flags.Default|flags.IgnoreUnknown).Parse(); err != nil { 65 return err 66 } 67 68 faucetSvc, err := faucet.NewService(log, vegaPaths, *faucetCfg, pass) 69 if err != nil { 70 return fmt.Errorf("could not initialise the faucet service: %w", err) 71 } 72 73 ctx, cancel := context.WithCancel(opts.ctx) 74 go func() { 75 defer cancel() 76 if err := faucetSvc.Start(); err != nil { 77 log.Error("error starting faucet server", logging.Error(err)) 78 } 79 }() 80 81 waitSig(ctx, log) 82 83 if err := faucetSvc.Stop(); err != nil { 84 log.Error("error stopping faucet server", logging.Error(err)) 85 } else { 86 log.Info("faucet server stopped with success") 87 } 88 89 return nil 90 } 91 92 // waitSig will wait for a sigterm or sigint interrupt. 93 func waitSig(ctx context.Context, log *logging.Logger) { 94 gracefulStop := make(chan os.Signal, 1) 95 signal.Notify(gracefulStop, syscall.SIGTERM) 96 signal.Notify(gracefulStop, syscall.SIGINT) 97 98 select { 99 case sig := <-gracefulStop: 100 log.Info("Caught signal", logging.String("name", fmt.Sprintf("%+v", sig))) 101 case <-ctx.Done(): 102 // nothing to do 103 } 104 }