Ejemplo n.º 1
0
Archivo: exec.go Proyecto: vmware/vic
// eventCallback will process events
func eventCallback(ie events.Event) {
	// grab the container from the cache
	container := Containers.Container(ie.Reference())
	if container != nil {

		newState := eventedState(ie.String(), container.CurrentState())
		// do we have a state change
		if newState != container.CurrentState() {
			switch newState {
			case StateStopping,
				StateRunning,
				StateStopped,
				StateSuspended:

				log.Debugf("Container(%s) state set to %s via event activity",
					container.ExecConfig.ID, newState.String())

				container.SetState(newState)
				if newState == StateStopped {
					container.onStop()
				}

				// container state has changed so we need to update the container attributes
				// we'll do this in a go routine to avoid blocking
				go func() {
					ctx, cancel := context.WithTimeout(context.Background(), propertyCollectorTimeout)
					defer cancel()

					err := container.Refresh(ctx)
					if err != nil {
						log.Errorf("Event driven container update failed: %s", err.Error())
					}
					// regardless of update success failure publish the container event
					publishContainerEvent(container.ExecConfig.ID, ie.Created(), ie.String())
				}()
			case StateRemoved:
				log.Debugf("Container(%s) %s via event activity", container.ExecConfig.ID, newState.String())
				Containers.Remove(container.ExecConfig.ID)
				publishContainerEvent(container.ExecConfig.ID, ie.Created(), ie.String())

			}
		}
	}
	return
}