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

mendersoftware / mender-server / 1600238645

23 Dec 2024 10:30AM UTC coverage: 76.615% (+3.8%) from 72.818%
1600238645

Pull #274

gitlab-ci

bahaa-ghazal
fix: implement signal handler for `server` commands

Changelog: Title
Ticket: QA-782
Signed-off-by: Bahaa Aldeen Ghazal <bahaa.ghazal@northern.tech>
Pull Request #274: fix: implement signal handler for `server` commands

4254 of 6166 branches covered (68.99%)

Branch coverage included in aggregate %.

63 of 91 new or added lines in 8 files covered. (69.23%)

46 existing lines in 5 files now uncovered.

45257 of 58457 relevant lines covered (77.42%)

21.82 hits per line

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

89.09
/backend/services/iot-manager/server/server.go
1
// Copyright 2024 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 server
16

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

24
        "github.com/pkg/errors"
25
        "golang.org/x/sys/unix"
26

27
        "github.com/mendersoftware/mender-server/pkg/config"
28
        "github.com/mendersoftware/mender-server/pkg/log"
29

30
        api "github.com/mendersoftware/mender-server/services/iot-manager/api/http"
31
        "github.com/mendersoftware/mender-server/services/iot-manager/app"
32
        "github.com/mendersoftware/mender-server/services/iot-manager/client/devauth"
33
        "github.com/mendersoftware/mender-server/services/iot-manager/client/iotcore"
34
        "github.com/mendersoftware/mender-server/services/iot-manager/client/iothub"
35
        "github.com/mendersoftware/mender-server/services/iot-manager/client/workflows"
36
        dconfig "github.com/mendersoftware/mender-server/services/iot-manager/config"
37
        "github.com/mendersoftware/mender-server/services/iot-manager/store"
38
)
39

40
// InitAndRun initializes the server and runs it
41
func InitAndRun(conf config.Reader, dataStore store.DataStore) error {
2✔
42
        ctx := context.Background()
2✔
43
        httpClient := new(http.Client)
2✔
44
        wf := workflows.NewClient(
2✔
45
                conf.GetString(dconfig.SettingWorkflowsURL),
2✔
46
                workflows.NewOptions().SetClient(httpClient),
2✔
47
        )
2✔
48
        hub := iothub.NewClient(iothub.NewOptions().SetClient(httpClient))
2✔
49
        core := iotcore.NewClient()
2✔
50

2✔
51
        log.Setup(conf.GetBool(dconfig.SettingDebugLog))
2✔
52
        l := log.FromContext(ctx)
2✔
53

2✔
54
        da, err := devauth.NewClient(devauth.Config{
2✔
55
                Client:         httpClient,
2✔
56
                DevauthAddress: conf.GetString(dconfig.SettingDeviceauthURL),
2✔
57
        })
2✔
58
        if err != nil {
2✔
59
                return errors.Wrap(err, "failed to initialize devicauth client")
×
60
        }
×
61

62
        azureIotManagerApp := app.New(dataStore, wf, da).WithIoTHub(hub).WithIoTCore(core)
2✔
63
        azureIotManagerApp = azureIotManagerApp.
2✔
64
                WithWebhooksTimeout(config.Config.GetUint(dconfig.SettingWebhooksTimeoutSeconds))
2✔
65

2✔
66
        router := api.NewRouter(azureIotManagerApp,
2✔
67
                api.NewConfig().
2✔
68
                        SetClient(httpClient),
2✔
69
        )
2✔
70

2✔
71
        var listen = conf.GetString(dconfig.SettingListen)
2✔
72
        srv := &http.Server{
2✔
73
                Addr:    listen,
2✔
74
                Handler: router,
2✔
75
        }
2✔
76

2✔
77
        l.Info("IoT Manager service starting up")
2✔
78
        l.Infof("listening on %s", listen)
2✔
79

2✔
80
        go func() {
4✔
81
                if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
2✔
82
                        l.Fatalf("listen: %s\n", err)
×
83
                }
×
84
        }()
85

86
        quit := make(chan os.Signal, 1)
2✔
87
        signal.Notify(quit, unix.SIGINT, unix.SIGTERM)
2✔
88
        <-quit
2✔
89

2✔
90
        l.Info("server shutdown")
2✔
91

2✔
92
        ctxWithTimeout, cancel := context.WithTimeout(ctx, 5*time.Second)
2✔
93
        defer cancel()
2✔
94
        if err := srv.Shutdown(ctxWithTimeout); err != nil {
2✔
NEW
95
                l.Fatal("error when shutting down the server ", err)
×
NEW
96
        }
×
97

98
        l.Info("server exiting")
2✔
99
        return nil
2✔
100
}
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