Skip to content

Commit

Permalink
Merge pull request #1609 from cloudxxx8/issue-1608
Browse files Browse the repository at this point in the history
refactor: Refine the discovery and scan logs
  • Loading branch information
jinlinGuan authored Aug 7, 2024
2 parents 5c2a377 + c1d0121 commit 53be428
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 6 additions & 8 deletions internal/application/profilescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"context"
"sync"

"github.com/google/uuid"

"github.com/edgexfoundry/device-sdk-go/v3/pkg/interfaces"
sdkModels "github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/container"
Expand All @@ -27,7 +25,7 @@ type profileScanLocker struct {

var locker = profileScanLocker{busyMap: make(map[string]bool)}

func ProfileScanWrapper(busy chan bool, ps interfaces.ProfileScan, req sdkModels.ProfileScanRequest, dic *di.Container) {
func ProfileScanWrapper(busy chan bool, ps interfaces.ProfileScan, req sdkModels.ProfileScanRequest, correlationId string, dic *di.Container) {
locker.mux.Lock()
b := locker.busyMap[req.DeviceName]
busy <- b
Expand All @@ -41,28 +39,28 @@ func ProfileScanWrapper(busy chan bool, ps interfaces.ProfileScan, req sdkModels
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
dpc := bootstrapContainer.DeviceProfileClientFrom(dic.Get)
dc := bootstrapContainer.DeviceClientFrom(dic.Get)
ctx := context.WithValue(context.Background(), common.CorrelationHeader, uuid.NewString()) //nolint: staticcheck
ctx := context.WithValue(context.Background(), common.CorrelationHeader, correlationId) //nolint: staticcheck

lc.Debugf("profile scan triggered with device name '%s' and profile name '%s'", req.DeviceName, req.ProfileName)
lc.Debugf("profile scan triggered with device name '%s' and profile name '%s', with Correlation Id '%s'", req.DeviceName, req.ProfileName, correlationId)
profile, err := ps.ProfileScan(req)
if err != nil {
lc.Errorf("failed to trigger profile scan: %v", err.Error())
lc.Errorf("failed to trigger profile scan: %v, with Correlation Id '%s'", err.Error(), correlationId)
releaseLock(req.DeviceName)
return
}
// Add profile to metadata
profileReq := requests.NewDeviceProfileRequest(dtos.FromDeviceProfileModelToDTO(profile))
_, err = dpc.Add(ctx, []requests.DeviceProfileRequest{profileReq})
if err != nil {
lc.Errorf("failed to add device profile '%s': %v", profile.Name, err)
lc.Errorf("failed to add device profile '%s': %v, with Correlation Id '%s'", profile.Name, err, correlationId)
releaseLock(req.DeviceName)
return
}
// Update device
deviceReq := requests.NewUpdateDeviceRequest(dtos.UpdateDevice{Name: &req.DeviceName, ProfileName: &profile.Name})
_, err = dc.Update(ctx, []requests.UpdateDeviceRequest{deviceReq})
if err != nil {
lc.Errorf("failed to update device '%s' with profile '%s': %v", req.DeviceName, profile.Name, err)
lc.Errorf("failed to update device '%s' with profile '%s': %v, with Correlation Id '%s'", req.DeviceName, profile.Name, err, correlationId)
}

// ReleaseLock
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/http/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *RestController) Discovery(e echo.Context) error {
go func() {
c.lc.Info("Discovery triggered.", common.CorrelationHeader, correlationId)
go autodiscovery.DiscoveryWrapper(driver, c.lc)
c.lc.Info("Discovery done.", common.CorrelationHeader, correlationId)
c.lc.Info("Discovery end.", common.CorrelationHeader, correlationId)
}()
response := commonDTO.NewBaseResponse("", "Trigger discovery with correlationId "+correlationId, http.StatusAccepted)
return c.sendResponse(writer, request, common.ApiDiscoveryRoute, response, http.StatusAccepted)
Expand Down Expand Up @@ -83,8 +83,8 @@ func (c *RestController) ProfileScan(e echo.Context) error {
busy := make(chan bool)
go func() {
c.lc.Info("Profile scanning is triggered.", common.CorrelationHeader, correlationId)
application.ProfileScanWrapper(busy, ps, req, c.dic)
c.lc.Info("Profile scanning is done.", common.CorrelationHeader, correlationId)
application.ProfileScanWrapper(busy, ps, req, correlationId, c.dic)
c.lc.Info("Profile scanning is end.", common.CorrelationHeader, correlationId)
}()
b := <-busy
if b {
Expand Down Expand Up @@ -123,7 +123,7 @@ func profileScanValidation(request []byte, dic *di.Container) (sdkModels.Profile
return r, errors.NewCommonEdgeX(errors.KindStatusConflict, fmt.Sprintf("profile name %s is duplicated", req.ProfileName), nil)
}
} else {
req.ProfileName = fmt.Sprintf("%s_profile_%d", req.DeviceName, time.Now().UnixNano())
req.ProfileName = fmt.Sprintf("%s_profile_%d", req.DeviceName, time.Now().UnixMilli())
}

r = sdkModels.ProfileScanRequest{
Expand Down

0 comments on commit 53be428

Please sign in to comment.