github.com/demonoid81/containerd@v1.3.4/runtime/monitor.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package runtime 18 19 // TaskMonitor provides an interface for monitoring of containers within containerd 20 type TaskMonitor interface { 21 // Monitor adds the provided container to the monitor 22 Monitor(Task) error 23 // Stop stops and removes the provided container from the monitor 24 Stop(Task) error 25 } 26 27 // NewMultiTaskMonitor returns a new TaskMonitor broadcasting to the provided monitors 28 func NewMultiTaskMonitor(monitors ...TaskMonitor) TaskMonitor { 29 return &multiTaskMonitor{ 30 monitors: monitors, 31 } 32 } 33 34 // NewNoopMonitor is a task monitor that does nothing 35 func NewNoopMonitor() TaskMonitor { 36 return &noopTaskMonitor{} 37 } 38 39 type noopTaskMonitor struct { 40 } 41 42 func (mm *noopTaskMonitor) Monitor(c Task) error { 43 return nil 44 } 45 46 func (mm *noopTaskMonitor) Stop(c Task) error { 47 return nil 48 } 49 50 type multiTaskMonitor struct { 51 monitors []TaskMonitor 52 } 53 54 func (mm *multiTaskMonitor) Monitor(c Task) error { 55 for _, m := range mm.monitors { 56 if err := m.Monitor(c); err != nil { 57 return err 58 } 59 } 60 return nil 61 } 62 63 func (mm *multiTaskMonitor) Stop(c Task) error { 64 for _, m := range mm.monitors { 65 if err := m.Stop(c); err != nil { 66 return err 67 } 68 } 69 return nil 70 }