shutdownServices Handles the shutdown process of services for the current application. Iterates over all the started services in reverse order and tries to terminate them, returning an error if any error occurs.
(ctx context.Context)
| 114 | // Iterates over all the started services in reverse order and tries to terminate them, |
| 115 | // returning an error if any error occurs. |
| 116 | func (app *App) shutdownServices(ctx context.Context) error { |
| 117 | if app.state.ServicesLen() == 0 { |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | var errs []error |
| 122 | for key, srv := range app.state.Services() { |
| 123 | if srv == nil { |
| 124 | return fmt.Errorf("fiber: service %q is nil", key) |
| 125 | } |
| 126 | if err := ctx.Err(); err != nil { |
| 127 | // Context is canceled, do a best effort to terminate the services. |
| 128 | errs = append(errs, fmt.Errorf("service %s terminate: %w", srv.String(), err)) |
| 129 | continue |
| 130 | } |
| 131 | |
| 132 | err := srv.Terminate(ctx) |
| 133 | if err != nil { |
| 134 | // Best effort to terminate the services. |
| 135 | errs = append(errs, fmt.Errorf("service %s terminate: %w", srv.String(), err)) |
| 136 | continue |
| 137 | } |
| 138 | |
| 139 | // Remove the service from the State |
| 140 | app.state.deleteService(srv) |
| 141 | } |
| 142 | return errors.Join(errs...) |
| 143 | } |
| 144 | |
| 145 | // logServices logs information about services and returns an error |
| 146 | // if any configured service is nil. |