Skip to content

Commit

Permalink
fix: Correct ProfileScan err handling and response
Browse files Browse the repository at this point in the history
Added more logging and return base response to autodiscovery as well
Close #1606

Signed-off-by: Cloud Tsai <cloudxxx8@gmail.com>
  • Loading branch information
cloudxxx8 committed Jul 30, 2024
1 parent fce9cd2 commit 2c0f4ed
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 deletions.
10 changes: 9 additions & 1 deletion internal/application/profilescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ func ProfileScanWrapper(busy chan bool, ps interfaces.ProfileScan, req sdkModels
profile, err := ps.ProfileScan(req)
if err != nil {
lc.Errorf("failed to trigger profile scan: %v", err.Error())
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)
releaseLock(req.DeviceName)
return
}
// Update device
deviceReq := requests.NewUpdateDeviceRequest(dtos.UpdateDevice{Name: &req.DeviceName, ProfileName: &profile.Name})
Expand All @@ -62,7 +66,11 @@ func ProfileScanWrapper(busy chan bool, ps interfaces.ProfileScan, req sdkModels
}

// ReleaseLock
releaseLock(req.DeviceName)
}

func releaseLock(deviceName string) {
locker.mux.Lock()
locker.busyMap[req.DeviceName] = false
locker.busyMap[deviceName] = false
locker.mux.Unlock()
}
23 changes: 19 additions & 4 deletions internal/controller/http/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"github.com/edgexfoundry/device-sdk-go/v3/internal/autodiscovery"
"github.com/edgexfoundry/device-sdk-go/v3/internal/cache"
"github.com/edgexfoundry/device-sdk-go/v3/internal/container"
"github.com/edgexfoundry/device-sdk-go/v3/internal/controller/http/correlation"
sdkModels "github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
"github.com/edgexfoundry/go-mod-bootstrap/v3/di"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
commonDTO "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/requests"
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v3/models"
Expand All @@ -43,8 +45,14 @@ func (c *RestController) Discovery(e echo.Context) error {

driver := container.ProtocolDriverFrom(c.dic.Get)

go autodiscovery.DiscoveryWrapper(driver, c.lc)
return c.sendResponse(writer, request, common.ApiDiscoveryRoute, nil, http.StatusAccepted)
correlationId := correlation.IdFromContext(request.Context())
go func() {
c.lc.Info("Discovery triggered.", common.CorrelationHeader, correlationId)
go autodiscovery.DiscoveryWrapper(driver, c.lc)
c.lc.Info("Discovery done.", common.CorrelationHeader, correlationId)
}()
response := commonDTO.NewBaseResponse("", "Trigger discovery with correlationId "+correlationId, http.StatusAccepted)
return c.sendResponse(writer, request, common.ApiDiscoveryRoute, response, http.StatusAccepted)
}

func (c *RestController) ProfileScan(e echo.Context) error {
Expand All @@ -71,14 +79,21 @@ func (c *RestController) ProfileScan(e echo.Context) error {
return c.sendEdgexError(writer, request, edgexErr, common.ApiProfileScan)
}

correlationId := correlation.IdFromContext(request.Context())
busy := make(chan bool)
go application.ProfileScanWrapper(busy, ps, req, c.dic)
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)
}()
b := <-busy
if b {
edgexErr := errors.NewCommonEdgeX(errors.KindStatusConflict, fmt.Sprintf("Another profile scan process for %s is currently running", req.DeviceName), nil)
return c.sendEdgexError(writer, request, edgexErr, common.ApiProfileScan)
}
return c.sendResponse(writer, request, common.ApiProfileScan, nil, http.StatusAccepted)

response := commonDTO.NewBaseResponse("", "Trigger profile scan with correlationId "+correlationId, http.StatusAccepted)
return c.sendResponse(writer, request, common.ApiProfileScan, response, http.StatusAccepted)
}

func profileScanValidation(request []byte, dic *di.Container) (sdkModels.ProfileScanRequest, errors.EdgeX) {
Expand Down
58 changes: 28 additions & 30 deletions openapi/v3/device-sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ paths:
responses:
'202':
description: The service is running the discovery request.
content:
application/json:
schema:
$ref: '#/components/schemas/BaseResponse'
'423':
description: The service is disabled or administratively locked.
content:
Expand Down Expand Up @@ -640,23 +644,21 @@ paths:
schema:
$ref: '#/components/schemas/ErrorResponse'
example:
value:
apiVersion: "v3"
requestId: "73f0932c-0148-11eb-adc1-0242ac120002"
statusCode: 400
message: "Bad Request"
apiVersion: "v3"
requestId: "73f0932c-0148-11eb-adc1-0242ac120002"
statusCode: 400
message: "Bad Request"
'404':
description: "The requested resource does not exist."
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
example:
value:
apiVersion: "v3"
requestId: "84c9489c-0148-11eb-adc1-0242ac120002"
statusCode: 404
message: "Not Found"
apiVersion: "v3"
requestId: "84c9489c-0148-11eb-adc1-0242ac120002"
statusCode: 404
message: "Not Found"
'409':
description: "Conflict detected. Profile name must be universally unique or previous request is still processing."
headers:
Expand All @@ -667,11 +669,10 @@ paths:
schema:
$ref: '#/components/schemas/ErrorResponse'
example:
value:
apiVersion: "v3"
requestId: "8a41b3f4-0148-11eb-adc1-0242ac120002"
statusCode: 409
message: "Data Duplicate"
apiVersion: "v3"
requestId: "8a41b3f4-0148-11eb-adc1-0242ac120002"
statusCode: 409
message: "Data Duplicate"
'423':
description: The device service is locked (admin state).
headers:
Expand All @@ -682,35 +683,32 @@ paths:
schema:
$ref: '#/components/schemas/ErrorResponse'
example:
value:
apiVersion: "v3"
requestId: "8a41b3f4-0148-11eb-adc1-0242ac120002"
statusCode: 423
message: "Locked"
apiVersion: "v3"
requestId: "8a41b3f4-0148-11eb-adc1-0242ac120002"
statusCode: 423
message: "Locked"
'500':
description: An unexpected error occurred on the server
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
example:
value:
apiVersion: "v3"
requestId: "e6e8a2f4-eb14-4649-9e2b-175247911369"
statusCode: 500
message: "Internal Server Error"
apiVersion: "v3"
requestId: "e6e8a2f4-eb14-4649-9e2b-175247911369"
statusCode: 500
message: "Internal Server Error"
'501':
description: Request is not supported
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
example:
value:
apiVersion: "v3"
requestId: "e6e8a2f4-eb14-4649-9e2b-175247911369"
statusCode: 501
message: "Not implemented"
apiVersion: "v3"
requestId: "e6e8a2f4-eb14-4649-9e2b-175247911369"
statusCode: 501
message: "Not implemented"

/config:
get:
Expand Down

0 comments on commit 2c0f4ed

Please sign in to comment.