github.com/minio/console@v1.4.1/api/admin_service.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/go-openapi/runtime/middleware"
    24  	"github.com/minio/console/api/operations"
    25  	"github.com/minio/console/models"
    26  
    27  	svcApi "github.com/minio/console/api/operations/service"
    28  )
    29  
    30  func registerServiceHandlers(api *operations.ConsoleAPI) {
    31  	// Restart Service
    32  	api.ServiceRestartServiceHandler = svcApi.RestartServiceHandlerFunc(func(params svcApi.RestartServiceParams, session *models.Principal) middleware.Responder {
    33  		if err := getRestartServiceResponse(session, params); err != nil {
    34  			return svcApi.NewRestartServiceDefault(err.Code).WithPayload(err.APIError)
    35  		}
    36  		return svcApi.NewRestartServiceNoContent()
    37  	})
    38  }
    39  
    40  // serviceRestart - restarts the MinIO cluster
    41  func serviceRestart(ctx context.Context, client MinioAdmin) error {
    42  	if err := client.serviceRestart(ctx); err != nil {
    43  		return err
    44  	}
    45  	// copy behavior from minio/mc mainAdminServiceRestart()
    46  	//
    47  	// Max. time taken by the server to shutdown is 5 seconds.
    48  	// This can happen when there are lot of s3 requests pending when the server
    49  	// receives a restart command.
    50  	// Sleep for 6 seconds and then check if the server is online.
    51  	time.Sleep(6 * time.Second)
    52  
    53  	// Fetch the service status of the specified MinIO server
    54  	_, err := client.serverInfo(ctx)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	return nil
    59  }
    60  
    61  // getRestartServiceResponse performs serviceRestart()
    62  func getRestartServiceResponse(session *models.Principal, params svcApi.RestartServiceParams) *CodedAPIError {
    63  	ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
    64  	defer cancel()
    65  	mAdmin, err := NewMinioAdminClient(params.HTTPRequest.Context(), session)
    66  	if err != nil {
    67  		return ErrorWithContext(ctx, err)
    68  	}
    69  	// create a MinIO Admin Client interface implementation
    70  	// defining the client to be used
    71  	adminClient := AdminClient{Client: mAdmin}
    72  
    73  	if err := serviceRestart(ctx, adminClient); err != nil {
    74  		return ErrorWithContext(ctx, err)
    75  	}
    76  	return nil
    77  }