github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3em/agent/options.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package agent 22 23 import ( 24 "fmt" 25 "os" 26 "time" 27 28 "github.com/m3db/m3/src/m3em/os/exec" 29 xclock "github.com/m3db/m3/src/x/clock" 30 "github.com/m3db/m3/src/x/instrument" 31 ) 32 33 const ( 34 // defaultHeartbeatTimeout is the default heartbeat timeout 35 defaultHeartbeatTimeout = 2 * time.Minute 36 37 // defaultNewFileMode is the file mode used for new files by default 38 defaultNewFileMode = os.FileMode(0666) 39 40 // defaultNewDirectoryMode is the file mode used for new directories by default 41 defaultNewDirectoryMode = os.ModeDir | os.FileMode(0755) 42 ) 43 44 var ( 45 defaultNoErrorFn = func() error { 46 return nil 47 } 48 ) 49 50 type opts struct { 51 iopts instrument.Options 52 workingDir string 53 execGenFn ExecGenFn 54 initFn HostResourcesFn 55 releaseFn HostResourcesFn 56 envMap exec.EnvMap 57 heartbeatTimeout time.Duration 58 nowFn xclock.NowFn 59 newFileMode os.FileMode 60 newDirectoryMode os.FileMode 61 } 62 63 // NewOptions constructs new options 64 func NewOptions(io instrument.Options) Options { 65 return &opts{ 66 iopts: io, 67 initFn: defaultNoErrorFn, 68 releaseFn: defaultNoErrorFn, 69 heartbeatTimeout: defaultHeartbeatTimeout, 70 nowFn: time.Now, 71 newFileMode: defaultNewFileMode, 72 newDirectoryMode: defaultNewDirectoryMode, 73 } 74 } 75 76 func (o *opts) Validate() error { 77 if o.execGenFn == nil { 78 return fmt.Errorf("ExecGenFn is not set") 79 } 80 81 if o.workingDir == "" { 82 return fmt.Errorf("WorkingDirectory is not set") 83 } 84 85 return nil 86 } 87 88 func (o *opts) SetInstrumentOptions(io instrument.Options) Options { 89 o.iopts = io 90 return o 91 } 92 93 func (o *opts) InstrumentOptions() instrument.Options { 94 return o.iopts 95 } 96 97 func (o *opts) SetWorkingDirectory(wd string) Options { 98 o.workingDir = wd 99 return o 100 } 101 102 func (o *opts) WorkingDirectory() string { 103 return o.workingDir 104 } 105 106 func (o *opts) SetExecGenFn(fn ExecGenFn) Options { 107 o.execGenFn = fn 108 return o 109 } 110 111 func (o *opts) ExecGenFn() ExecGenFn { 112 return o.execGenFn 113 } 114 115 func (o *opts) SetInitHostResourcesFn(fn HostResourcesFn) Options { 116 o.initFn = fn 117 return o 118 } 119 120 func (o *opts) InitHostResourcesFn() HostResourcesFn { 121 return o.initFn 122 } 123 124 func (o *opts) SetReleaseHostResourcesFn(fn HostResourcesFn) Options { 125 o.releaseFn = fn 126 return o 127 } 128 129 func (o *opts) ReleaseHostResourcesFn() HostResourcesFn { 130 return o.releaseFn 131 } 132 133 func (o *opts) SetEnvMap(em exec.EnvMap) Options { 134 o.envMap = em 135 return o 136 } 137 138 func (o *opts) EnvMap() exec.EnvMap { 139 return o.envMap 140 } 141 142 func (o *opts) SetHeartbeatTimeout(t time.Duration) Options { 143 o.heartbeatTimeout = t 144 return o 145 } 146 147 func (o *opts) HeartbeatTimeout() time.Duration { 148 return o.heartbeatTimeout 149 } 150 151 func (o *opts) SetNowFn(fn xclock.NowFn) Options { 152 o.nowFn = fn 153 return o 154 } 155 156 func (o *opts) NowFn() xclock.NowFn { 157 return o.nowFn 158 } 159 160 func (o *opts) SetNewFileMode(value os.FileMode) Options { 161 o.newFileMode = value 162 return o 163 } 164 165 func (o *opts) NewFileMode() os.FileMode { 166 return o.newFileMode 167 } 168 169 func (o *opts) SetNewDirectoryMode(value os.FileMode) Options { 170 o.newDirectoryMode = value 171 return o 172 } 173 174 func (o *opts) NewDirectoryMode() os.FileMode { 175 return o.newDirectoryMode 176 }