Shutdown gracefully stops background workers and the HTTP server. It ensures all resources are properly cleaned up and connections are closed. The shutdown is idempotent and can be called multiple times safely. Parameters: - ctx: The context for controlling the shutdown timeout Returns: - error: A
(ctx context.Context)
| 1786 | // Returns: |
| 1787 | // - error: An error if shutdown fails |
| 1788 | func (s *Service) Shutdown(ctx context.Context) error { |
| 1789 | if s == nil { |
| 1790 | return nil |
| 1791 | } |
| 1792 | var shutdownErr error |
| 1793 | s.shutdownOnce.Do(func() { |
| 1794 | if ctx == nil { |
| 1795 | ctx = context.Background() |
| 1796 | } |
| 1797 | |
| 1798 | if s.homeCancel != nil { |
| 1799 | s.homeCancel() |
| 1800 | s.homeCancel = nil |
| 1801 | } |
| 1802 | if s.homeClient != nil { |
| 1803 | s.homeClient.Close() |
| 1804 | s.homeClient = nil |
| 1805 | } |
| 1806 | if s.homeLogForwarder != nil { |
| 1807 | s.homeLogForwarder.Stop() |
| 1808 | s.homeLogForwarder = nil |
| 1809 | } |
| 1810 | home.ClearCurrent() |
| 1811 | |
| 1812 | // legacy refresh loop removed; only stopping core auth manager below |
| 1813 | |
| 1814 | if s.watcherCancel != nil { |
| 1815 | s.watcherCancel() |
| 1816 | } |
| 1817 | if s.coreManager != nil { |
| 1818 | s.coreManager.StopAutoRefresh() |
| 1819 | } |
| 1820 | if s.watcher != nil { |
| 1821 | if err := s.watcher.Stop(); err != nil { |
| 1822 | log.Errorf("failed to stop file watcher: %v", err) |
| 1823 | shutdownErr = err |
| 1824 | } |
| 1825 | } |
| 1826 | if s.wsGateway != nil { |
| 1827 | if err := s.wsGateway.Stop(ctx); err != nil { |
| 1828 | log.Errorf("failed to stop websocket gateway: %v", err) |
| 1829 | if shutdownErr == nil { |
| 1830 | shutdownErr = err |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | if s.authQueueStop != nil { |
| 1835 | s.authQueueStop() |
| 1836 | s.authQueueStop = nil |
| 1837 | } |
| 1838 | |
| 1839 | if errShutdownPprof := s.shutdownPprof(ctx); errShutdownPprof != nil { |
| 1840 | log.Errorf("failed to stop pprof server: %v", errShutdownPprof) |
| 1841 | if shutdownErr == nil { |
| 1842 | shutdownErr = errShutdownPprof |
| 1843 | } |
| 1844 | } |
| 1845 |
no test coverage detected