aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/vespa-wrapper/services/sentinel.go
blob: 1017924263b6132a605637d68fe8fc3bd4f1c61d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Author: arnej

package services

import (
	"fmt"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"time"

	"github.com/vespa-engine/vespa/client/go/internal/admin/envvars"
	"github.com/vespa-engine/vespa/client/go/internal/admin/trace"
	"github.com/vespa-engine/vespa/client/go/internal/util"
	"github.com/vespa-engine/vespa/client/go/internal/vespa"
)

const (
	SENTINEL_PIDFILE      = "var/run/sentinel.pid"
	SENTINEL_SERVICE_NAME = "config-sentinel"
)

func startSentinelWithRunserver() {
	_, veHost := commonPreChecks()
	vespa.CheckCorrectUser()
	os.Setenv(envvars.VESPA_SERVICE_NAME, SENTINEL_SERVICE_NAME)
	configId := fmt.Sprintf("hosts/%s", veHost)
	args := []string{
		"-r", "10",
		"-s", SENTINEL_SERVICE_NAME,
		"-p", SENTINEL_PIDFILE,
		"--",
		"sbin/vespa-config-sentinel",
		"-c", configId,
	}
	cmd := exec.Command("vespa-runserver", args...)
	cmd.Stdin = nil
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Start()
	p := cmd.Process
	if p != nil {
		p.Release()
	}
	os.Unsetenv(envvars.VESPA_SERVICE_NAME)
}

func waitForSentinelPid() bool {
	backtick := util.BackTicksWithStderr
	start := time.Now()
	for sleepcount := 0; sleepcount < 1000; sleepcount++ {
		time.Sleep(10 * time.Millisecond)
		got, err := os.ReadFile(CONFIGPROXY_PIDFILE)
		if err == nil {
			pid, err := strconv.Atoi(strings.TrimSpace(string(got)))
			if err == nil && pid > 0 {
				_, err := backtick.Run("kill", "-0", strconv.Itoa(pid))
				if err == nil {
					secs := int(time.Since(start).Seconds())
					if secs > 0 {
						fmt.Printf("config sentinel started after %d seconds\n", secs)
					}
					return true
				}
			}
		}
		if sleepcount%500 == 90 {
			trace.Warning("Waiting for sentinel to start")
		}
	}
	return false
}

func StartConfigSentinel() int {
	commonPreChecks()
	vespa.MaybeSwitchUser("start-config-sentinel")
	startSentinelWithRunserver()
	if waitForSentinelPid() {
		return 0
	}
	return 1
}

func stopSentinelWithRunserver() {
	_, err := util.SystemCommand.Run("vespa-runserver",
		"-s", SENTINEL_SERVICE_NAME,
		"-p", SENTINEL_PIDFILE, "-S")
	if err != nil {
		trace.Warning("Stopping sentinel:", err)
	}
}