func()

in openwhisk/stopHandler.go [41:101]


func (ap *ActionProxy) stopHandler(w http.ResponseWriter, r *http.Request) {
	if ap.proxyMode != ProxyModeServer {
		sendError(w, http.StatusUnprocessableEntity, "Stop is only supported in server mode")
		return
	}

	if ap.serverProxyData == nil {
		Debug("Server proxy data not initialized... a restart might have happened!")
		sendError(w, http.StatusInternalServerError, "Server proxy data not initialized")
		return
	}

	// parse the request
	body, err := io.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		sendError(w, http.StatusBadRequest, fmt.Sprintf("Error reading request body: %v", err))
		return
	}

	var stopRequest stopRequest
	err = json.NewDecoder(bytes.NewReader(body)).Decode(&stopRequest)
	if err != nil {
		sendError(w, http.StatusBadRequest, fmt.Sprintf("Error decoding run body: %v", err))
		return
	}

	innerAPValue, ok := ap.serverProxyData.actions[stopRequest.ActionCodeHash]
	if !ok {
		Debug("Action hash '%s' not found in server proxy data", stopRequest.ActionCodeHash)
		sendError(w, http.StatusNotFound, "Action to be removed in remote runtime not found. Check logs for details.")
		return
	}

	connectedIDFound := false
	for i, connectedID := range innerAPValue.connectedActionIDs {
		if connectedID == stopRequest.ProxiedActionID {
			// remove id from the array
			innerAPValue.connectedActionIDs = removeID(innerAPValue.connectedActionIDs, i)
			connectedIDFound = true
			break
		}
	}
	if !connectedIDFound {
		Debug("Action ID '%s' not found in server proxy data", stopRequest.ProxiedActionID)
		sendError(w, http.StatusNotFound, "Action to be removed in remote runtime not found. Check logs for details.")
		return
	}

	Debug("Removed action ID. Length of connectedActionIDs: %d", len(innerAPValue.connectedActionIDs))

	if len(innerAPValue.connectedActionIDs) == 0 {
		if isSetupActionRunning(stopRequest.ActionCodeHash) {
			go ap.timedDelete(stopRequest.ActionCodeHash)
		} else {
			stopAndDelete(ap, innerAPValue, stopRequest.ActionCodeHash)
		}
	}

	sendOK(w)
}