24 lines
1017 B
Cheetah
24 lines
1017 B
Cheetah
package servicecontext
|
|
import "errors"
|
|
// Close is idempotent, including startup failure and graceful shutdown.
|
|
func(c *Context)Close()error{
|
|
if c==nil{return nil}
|
|
c.closeOnce.Do(func(){
|
|
if c.Lifecycle!=nil{c.Lifecycle.RequestShutdown(nil)}
|
|
c.closeMu.Lock();c.closed=true;cleanups:=c.cleanups;c.cleanups=nil;c.closeMu.Unlock()
|
|
for i:=len(cleanups)-1;i>=0;i--{c.closeErr=errors.Join(c.closeErr,cleanups[i]())}
|
|
{{if ne .Storage "none"}}if c.storageClose!=nil{c.closeErr=errors.Join(c.closeErr,c.storageClose())}{{end}}
|
|
{{if eq .Database "mysql"}}if c.DB!=nil{c.closeErr=errors.Join(c.closeErr,c.DB.Close())}{{end}}
|
|
})
|
|
return c.closeErr
|
|
}
|
|
|
|
// OnClose transfers a resource cleanup to this context immediately after creation.
|
|
// Hooks run once in reverse order, also when later initialization fails.
|
|
func(c *Context)OnClose(cleanup func()error) {
|
|
if cleanup==nil{return}
|
|
c.closeMu.Lock();defer c.closeMu.Unlock()
|
|
if c.closed{panic("svc: register cleanup after Close")}
|
|
c.cleanups=append(c.cleanups,cleanup)
|
|
}
|