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

mendersoftware / deviceconnect / 1034538214

12 Oct 2023 11:23AM UTC coverage: 76.444% (-1.5%) from 77.923%
1034538214

push

gitlab-ci

web-flow
Merge pull request #319 from mendersoftware/master

Sync staging with master

12 of 21 new or added lines in 2 files covered. (57.14%)

1548 of 2025 relevant lines covered (76.44%)

35.12 hits per line

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

79.41
/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
        gracefulShutdownTimeout time.Duration
40
}
41

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

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

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

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

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

71
// Shutdown responds to GET /shutdown
72
func (h StatusController) Shutdown(c *gin.Context) {
1✔
73
        pid := os.Getpid()
1✔
74
        err := syscall.Kill(pid, syscall.SIGUSR1)
1✔
75
        if err != nil {
1✔
NEW
76
                ctx := c.Request.Context()
×
NEW
77
                log.FromContext(ctx).Error(errors.Wrap(err, "shutdown failed"))
×
NEW
78
                c.JSON(http.StatusServiceUnavailable, gin.H{
×
NEW
79
                        "error": err.Error(),
×
NEW
80
                })
×
NEW
81
                return
×
NEW
82
        }
×
83
        time.Sleep(h.gracefulShutdownTimeout)
1✔
84
        c.Writer.WriteHeader(http.StatusAccepted)
1✔
85
}
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