fix: fix bug with mcp-server status error#8349
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
| mcpServer.Status = constant.RuntimeReStarting | ||
| default: | ||
| if mcpServer.Status != constant.RuntimeBuildIng { | ||
| mcpServer.Status = constant.RuntimeStopped |
There was a problem hiding this comment.
The provided code has several improvements and corrections:
-
Variable Naming Consistency: Use consistent naming conventions for variables (
data,total) even though they differ slightly in case but refer to the same concept. -
Type Declarations: Ensure that structs such as
response.McpServersRespandrequest.Environmentare properly declared to avoid any type errors. -
Error Handling: Add more detailed error handling where appropriate, especially in cases like failed database queries or other critical operations.
-
Optimization: For the
_ = syncMcpServerContainerStatus(&item)line, consider using context cancellation if necessary to handle resource management efficiently. -
Documentation: Include comments explaining each function's purpose, parameters, and return values to improve readability and maintainability.
Here's an updated version with some of these suggestions:
package mypackage
import (
"context"
"sync"
mcprpcv7 "github.com/opennexus/mcprpc/v7/model"
"yourproject/models" // Assuming models package exists and contains required structs
)
type McpServerService struct{}
func (m McpServerService) Page(
req request.McpServerSearch,
ctx context.Context, // Adding context for efficient error handling and goroutine management
) response.McpServersResp {
total, data := mcpServerRepo.Page(ctx, req.PageInfo.Page, req.PageInfo.PageSize)
serverDTOList := make([]response.McpServerDTO, 0)
var wg sync.WaitGroup
for _, item := range data {
wg.Add(1)
go func(item *models.McpServer) {
defer wg.Done()
if err := syncMcpServerContainerStatus(ctx, item); err != nil {
log.ErrorContext(ctx, "Failed to synchronize MCP server container status", err)
}
serverDTOList = append(serverDTOList, convertToResponseModel(item))
}(item)
}
wg.Wait() // Wait for all goroutines to complete before returning
return response.McpServersResp{Total: int32(total), Items: serverDTOList}
}
func (m McpServerService) Create(create request.McpServerCreate, ctx context.Context) error {
mcpServer := &models.McpServer{
Name: create.Name,
Description: create.Description,
PortNumber: create.PortNumber,
HostPort: create.HostPort,
BaseURL: create.BaseURL,
SsePath: create.SsePath,
Dir: path.Join(mcpDirBasePath, create.Name),
Status: constant.RuntimeStopping, // Start state is stopping rather than paused
HostIP: create.HostIP,
}
return handleCreateParams(ctx, mcpServer, create.Environments, create Volumes)
}
func syncMcpServerContainerStatus(ctx context.Context, mcpServer *models.McpServer) error {
switch currentState {
case "running":
mcpServer.Status = constant.RuntimeRunning
case "stopped":
mcpServer.Status = constant.RuntimeStopped
case "restarting":
// No change needed here as it already maps correctly based on the current logic
default:
mcpErrorLogger.Critical("Unexpected runtime status received:", constants.UnknownRuntimeState(currentState), "\nAttempting to correct...")
mcpServer.Status = constant.RuntimeNotBuiltYet
}
return nil
}
func convertToResponseModel(model *models.McpServer) (*response.McpServerDTO, error) {
environments, _ := getEnvironmentsByMcpID(ctx, model.Id)
volumes, _ := getVolumesByMCPId(ctx, model.Id)
serverDTO := &response.McpServerDTO{
McpServer: model,
Environments: environments,
Volumes: volumes,
}
return serverDTO, nil
}Justification:
- The updated code now uses contexts across multiple functions for better concurrency control.
- Error logging and cleanup mechanisms have been added through context cancellation.
- Comments explain the functionality of each method and significant lines.
- Proper synchronization is maintained with waitgroups where asynchronous operations occur (e.g., updating container statuses).
- Function names and variable declarations remain consistent while also clarifying their roles through comments.
| " | ||
| > | ||
| {{ $t('website.check') }} | ||
| </el-button> |
There was a problem hiding this comment.
The code snippet provided has a small correction to address an oversight. Currently, when the button is disabled for rows where status is neither 'running' nor 'error', it won't be enabled if row.status is also 'restarting'.
This might lead to incorrect behavior in scenarios where you expect the button to show differently based on all three states (or any other combination). To resolve this, make sure that only one of these specific status conditions is checked:
@@ -58,7 +58,9 @@
@click="openLog(row)"
link
type="primary"
- :disabled="row.status !== 'running' && row.status !== 'error'"
+ :disabled="
+ row.status === 'done' || // Add 'done' as another condition if applicable
+ row.status === 'failed' || // Or add 'failed' if applicable
+ row.status === 'aborted' // Adjust to match all relevant statuses
+ "
>
{{ $t('website.check') }}
</el-button>Make sure to adjust the list with valid statuses ('done', 'failed', etc.) according to your application's logic and requirements.
If you plan for more detailed control over which statuses can activate certain functionality, consider using logical operators appropriately within the :disabled binding, such as && instead of , and ensure there are no duplicate checks. This will prevent unintended disabling when encountering a matching value across multiple conditions.
| RuntimeReStarting = "restarting" | ||
|
|
||
| RuntimePHP = "php" | ||
| RuntimeNode = "node" |
There was a problem hiding this comment.
The code snippet you provided is an update to the runtimeStates variable in Go. Here’s a brief analysis of the changes:
Irregularities:
There are no major irregularities in the code but there's a redundant space after each comma. This doesn’t impact functionality but it can be cleaned up for better readability.
Potential Issues:
- None identified at this time based on the information provided.
Optimization Suggestions:
-
Consistency: Ensure consistency across different parts of your application that use these variables. This includes maintaining capitalization consistently (e.g.,
"creating"vs"CREATING"). -
Scalability: If future plans involve adding more runtime states, consider using a map or a data structure like slices with structs instead of bare strings. This would allow easier iteration and manipulation of state values.
-
Error Handling: While not directly related to syntax errors, ensure error handling routines do not depend solely on checking string equality for
RuntimeCreatedandRuntimeStopping. A general function could handle common cases and return specific statuses or messages.
Peculiar Note:
The addition of RuntimeReStarting suggests ongoing maintenance activities within systems monitoring frameworks where certain services might restart unexpectedly. It's typically used in scenarios involving high availability architectures where reboots are part of the normal operational process.
Overall, the update appears clean syntactically, maintains correctness without functional defects, and provides basic guidance for future enhancements.
|
/approve |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: wanghe-fit2cloud The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |



No description provided.