fix: 增强 DockerContainer 初始化逻辑,添加重试机制和详细日志记录,以确保容器成功创建和启动
This commit is contained in:
parent
fd34e67142
commit
7049546225
@ -19,15 +19,33 @@ var DockerContainer *dockerContainer
|
|||||||
func init() {
|
func init() {
|
||||||
//检测docker服务是否已运行
|
//检测docker服务是否已运行
|
||||||
go func() {
|
go func() {
|
||||||
|
maxRetries := 5
|
||||||
|
retryCount := 0
|
||||||
for {
|
for {
|
||||||
// Initialize DockerContainer with memory, CPU, and disk limits
|
// Initialize DockerContainer with memory, CPU, and disk limits
|
||||||
DockerContainer, err := NewDockerContainer(int64(2*1024*1024*1024), int64(1000000000))
|
container, err := NewDockerContainer(int64(2*1024*1024*1024), int64(1000000000))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("NewDockerContainer failed", err)
|
retryCount++
|
||||||
time.Sleep(1 * time.Second)
|
if retryCount > maxRetries {
|
||||||
|
log.Fatalf("Failed to initialize Docker container after %d retries: %v", maxRetries, err)
|
||||||
|
}
|
||||||
|
log.Printf("NewDockerContainer failed (attempt %d/%d): %v", retryCount, maxRetries, err)
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
DockerContainer.CreateAndStartContainer()
|
|
||||||
|
if err := container.CreateAndStartContainer(); err != nil {
|
||||||
|
retryCount++
|
||||||
|
if retryCount > maxRetries {
|
||||||
|
log.Fatalf("Failed to create and start container after %d retries: %v", maxRetries, err)
|
||||||
|
}
|
||||||
|
log.Printf("CreateAndStartContainer failed (attempt %d/%d): %v", retryCount, maxRetries, err)
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
DockerContainer = container
|
||||||
|
log.Println("Docker container successfully initialized")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user