MCPcopy Create free account
hub / github.com/OpenSN-Library/OpenSN-Library / UpdateInstanceState

Function UpdateInstanceState

daemon/pkg/module/instance_module.go:205–245  ·  view source on GitHub ↗
(oldInstance, newInstance *model.Instance)

Source from the content-addressed store, hash-verified

203}
204
205func 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
247const InstanceModuleContainerName = "instance_manager"
248

Callers 1

watchInstanceDaemonFunction · 0.85

Calls 6

CreateContainerFunction · 0.85
StopContainerFunction · 0.85
RemoveContainerFunction · 0.85
StartContainerFunction · 0.85
IsRunningMethod · 0.65
IsCreatedMethod · 0.65

Tested by

no test coverage detected