@@ -49,7 +49,8 @@ const (
4949 templateSuffix string = ".tmpl"
5050 composeFileSuffix string = ".yml"
5151
52- nethermindAdminUrl string = "http://127.0.0.1:7434"
52+ nethermindAdminUrl string = "http://127.0.0.1:7434"
53+ pruneStarterContainerSuffix string = "_nm_prune_starter"
5354
5455 DebugColor = color .FgYellow
5556)
@@ -830,13 +831,72 @@ func (c *Client) RunPruneProvisioner(container, volume string) error {
830831
831832}
832833
833- // Executes a Go program that triggers NM pruning
834- func (c * Client ) RunNethermindPruneStarter (executionContainerName string , pruneStarterContainerName string ) error {
835- cmd := fmt .Sprintf (`docker run --rm --name %s --network container:%s rocketpool/nm-prune-starter %s` , pruneStarterContainerName , executionContainerName , nethermindAdminUrl )
834+ // Curls the Nethermind admin URL to trigger pruning
835+ func (c * Client ) RunNethermindPruneStarter (executionContainerName string ) error {
836+ retryCount := 5
837+ retryTime := 3 * time .Second
838+
839+ for i := 0 ; i < retryCount ; i ++ {
840+ command := fmt .Sprintf (`-m 30 -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"admin_prune","params":[],"id":%d}' %s` , i + 1 , nethermindAdminUrl )
841+ cmdText := fmt .Sprintf (`docker run --quiet --rm --name curl%s --network container:%s curlimages/curl -Ss %s` , pruneStarterContainerSuffix , executionContainerName , command )
842+
843+ if i != 0 {
844+ fmt .Printf ("Trying again in %v... (%d/%d)\n " , retryTime , i + 1 , retryCount )
845+ time .Sleep (retryTime )
846+ }
847+
848+ cmd , err := c .newCommand (cmdText )
849+ if err != nil {
850+ return fmt .Errorf ("error creating command for prune starter: %w" , err )
851+ }
852+
853+ stdOut , stdErr , err := cmd .OutputPipes ()
854+ if err != nil {
855+ return fmt .Errorf ("error getting output pipes for prune starter: %w" , err )
856+ }
857+
858+ err = cmd .Start ()
859+ if err != nil {
860+ return fmt .Errorf ("error running prune starter: %w" , err )
861+ }
862+ defer func () {
863+ _ = cmd .Wait ()
864+ }()
865+
866+ // Check for a curl error
867+ stdErrTextBytes , err := io .ReadAll (stdErr )
868+ if err != nil {
869+ return fmt .Errorf ("error reading error from prune starter: %w" , err )
870+ }
871+ stdErrText := string (stdErrTextBytes )
872+ if stdErrText != "" {
873+ fmt .Printf ("Error while curling the Nethermind admin URL: %s\n " , stdErrText )
874+ continue
875+ }
876+
877+ // Grab the response
878+ stdOutText , err := io .ReadAll (stdOut )
879+ if err != nil {
880+ return fmt .Errorf ("error reading response from prune starter: %w" , err )
881+ }
882+ // Parse the response as JSON
883+ var response map [string ]any
884+ err = json .Unmarshal (stdOutText , & response )
885+ if err != nil {
886+ return fmt .Errorf ("error parsing response from prune starter: %w" , err )
887+ }
888+
889+ if errObject , ok := response ["error" ].(map [string ]any ); ok {
890+ fmt .Printf ("Error starting prune: code %d, message = %s, data = %s\n " , errObject ["code" ], errObject ["message" ], errObject ["data" ])
891+ continue
892+ } else {
893+ fmt .Printf ("Success: Pruning is now \" %s\" \n " , response ["result" ])
894+ fmt .Println ("Your main execution client is now pruning. You can follow its progress with `rocketpool service logs eth1`." )
895+ fmt .Println ("NOTE: While pruning, you **cannot** interrupt the client (e.g. by restarting) or you risk corrupting the database!" )
896+ fmt .Println ("You must let it run to completion!" )
897+ break
898+ }
836899
837- err := c .printOutput (cmd )
838- if err != nil {
839- return err
840900 }
841901 return nil
842902}
0 commit comments