• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / deviceconnect / 1033161718

11 Oct 2023 11:17AM UTC coverage: 100.0% (+22.3%) from 77.704%
1033161718

Pull #317

gitlab-ci

tranchitella
feat: new internal API end-point to gracefully shutdown the service

Changelog: Ticket
Ticket: MC-7039

Signed-off-by: Fabio Tranchitella <fabio.tranchitella@northern.tech>
Pull Request #317: feat: new internal API end-point to gracefully shutdown the service

6 of 13 new or added lines in 2 files covered. (46.15%)

0 of 0 relevant lines covered (NaN%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.79
/api/http/status.go
1
// Copyright 2021 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package http
16

17
import (
18
        "context"
19
        "net/http"
20
        "os"
21
        "syscall"
22
        "time"
23

24
        "github.com/gin-gonic/gin"
25
        "github.com/pkg/errors"
26

27
        "github.com/mendersoftware/go-lib-micro/log"
28

29
        "github.com/mendersoftware/deviceconnect/app"
30
)
31

32
const (
33
        defaultTimeout = time.Second * 10
34
)
35

36
// StatusController contains status-related end-points
37
type StatusController struct {
38
        app app.App
39
}
40

41
// NewStatusController returns a new StatusController
42
func NewStatusController(app app.App) *StatusController {
94✔
43
        return &StatusController{app: app}
94✔
44
}
94✔
45

46
// Alive responds to GET /alive
47
func (h StatusController) Alive(c *gin.Context) {
1✔
48
        c.Writer.WriteHeader(http.StatusNoContent)
1✔
49
}
1✔
50

51
// Health responds to GET /health
52
func (h StatusController) Health(c *gin.Context) {
2✔
53
        ctx := c.Request.Context()
2✔
54
        l := log.FromContext(ctx)
2✔
55
        ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
2✔
56
        defer cancel()
2✔
57

2✔
58
        err := h.app.HealthCheck(ctx)
2✔
59
        if err != nil {
3✔
60
                l.Error(errors.Wrap(err, "health check failed"))
1✔
61
                c.JSON(http.StatusServiceUnavailable, gin.H{
1✔
62
                        "error": err.Error(),
1✔
63
                })
1✔
64
                return
1✔
65
        }
1✔
66

67
        c.Writer.WriteHeader(http.StatusNoContent)
1✔
68
}
69

70
// Shutdown responds to GET /shutdown
71
func (h StatusController) Shutdown(c *gin.Context) {
1✔
72
        pid := os.Getpid()
1✔
73
        err := syscall.Kill(pid, syscall.SIGUSR1)
1✔
74
        if err != nil {
1✔
NEW
75
                ctx := c.Request.Context()
×
NEW
76
                log.FromContext(ctx).Error(errors.Wrap(err, "shutdown failed"))
×
NEW
77
                c.JSON(http.StatusServiceUnavailable, gin.H{
×
NEW
78
                        "error": err.Error(),
×
NEW
79
                })
×
NEW
80
                return
×
NEW
81
        }
×
82
        c.Writer.WriteHeader(http.StatusAccepted)
1✔
83
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc