MCPcopy Create free account
hub / github.com/cortexproject/cortex / main

Method main

pkg/util/services/basic_service.go:153–225  ·  view source on GitHub ↗

This is the "main" method, that does most of the work of service. Service is in Starting state when this method runs. Entire lifecycle of the service happens here.

()

Source from the content-addressed store, hash-verified

151// Service is in Starting state when this method runs.
152// Entire lifecycle of the service happens here.
153func (b *BasicService) main() {
154 var err error
155
156 if b.startFn != nil {
157 err = b.startFn(b.serviceContext)
158 }
159
160 if err != nil {
161 b.mustSwitchState(Starting, Failed, func() {
162 b.failureCase = err
163 b.serviceCancel() // cancel the context, just in case if anything started in StartingFn is using it
164 // we will not reach Running or Terminated, notify waiters
165 close(b.runningWaitersCh)
166 close(b.terminatedWaitersCh)
167 b.notifyListeners(func(l Listener) { l.Failed(Starting, err) }, true)
168 })
169 return
170 }
171
172 stoppingFrom := Starting
173
174 // Starting has succeeded. We should switch to Running now, but let's not do that
175 // if our context has been canceled in the meantime.
176 if err = b.serviceContext.Err(); err != nil {
177 err = nil // don't report this as a failure, it is a normal "stop" signal.
178 goto stop
179 }
180
181 // We have reached Running state
182 b.mustSwitchState(Starting, Running, func() {
183 // unblock waiters waiting for Running state
184 close(b.runningWaitersCh)
185 b.notifyListeners(func(l Listener) { l.Running() }, false)
186 })
187
188 stoppingFrom = Running
189 if b.runningFn != nil {
190 err = b.runningFn(b.serviceContext)
191 }
192
193stop:
194 failure := err
195 b.mustSwitchState(stoppingFrom, Stopping, func() {
196 if stoppingFrom == Starting {
197 // we will not reach Running state
198 close(b.runningWaitersCh)
199 }
200 b.notifyListeners(func(l Listener) { l.Stopping(stoppingFrom) }, false)
201 })
202
203 // Make sure we cancel the context before running stoppingFn
204 b.serviceCancel()
205
206 if b.stoppingFn != nil {
207 err = b.stoppingFn(failure)
208 if failure == nil {
209 failure = err
210 }

Callers 1

StartAsyncMethod · 0.95

Calls 7

mustSwitchStateMethod · 0.95
notifyListenersMethod · 0.95
FailedMethod · 0.65
ErrMethod · 0.65
RunningMethod · 0.65
StoppingMethod · 0.65
TerminatedMethod · 0.65

Tested by

no test coverage detected