2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-06-15 23:20:38 -04:00
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-06-15 23:20:38 -04:00
|
|
|
|
|
|
|
import (
|
2018-03-15 16:27:16 -04:00
|
|
|
"context"
|
2020-09-12 02:02:32 -04:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
2016-06-15 23:20:38 -04:00
|
|
|
"os"
|
2018-11-12 18:07:16 -05:00
|
|
|
"strings"
|
2018-04-05 18:04:40 -04:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2016-06-15 23:20:38 -04:00
|
|
|
)
|
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
func handleSignals() {
|
|
|
|
// Custom exit function
|
2019-08-13 00:25:34 -04:00
|
|
|
exit := func(success bool) {
|
2017-07-12 19:33:21 -04:00
|
|
|
// If global profiler is set stop before we exit.
|
2020-01-10 20:19:58 -05:00
|
|
|
globalProfilerMu.Lock()
|
|
|
|
defer globalProfilerMu.Unlock()
|
2020-01-21 18:49:25 -05:00
|
|
|
for _, p := range globalProfiler {
|
|
|
|
p.Stop()
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
2016-06-15 23:20:38 -04:00
|
|
|
|
2019-08-13 00:25:34 -04:00
|
|
|
if success {
|
2017-07-12 19:33:21 -04:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2016-06-15 23:20:38 -04:00
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-06-15 23:20:38 -04:00
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
stopProcess := func() bool {
|
|
|
|
var err, oerr error
|
2016-06-15 23:20:38 -04:00
|
|
|
|
2020-09-08 12:10:55 -04:00
|
|
|
// send signal to various go-routines that they need to quit.
|
|
|
|
cancelGlobalContext()
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
if globalNotificationSys != nil {
|
|
|
|
globalNotificationSys.RemoveAllRemoteTargets()
|
|
|
|
}
|
|
|
|
|
2019-12-02 18:54:26 -05:00
|
|
|
if httpServer := newHTTPServerFn(); httpServer != nil {
|
|
|
|
err = httpServer.Shutdown()
|
2020-09-12 02:02:32 -04:00
|
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
}
|
2019-12-02 18:54:26 -05:00
|
|
|
}
|
2016-06-15 23:20:38 -04:00
|
|
|
|
2020-10-09 12:59:52 -04:00
|
|
|
if objAPI := newObjectLayerFn(); objAPI != nil {
|
2018-03-15 16:27:16 -04:00
|
|
|
oerr = objAPI.Shutdown(context.Background())
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), oerr)
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
2016-06-15 23:20:38 -04:00
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
return (err == nil && oerr == nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2020-09-08 12:10:55 -04:00
|
|
|
case <-globalHTTPServerErrorCh:
|
|
|
|
exit(stopProcess())
|
2017-07-12 19:33:21 -04:00
|
|
|
case osSignal := <-globalOSSignalCh:
|
2018-11-12 18:07:16 -05:00
|
|
|
logger.Info("Exiting on signal: %s", strings.ToUpper(osSignal.String()))
|
2017-07-12 19:33:21 -04:00
|
|
|
exit(stopProcess())
|
|
|
|
case signal := <-globalServiceSignalCh:
|
2019-08-28 18:04:43 -04:00
|
|
|
switch signal {
|
|
|
|
case serviceRestart:
|
2018-04-10 12:37:14 -04:00
|
|
|
logger.Info("Restarting on service signal")
|
2018-05-31 15:30:15 -04:00
|
|
|
stop := stopProcess()
|
2017-07-12 19:33:21 -04:00
|
|
|
rerr := restartProcess()
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), rerr)
|
2018-05-31 15:30:15 -04:00
|
|
|
exit(stop && rerr == nil)
|
2019-08-28 18:04:43 -04:00
|
|
|
case serviceStop:
|
2018-04-10 12:37:14 -04:00
|
|
|
logger.Info("Stopping on service signal")
|
2017-07-12 19:33:21 -04:00
|
|
|
exit(stopProcess())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 23:20:38 -04:00
|
|
|
}
|