aboutsummaryrefslogtreecommitdiffstats
path: root/configd/src/apps/sentinel/service.h
blob: a5cdaabaab9f4327b15b0fb059c5adae3b011da1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "metrics.h"
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/util/time.h>
#include <vespa/config-sentinel.h>
#include <list>

using cloud::config::SentinelConfig;

namespace config::sentinel {

class OutputConnection;

class Service
{
private:
    Service(const Service &);
    Service& operator=(const Service &);

    pid_t _pid;
    enum ServiceState { READY, STARTING, RUNNING, TERMINATING, KILLING,
                        RESTARTING, REMOVING,
                        FINISHED, TERMINATED, KILLED, FAILED } _rawState;
    const enum ServiceState& _state;
    int _exitStatus;
    SentinelConfig::Service *_config;
    bool _isAutomatic;

    static constexpr vespalib::duration MAX_RESTART_PENALTY = 1800s;
    vespalib::duration _restartPenalty;
    vespalib::steady_time _last_start;

    [[noreturn]] void runChild();
    void setState(ServiceState state);
    void runCommand(const std::string & command);
    const char *stateName(ServiceState state) const;

    const SentinelConfig::Application _application;
    std::list<OutputConnection *> &_outputConnections;

    StartMetrics &_metrics;

public:
    using UP = std::unique_ptr<Service>;
    ~Service();
    Service(const SentinelConfig::Service& config,
            const SentinelConfig::Application& application,
			std::list<OutputConnection *> &ocs,
			StartMetrics &metrics);
    void reconfigure(const SentinelConfig::Service& config);
    int pid() const { return _pid; }
    void prepare_for_shutdown();
    int terminate(bool catchable, bool dumpState);
    int terminate() {
        return terminate(true, false);
    }
    void start();
    void remove();
    void youExited(int status); // Call this if waitpid says it exited
    const vespalib::string & name() const;
    const char *stateName() const { return stateName(_state); }
    bool isRunning() const;
    bool wantsRestart() const;
    int exitStatus() const { return _exitStatus; }
    const SentinelConfig::Service& serviceConfig() const { return *_config; }
    void setAutomatic(bool autoStatus);
    bool isAutomatic() const { return _isAutomatic; }
    void resetRestartPenalty() { _restartPenalty = vespalib::duration::zero(); }
    void incrementRestartPenalty();
};

}