diff --git a/internal/application/profilescan.go b/internal/application/profilescan.go index 30feec896..5b0cf7fea 100644 --- a/internal/application/profilescan.go +++ b/internal/application/profilescan.go @@ -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}) @@ -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() } diff --git a/internal/controller/http/discovery.go b/internal/controller/http/discovery.go index 6907ab6e9..8e5759e96 100644 --- a/internal/controller/http/discovery.go +++ b/internal/controller/http/discovery.go @@ -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" @@ -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 { @@ -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) { diff --git a/openapi/v3/device-sdk.yaml b/openapi/v3/device-sdk.yaml index a7a14b33b..4ff99d67f 100644 --- a/openapi/v3/device-sdk.yaml +++ b/openapi/v3/device-sdk.yaml @@ -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: @@ -640,11 +644,10 @@ 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: @@ -652,11 +655,10 @@ paths: 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: @@ -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: @@ -682,11 +683,10 @@ 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: @@ -694,11 +694,10 @@ paths: 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: @@ -706,11 +705,10 @@ paths: 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: