Skip to content

Conversation

mikeland73
Copy link
Contributor

@mikeland73 mikeland73 commented Feb 6, 2023

Summary

This PR enables automatic port forwarding when services are started in devbox cloud. It does this by maintaining synced "status" files in .devbox.cloud/[host-id]

How to review this PR:

  • cloud.AutoPortForward is a background thread that listens to changes in .devbox.cloud/[host-id] and starts port forwarding as needed. It only runs locally.
  • services.ListenToChanges listens to changes in .devbox.cloud/[host-id] and calls a callback when an event occurs. The path of a service status file is .devbox.cloud/[host-id]/[service-name].json. It was modified to listen to statuses of individual services. I did this because having a single services.json could lead to file edit race conditions when starting/stoping multiple services.
  • services.listenToAutoPortForwardingChangesOnRemote only runs on the cloud. It's the complement to cloud.AutoPortForward. It listens for when port forwarding has already been set up so it can print out the local ports on the cloud shell. It waits for 5 seconds and then gives up.

How was it tested?

  • Added nginx and apache.
  • Started cloud shell. Copied over newest devbox binary.
  • Ran devbox services start/stop in several combinations and observed that port forwarding started and stopped as expected.

image

image

@mikeland73 mikeland73 requested review from gcurtis and loreto February 7, 2023 00:19
@mikeland73 mikeland73 marked this pull request as ready for review February 7, 2023 00:22
return status, errors.WithStack(json.Unmarshal(content, status))
}

func hostname() (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.Hostname()?

return errors.WithStack(err)
}
return cloud.Shell(cmd.ErrOrStderr(), box.ProjectDir(), flags.githubUsername)
return cloud.Shell(cmd.Context(), cmd.ErrOrStderr(), box.ProjectDir(), flags.githubUsername)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this context get set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt.Fprintf(w, "Waiting for port forwarding to start/stop for service %q\n", serviceName)
waitGroup.Add(1)

ctx, cancel := context.WithCancel(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the cancel of this context be deferred? It ultimately needs to be called everywhere otherwise it'll leak resources.


// After 5 seconds, if the context has not been canceled, go ahead and cancel it
// and also decrement the wait group so that the caller can continue.
time.AfterFunc(5*time.Second, func() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be done with context.WithTimeout?

Copy link
Contributor Author

@mikeland73 mikeland73 Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would need a timeout and also a cancel. I'll change


// Done function is thread safe and will cancel the context and decrement the wait group
// if context is already canceled, it will not do anything
var m sync.Mutex
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a sync.Once instead of a mutex?

@mikeland73 mikeland73 merged commit 60a4f60 into main Feb 10, 2023
@mikeland73 mikeland73 deleted the landau/start-auto-forward-on-cloud branch February 10, 2023 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants