draincloud-core/internal/closer/closer.go
2024-10-11 00:36:51 +03:00

41 lines
713 B
Go

package closer
import (
"context"
"errors"
"git.optclblast.xyz/draincloud/draincloud-core/internal/logger"
)
var globalCloser *Closer = &Closer{
closeFns: make([]func() error, 0),
}
type Closer struct {
closeFns []func() error
}
func (c *Closer) Add(fn func() error) {
c.closeFns = append(c.closeFns, fn)
}
func (c *Closer) Close() error {
var commonErr error
for _, fn := range c.closeFns {
if err := fn(); err != nil {
logger.Error(context.Background(), "[closer][Close] error at close func call", logger.Err(err))
commonErr = errors.Join(commonErr, err)
}
}
return commonErr
}
func Add(fn func() error) {
globalCloser.Add(fn)
}
func Close() error {
return globalCloser.Close()
}