Skip to content

fix: discovery cleanup and watchers continuity upon core install #2062

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

Merged
merged 9 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: pr comments
  • Loading branch information
Bikappa committed Mar 13, 2023
commit 27625c5be0806c35401ef86e7d3988377fde7af3
1 change: 1 addition & 0 deletions arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (pmb *Builder) BuildIntoExistingPackageManager(target *PackageManager) {
} else {
target.discoveryManager = pmb.discoveryManager
}
target.discoveryManager.AddAllDiscoveriesFrom(pmb.discoveryManager)
target.userAgent = pmb.userAgent
}

Expand Down
17 changes: 11 additions & 6 deletions arduino/discovery/discoverymanager/discoverymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,9 @@ func (dm *DiscoveryManager) Clear() {
logrus.Infof("Closed and removed discovery %s", d.GetID())
}
}
dm.discoveriesRunning = false
dm.discoveries = map[string]*discovery.PluggableDiscovery{}
}

// AreDiscoveriesRunning returns a boolean representing the running status of discoveries
func (dm *DiscoveryManager) AreDiscoveriesRunning() bool {
return dm.discoveriesRunning
}

// IDs returns the list of discoveries' ids in this DiscoveryManager
func (dm *DiscoveryManager) IDs() []string {
ids := []string{}
Expand Down Expand Up @@ -283,3 +277,14 @@ func (dm *DiscoveryManager) List() []*discovery.Port {
}
return res
}

// AddAllDiscoveriesFrom transfers discoveries from src to the receiver
func (dm *DiscoveryManager) AddAllDiscoveriesFrom(src *DiscoveryManager) {
for id, d := range src.discoveries {
dm.discoveries[id] = d
}

if src.discoveriesRunning {
dm.Start()
}
}
19 changes: 1 addition & 18 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
},
})
}
var shouldRestartDiscovery bool

{
// We need to rebuild the PackageManager currently in use by this instance
// in case this is not the first Init on this instances, that might happen
Expand All @@ -271,7 +271,6 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro

// register whether the discoveries are running, if so we need to start them in
// order for the previous watchers to keep receiving events
shouldRestartDiscovery = areDiscoveriesRunning(instance.pm)
pmb, commitPackageManager := instance.pm.NewBuilder()

loadBuiltinTools := func() []error {
Expand Down Expand Up @@ -467,9 +466,6 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
responseError(s)
}

if shouldRestartDiscovery {
pme.DiscoveryManager().Start()
}
// Refreshes the locale used, this will change the
// language of the CLI if the locale is different
// after started.
Expand All @@ -478,19 +474,6 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
return nil
}

func areDiscoveriesRunning(pm *packagemanager.PackageManager) bool {
if pm == nil {
return false
}
exp, release := pm.NewExplorer()
defer release()

if exp.DiscoveryManager() != nil && exp.DiscoveryManager().AreDiscoveriesRunning() {
return true
}
return false
}

// Destroy FIXMEDOC
func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
if ok := instances.RemoveID(req.GetInstance().GetId()); !ok {
Expand Down