-
Notifications
You must be signed in to change notification settings - Fork 265
[cloud] Auto-port forward during cloud shell #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
internal/services/status.go
Outdated
return status, errors.WithStack(json.Unmarshal(content, status)) | ||
} | ||
|
||
func hostname() (string, error) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It gets set when we execute cobra root command https://github.com/jetpack-io/devbox/blob/main/internal/boxcli/midcobra/midcobra.go#L64-L64
internal/services/services.go
Outdated
fmt.Fprintf(w, "Waiting for port forwarding to start/stop for service %q\n", serviceName) | ||
waitGroup.Add(1) | ||
|
||
ctx, cancel := context.WithCancel(ctx) |
There was a problem hiding this comment.
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.
internal/services/services.go
Outdated
|
||
// 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() { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
internal/services/services.go
Outdated
|
||
// 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 |
There was a problem hiding this comment.
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?
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 singleservices.json
could lead to file edit race conditions when starting/stoping multiple services.services.listenToAutoPortForwardingChangesOnRemote
only runs on the cloud. It's the complement tocloud.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?
devbox services start/stop
in several combinations and observed that port forwarding started and stopped as expected.