(oldInstance, newInstance *model.Instance)
| 203 | } |
| 204 | |
| 205 | func UpdateInstanceState(oldInstance, newInstance *model.Instance) error { |
| 206 | var err error |
| 207 | InstanceParallelLimitChan <- 1 |
| 208 | defer func() { |
| 209 | <-InstanceParallelLimitChan |
| 210 | }() |
| 211 | isRunning := oldInstance.IsRunning() |
| 212 | isCreated := oldInstance.IsCreated() |
| 213 | |
| 214 | shouldRunning := newInstance.Start |
| 215 | shouldCreate := newInstance.InstanceID != "" |
| 216 | |
| 217 | if isCreated != shouldCreate { |
| 218 | if shouldCreate { |
| 219 | err = CreateContainer(newInstance) |
| 220 | } else { |
| 221 | StopContainer(oldInstance) |
| 222 | data.DeleteInstancePid(oldInstance.InstanceID) |
| 223 | err = RemoveContainer(oldInstance) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if err != nil { |
| 228 | return fmt.Errorf("update create state error: %s", err.Error()) |
| 229 | } |
| 230 | |
| 231 | if shouldRunning != isRunning { |
| 232 | if shouldRunning { |
| 233 | var pid int |
| 234 | pid, err = StartContainer(newInstance) |
| 235 | data.SetInstancePid(newInstance.InstanceID, pid) |
| 236 | } else if shouldCreate { |
| 237 | err = StopContainer(oldInstance) |
| 238 | data.DeleteInstancePid(oldInstance.InstanceID) |
| 239 | } |
| 240 | } |
| 241 | if err != nil { |
| 242 | return fmt.Errorf("update running state error: %s", err.Error()) |
| 243 | } |
| 244 | return nil |
| 245 | } |
| 246 | |
| 247 | const InstanceModuleContainerName = "instance_manager" |
| 248 |
no test coverage detected