-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Unified runtime environment status #7978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ func handleRuntime(create request.RuntimeCreate, runtime *model.Runtime, fileOp | |
| } | ||
| runtime.DockerCompose = string(composeContent) | ||
| runtime.Env = string(envContent) | ||
| runtime.Status = constant.RuntimeCreating | ||
| runtime.Status = constant.StatusCreating | ||
| runtime.CodeDir = create.CodeDir | ||
|
|
||
| nodeDetail, err := appDetailRepo.GetFirst(repo.WithByID(runtime.AppDetailID)) | ||
|
|
@@ -89,22 +89,22 @@ func handlePHP(create request.RuntimeCreate, runtime *model.Runtime, fileOp file | |
| runtime.DockerCompose = string(composeContent) | ||
| runtime.Env = string(envContent) | ||
| runtime.Params = string(forms) | ||
| runtime.Status = constant.RuntimeBuildIng | ||
| runtime.Status = constant.StatusBuilding | ||
|
|
||
| go buildRuntime(runtime, "", "", false) | ||
| return | ||
| } | ||
|
|
||
| func startRuntime(runtime *model.Runtime) { | ||
| if err := runComposeCmdWithLog("up", runtime.GetComposePath(), runtime.GetLogPath()); err != nil { | ||
| runtime.Status = constant.RuntimeError | ||
| runtime.Status = constant.StatusError | ||
| runtime.Message = err.Error() | ||
| _ = runtimeRepo.Save(runtime) | ||
| return | ||
| } | ||
|
|
||
| if err := SyncRuntimeContainerStatus(runtime); err != nil { | ||
| runtime.Status = constant.RuntimeError | ||
| runtime.Status = constant.StatusError | ||
| runtime.Message = err.Error() | ||
| _ = runtimeRepo.Save(runtime) | ||
| return | ||
|
|
@@ -115,7 +115,7 @@ func reCreateRuntime(runtime *model.Runtime) { | |
| var err error | ||
| defer func() { | ||
| if err != nil { | ||
| runtime.Status = constant.RuntimeError | ||
| runtime.Status = constant.StatusError | ||
| runtime.Message = err.Error() | ||
| _ = runtimeRepo.Save(runtime) | ||
| } | ||
|
|
@@ -155,6 +155,45 @@ func runComposeCmdWithLog(operate string, composePath string, logPath string) er | |
| return nil | ||
| } | ||
|
|
||
| func SyncRuntimesStatus(runtimes []model.Runtime) error { | ||
| cli, err := docker.NewClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer cli.Close() | ||
| var containerNames []string | ||
| runtimeContainer := make(map[string]int) | ||
| for index, runtime := range runtimes { | ||
| containerNames = append(containerNames, runtime.ContainerName) | ||
| runtimeContainer["/"+runtime.ContainerName] = index | ||
| } | ||
| containers, err := cli.ListContainersByName(containerNames) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, contain := range containers { | ||
| if index, ok := runtimeContainer[contain.Names[0]]; ok { | ||
| switch contain.State { | ||
| case "exited": | ||
| runtimes[index].Status = constant.StatusError | ||
| case "running": | ||
| runtimes[index].Status = constant.StatusRunning | ||
| case "paused": | ||
| runtimes[index].Status = constant.StatusStopped | ||
| case "restarting": | ||
| runtimes[index].Status = constant.StatusRestarting | ||
| } | ||
| delete(runtimeContainer, contain.Names[0]) | ||
| } | ||
| } | ||
| for _, index := range runtimeContainer { | ||
| if runtimes[index].Status != constant.StatusBuilding { | ||
| runtimes[index].Status = constant.StatusStopped | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func SyncRuntimeContainerStatus(runtime *model.Runtime) error { | ||
| env, err := gotenv.Unmarshal(runtime.Env) | ||
| if err != nil { | ||
|
|
@@ -182,14 +221,14 @@ func SyncRuntimeContainerStatus(runtime *model.Runtime) error { | |
|
|
||
| switch container.State { | ||
| case "exited": | ||
| runtime.Status = constant.RuntimeError | ||
| runtime.Status = constant.StatusError | ||
| case "running": | ||
| runtime.Status = constant.RuntimeRunning | ||
| runtime.Status = constant.StatusRunning | ||
| case "paused": | ||
| runtime.Status = constant.RuntimeStopped | ||
| runtime.Status = constant.StatusStopped | ||
| default: | ||
| if runtime.Status != constant.RuntimeBuildIng { | ||
| runtime.Status = constant.RuntimeStopped | ||
| if runtime.Status != constant.StatusBuilding { | ||
| runtime.Status = constant.StatusStopped | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -232,7 +271,7 @@ func buildRuntime(runtime *model.Runtime, oldImageID string, oldEnv string, rebu | |
|
|
||
| err = cmd.Run() | ||
| if err != nil { | ||
| runtime.Status = constant.RuntimeError | ||
| runtime.Status = constant.StatusError | ||
| runtime.Message = buserr.New("ErrImageBuildErr").Error() + ":" + stderrBuf.String() | ||
| if errors.Is(ctx.Err(), context.DeadlineExceeded) { | ||
| runtime.Message = buserr.New("ErrImageBuildErr").Error() + ":" + buserr.New("ErrCmdTimeout").Error() | ||
|
|
@@ -286,21 +325,21 @@ func buildRuntime(runtime *model.Runtime, oldImageID string, oldEnv string, rebu | |
| } | ||
|
|
||
| if out, err := compose.Up(composePath); err != nil { | ||
| runtime.Status = constant.RuntimeStartErr | ||
| runtime.Status = constant.StatusStartErr | ||
| runtime.Message = out | ||
| } else { | ||
| extensions := getRuntimeEnv(runtime.Env, "PHP_EXTENSIONS") | ||
| if extensions != "" { | ||
| installCmd := fmt.Sprintf("docker exec -i %s %s %s", runtime.ContainerName, "install-ext", extensions) | ||
| err = cmd2.ExecWithLogFile(installCmd, 60*time.Minute, logPath) | ||
| if err != nil { | ||
| runtime.Status = constant.RuntimeError | ||
| runtime.Status = constant.StatusError | ||
| runtime.Message = buserr.New("ErrImageBuildErr").Error() + ":" + err.Error() | ||
| _ = runtimeRepo.Save(runtime) | ||
| return | ||
| } | ||
| } | ||
| runtime.Status = constant.RuntimeRunning | ||
| runtime.Status = constant.StatusRunning | ||
| } | ||
| } | ||
| _ = runtimeRepo.Save(runtime) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code appears to be a part of a PHP-based application that manages running containers such as Docker compose and runtime details, including status updates. The provided code has no major problems; however, there's an issue regarding version numbers: It references Go versions 1.X.x which do not exist anymore from 2021 till now. To address this, you might want to update these pointers (e.g., to Please note, I am unable to directly test or inspect the functionality here due to the lack of context around how it integrates with larger systems/other modules and external APIs but this seems correct based on the available information without additional data points. For optimizations:
Here’s an example of improved code structure considering the given code snippets: package application
import (
"context"
"fmt"
"github.com/docker/compose-go/api/v1/types/container"
docker "github.com/opencontainers/image-spec/specs-go/latest/docker-image"
)
type ModelRuntimeDetails interface {
GetAppID(context.Context) (*int64, error)
}
func GetModelRuntimeByApp(app ModelApplicationDetail) (ResultResponse[*models.Runtime], Error) {
appIDs, exists := appMap[app.ID]
result := ResultResponse[List{models.Runtime{}}, string>()
if !exists || len(*appIDs) == 0 {
result.Error = ErrUnknownAppName {AppName: app.Name}
return result, &result.Error
}
var rList []models.Runtime
for i, id := range *appIDs {
runtimeId := models.NewRuntime(id)
exists, err := rService.GetServiceByName(
context.TODO(),
docker.Tag{"latest"},
fmt.Sprintf("/com.docker.compose.client.v2beta1.models/%v",
model.RtTagKey),
).Execute(&model.ModelServiceGetResult{
ID: int32(rt.Id()),
Name: rt.AppName,
Status: model.Running,
Version: rt.Version,
})
if err != nil {
panic(fmt.Errorf("Failed to call service to fetch runtime %w", err))
}
runtimeIds[i] = runtimeId
log.Infof("%#v -> %#v", runtimeId, rt)
result.Data[runtimeIds[i]] = &rt
}
result.SetData(result.Data)
return result, nil
}
// In the actual implementation, instead of calling methods like Service, handleRuntime is called to take care of all related operations, reducing overhead.Remember to replace placeholder values ( As a reminder, always review the codebase thoroughly, refactor logically related parts together, optimize dependencies, and implement safety checks effectively throughout different aspects of your application to ensure robustness and reliability. This provides general hints on best practices across various sections of software development. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code is well-structured and appears to be formatted correctly in Go. There are no known issues present with this package script file.