Skip to content

Commit 9f660d9

Browse files
committed
Fix Pushpin Process Context and make log output less noisy
1 parent d39b380 commit 9f660d9

1 file changed

Lines changed: 76 additions & 91 deletions

File tree

pkg/commands/compute/serve.go

Lines changed: 76 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -600,22 +600,22 @@ func (c *ServeCommand) GetPushpinProxyPort(out io.Writer) (uint16, error) {
600600
}
601601
pushpinProxyPort = uint16(pushpinProxyPortInt)
602602
if c.Globals.Verbose() {
603-
text.Info(out, "Using Pushpin proxy port from --pushpin-proxy-port flag: %d\n\n", pushpinProxyPort)
603+
text.Output(out, "DEBUG: Using Pushpin proxy port from --pushpin-proxy-port flag: %d", pushpinProxyPort)
604604
}
605605
return pushpinProxyPort, nil
606606
}
607607

608608
pushpinProxyPort = c.Globals.Manifest.File.LocalServer.Pushpin.PushpinProxyPort
609609
if pushpinProxyPort != 0 {
610610
if c.Globals.Verbose() {
611-
text.Info(out, "Using Pushpin proxy port via `local_server.pushpin.proxy_port` setting: %d\n\n", pushpinProxyPort)
611+
text.Output(out, "DEBUG: Using Pushpin proxy port via `local_server.pushpin.proxy_port` setting: %d", pushpinProxyPort)
612612
}
613613
return pushpinProxyPort, nil
614614
}
615615

616616
pushpinProxyPort = 7677
617617
if c.Globals.Verbose() {
618-
text.Info(out, "Using default Pushpin proxy port %d\n\n", pushpinProxyPort)
618+
text.Output(out, "DEBUG: Using default Pushpin proxy port %d", pushpinProxyPort)
619619
}
620620
return pushpinProxyPort, nil
621621
}
@@ -640,22 +640,22 @@ func (c *ServeCommand) GetPushpinPublishPort(out io.Writer) (uint16, error) {
640640
}
641641
pushpinPublishPort = uint16(pushpinPublishPortInt)
642642
if c.Globals.Verbose() {
643-
text.Info(out, "Using Pushpin publish handler port from --pushpin-publish-port flag: %d\n\n", pushpinPublishPort)
643+
text.Output(out, "DEBUG: Using Pushpin publish handler port from --pushpin-publish-port flag: %d", pushpinPublishPort)
644644
}
645645
return pushpinPublishPort, nil
646646
}
647647

648648
pushpinPublishPort = c.Globals.Manifest.File.LocalServer.Pushpin.PushpinPublishPort
649649
if pushpinPublishPort != 0 {
650650
if c.Globals.Verbose() {
651-
text.Info(out, "Using Pushpin publish handler port via `local_server.pushpin.publish_port` setting: %d\n\n", pushpinPublishPort)
651+
text.Output(out, "DEBUG: Using Pushpin publish handler port via `local_server.pushpin.publish_port` setting: %d", pushpinPublishPort)
652652
}
653653
return pushpinPublishPort, nil
654654
}
655655

656656
pushpinPublishPort = 5561
657657
if c.Globals.Verbose() {
658-
text.Info(out, "Using default Pushpin publish handler port %d\n\n", pushpinPublishPort)
658+
text.Output(out, "DEBUG: Using default Pushpin publish handler port %d", pushpinPublishPort)
659659
}
660660
return pushpinPublishPort, nil
661661
}
@@ -669,21 +669,21 @@ func (c *ServeCommand) GetPushpinRunner(out io.Writer) (bin string, err error) {
669669
pushpinRunnerBinPath := c.pushpinRunnerBinPath
670670
if pushpinRunnerBinPath != "" {
671671
if c.Globals.Verbose() {
672-
text.Info(out, "Using user provided install of Pushpin runner via --pushpin-path flag: %s\n\n", pushpinRunnerBinPath)
672+
text.Output(out, "DEBUG: Using user provided install of Pushpin runner via --pushpin-path flag: %s", pushpinRunnerBinPath)
673673
}
674674
return filepath.Abs(pushpinRunnerBinPath)
675675
}
676676

677677
pushpinRunnerBinPath = c.Globals.Manifest.File.LocalServer.Pushpin.PushpinPath
678678
if pushpinRunnerBinPath != "" {
679679
if c.Globals.Verbose() {
680-
text.Info(out, "Using user provided install of Pushpin runner via `local_server.pushpin.pushpin_path` setting: %s\n\n", pushpinRunnerBinPath)
680+
text.Output(out, "DEBUG: Using user provided install of Pushpin runner via `local_server.pushpin.pushpin_path` setting: %s", pushpinRunnerBinPath)
681681
}
682682
return filepath.Abs(pushpinRunnerBinPath)
683683
}
684684

685685
if c.Globals.Verbose() {
686-
text.Info(out, "No --pushpin-path provided, attempting to find 'pushpin' in your PATH...")
686+
text.Output(out, "DEBUG: No --pushpin-path provided, attempting to find 'pushpin' in your PATH...")
687687
}
688688
pushpinRunnerBinPath, err = exec.LookPath("pushpin")
689689
if err != nil {
@@ -694,7 +694,7 @@ func (c *ServeCommand) GetPushpinRunner(out io.Writer) (bin string, err error) {
694694
}
695695

696696
if c.Globals.Verbose() {
697-
text.Info(out, "Found Pushpin runner via $PATH lookup: %s\n\n", pushpinRunnerBinPath)
697+
text.Output(out, "DEBUG: Found Pushpin runner via $PATH lookup: %s", pushpinRunnerBinPath)
698698
}
699699
return filepath.Abs(pushpinRunnerBinPath)
700700
}
@@ -767,6 +767,8 @@ func formatPushpinLog(line string) (string, string) {
767767
// pushpinContext contains information about the instance of Pushpin that is
768768
// executed when enabled.
769769
type pushpinContext struct {
770+
instanceID uint32
771+
confFilePath string
770772
pushpinRunnerBin string
771773
pushpinRunDir string
772774
pushpinLogDir string
@@ -811,45 +813,77 @@ func (c *pushpinContext) buildPushpinConf() string {
811813
// needs to eventually be called by the caller to shut down Pushpin.
812814
func (c *ServeCommand) startPushpin(spinner text.Spinner, out io.Writer) (pushpinContext, error) {
813815
text.Output(out, "%s: %s", text.BoldYellow("EXPERIMENTAL"), "Enabling Pushpin support for local testing of Fanout.")
814-
text.Break(out)
815816

816817
pushpinCtx := pushpinContext{}
817818

818819
var cleanup func()
819820

820-
err := spinner.Start()
821+
// Generate a non-zero instance ID to represent this Pushpin instance and build temporary
822+
// files
823+
for {
824+
p := make([]byte, 4)
825+
_, _ = rand.Read(p)
826+
pushpinCtx.instanceID = binary.BigEndian.Uint32(p)
827+
if pushpinCtx.instanceID != 0 {
828+
break
829+
}
830+
}
831+
832+
var err error
833+
pushpinCtx.proxyPort, err = c.GetPushpinProxyPort(out)
821834
if err != nil {
822835
return pushpinCtx, err
823836
}
824-
msg := "Running local Pushpin"
825-
spinner.Message(msg + "...")
826-
827-
spinner.StopMessage(msg)
828-
err = spinner.Stop()
837+
pushpinCtx.publishPort, err = c.GetPushpinPublishPort(out)
838+
if err != nil {
839+
return pushpinCtx, err
840+
}
841+
pushpinCtx.pushpinRunnerBin, err = c.GetPushpinRunner(out)
829842
if err != nil {
830843
return pushpinCtx, err
831844
}
832845

833846
pwd, _ := os.Getwd()
834847
pushpinCtx.pushpinLogDir = filepath.Join(pwd, "pushpin-logs")
835848

836-
pushpinCtx.proxyPort, err = c.GetPushpinProxyPort(out)
849+
pushpinCtx.pushpinRunDir = filepath.Join(
850+
os.TempDir(),
851+
fmt.Sprintf("pushpin-%08x", pushpinCtx.instanceID),
852+
)
853+
pushpinCtx.confFilePath = filepath.Join(
854+
os.TempDir(),
855+
fmt.Sprintf("pushpin-%08x.conf", pushpinCtx.instanceID),
856+
)
857+
pushpinCtx.routesFilePath = filepath.Join(
858+
os.TempDir(),
859+
fmt.Sprintf("pushpin-routes-%08x", pushpinCtx.instanceID),
860+
)
861+
862+
text.Break(out)
863+
864+
err = spinner.Start()
837865
if err != nil {
838866
return pushpinCtx, err
839867
}
840-
pushpinCtx.publishPort, err = c.GetPushpinPublishPort(out)
868+
msg := "Running local Pushpin"
869+
spinner.Message(msg + "...")
870+
871+
spinner.StopMessage(msg)
872+
err = spinner.Stop()
841873
if err != nil {
842874
return pushpinCtx, err
843875
}
844-
pushpinCtx.pushpinRunnerBin, err = c.GetPushpinRunner(out)
876+
877+
pushpinConfContents := pushpinCtx.buildPushpinConf()
878+
err = os.WriteFile(pushpinCtx.confFilePath, []byte(pushpinConfContents), 0o600)
845879
if err != nil {
846-
return pushpinCtx, err
880+
return pushpinCtx, fmt.Errorf("error writing config file %s: %w", pushpinCtx.confFilePath, err)
847881
}
848-
if c.Globals.Verbose() {
849-
text.Info(out, "Local pushpin proxy port: %d", pushpinCtx.proxyPort)
850-
text.Info(out, "Local pushpin publisher port: %d", pushpinCtx.publishPort)
851-
text.Info(out, "Local pushpin other reserved ports: %d - %d", pushpinCtx.publishPort+1, pushpinCtx.publishPort+3)
852-
text.Info(out, "Local pushpin runner: %s", pushpinCtx.pushpinRunnerBin)
882+
883+
pushpinRoutesContents := strings.Join(c.BuildPushpinRoutes(), "\n") + "\n"
884+
err = os.WriteFile(pushpinCtx.routesFilePath, []byte(pushpinRoutesContents), 0o600)
885+
if err != nil {
886+
return pushpinCtx, fmt.Errorf("error writing routes file %s: %w", pushpinCtx.routesFilePath, err)
853887
}
854888

855889
// Pushpin is configured with the following.
@@ -866,63 +900,11 @@ func (c *ServeCommand) startPushpin(spinner text.Spinner, out io.Writer) (pushpi
866900
// - if the backend enables HTTPS, then we enable that
867901
// - if the backend has a path prefix, then we set that up
868902
// - enables WebSocket-over-HTTP
869-
870903
// The runtime temporary directory, as well as the conf file and routes file
871904
// are set up and torn down along with fastly compute serve.
872-
var pushpinInstanceID uint32
873-
for {
874-
p := make([]byte, 4)
875-
_, _ = rand.Read(p)
876-
pushpinInstanceID = binary.BigEndian.Uint32(p)
877-
if pushpinInstanceID != 0 {
878-
break
879-
}
880-
}
881-
882-
pushpinRunDir := filepath.Join(
883-
os.TempDir(),
884-
fmt.Sprintf("pushpin-%08x", pushpinInstanceID),
885-
)
886-
if c.Globals.Verbose() {
887-
text.Break(out)
888-
text.Info(out, "Pushpin temporary runtime directory is %s", pushpinRunDir)
889-
}
890-
891-
pushpinConfFilePath := filepath.Join(
892-
os.TempDir(),
893-
fmt.Sprintf("pushpin-%08x.conf", pushpinInstanceID),
894-
)
895-
pushpinRoutesFilePath := filepath.Join(
896-
os.TempDir(),
897-
fmt.Sprintf("pushpin-routes-%08x", pushpinInstanceID),
898-
)
899-
900-
if c.Globals.Verbose() {
901-
text.Info(out, "Writing config file to %s...", pushpinConfFilePath)
902-
}
903-
904-
pushpinConfContents := pushpinCtx.buildPushpinConf()
905-
err = os.WriteFile(pushpinConfFilePath, []byte(pushpinConfContents), 0o600)
906-
if err != nil {
907-
return pushpinCtx, fmt.Errorf("error writing config file %s: %w", pushpinConfFilePath, err)
908-
}
909-
910-
if c.Globals.Verbose() {
911-
text.Info(out, "Writing routes file to %s...", pushpinRoutesFilePath)
912-
}
913-
pushpinRoutesContents := strings.Join(c.BuildPushpinRoutes(), "\n") + "\n"
914-
err = os.WriteFile(pushpinRoutesFilePath, []byte(pushpinRoutesContents), 0o600)
915-
if err != nil {
916-
return pushpinCtx, fmt.Errorf("error writing routes file %s: %w", pushpinRoutesFilePath, err)
917-
}
918-
919-
if c.Globals.Verbose() {
920-
text.Info(out, "Starting local Pushpin...")
921-
text.Break(out)
922-
}
923905

924906
args := []string{
925-
fmt.Sprintf("--config=%s", pushpinConfFilePath),
907+
fmt.Sprintf("--config=%s", pushpinCtx.confFilePath),
926908
"--verbose",
927909
}
928910

@@ -940,17 +922,17 @@ func (c *ServeCommand) startPushpin(spinner text.Spinner, out io.Writer) (pushpi
940922
killProcessTree(pushpinCmd.Process.Pid)
941923
}
942924
if c.Globals.Verbose() {
943-
text.Output(out, "removing %s", pushpinRunDir)
925+
text.Output(out, "removing %s", pushpinCtx.pushpinRunDir)
944926
}
945-
_ = os.RemoveAll(pushpinRunDir)
927+
_ = os.RemoveAll(pushpinCtx.pushpinRunDir)
946928
if c.Globals.Verbose() {
947-
text.Output(out, "deleting %s", pushpinConfFilePath)
929+
text.Output(out, "deleting %s", pushpinCtx.confFilePath)
948930
}
949-
_ = os.Remove(pushpinConfFilePath)
931+
_ = os.Remove(pushpinCtx.confFilePath)
950932
if c.Globals.Verbose() {
951-
text.Output(out, "deleting %s", pushpinRoutesFilePath)
933+
text.Output(out, "deleting %s", pushpinCtx.routesFilePath)
952934
}
953-
_ = os.Remove(pushpinRoutesFilePath)
935+
_ = os.Remove(pushpinCtx.routesFilePath)
954936
cancel()
955937
})
956938
}
@@ -977,8 +959,13 @@ func (c *ServeCommand) startPushpin(spinner text.Spinner, out io.Writer) (pushpi
977959

978960
// Start Pushpin
979961
if c.Globals.Verbose() {
980-
text.Info(out, "Starting Pushpin runner in the background...")
981962
text.Output(out, "%s: %s", text.BoldYellow("Pushpin command"), strings.Join(pushpinCmd.Args, " "))
963+
text.Output(out, "%s: %d", text.BoldYellow("Pushpin proxy port"), pushpinCtx.proxyPort)
964+
text.Output(out, "%s: %d", text.BoldYellow("Pushpin publisher port"), pushpinCtx.publishPort)
965+
text.Output(out, "%s: %d - %d", text.BoldYellow("Pushpin other reserved ports"), pushpinCtx.publishPort+1, pushpinCtx.publishPort+3)
966+
text.Output(out, "%s: %s", text.BoldYellow("Pushpin temporary runtime directory"), pushpinCtx.pushpinRunDir)
967+
text.Output(out, "%s: %s", text.BoldYellow("Pushpin conf file"), pushpinCtx.confFilePath)
968+
text.Output(out, "%s: %s", text.BoldYellow("Pushpin routes file"), pushpinCtx.routesFilePath)
982969
}
983970
if err := pushpinCmd.Start(); err != nil {
984971
return pushpinCtx, fmt.Errorf("failed to start Pushpin runner: %w", err)
@@ -1018,14 +1005,12 @@ func (c *ServeCommand) startPushpin(spinner text.Spinner, out io.Writer) (pushpi
10181005
if err != nil {
10191006
return pushpinCtx, fsterr.RemediationError{
10201007
Inner: err,
1021-
Remediation: fmt.Sprintf("A process may already be running on port %d.", pushpinCtx.proxyPort),
1008+
Remediation: fmt.Sprintf("Check that your disk isn't full and that a process isn't already running on ports %d or %d - %d.", pushpinCtx.proxyPort, pushpinCtx.publishPort, pushpinCtx.publishPort+3),
10221009
}
10231010
}
10241011

1025-
if c.Globals.Verbose() {
1026-
text.Info(out, "Local Pushpin started on port %d.", pushpinCtx.proxyPort)
1027-
text.Break(out)
1028-
}
1012+
text.Success(out, "Local Pushpin started.")
1013+
text.Break(out)
10291014

10301015
return pushpinCtx, nil
10311016
}

0 commit comments

Comments
 (0)