aboutsummaryrefslogtreecommitdiffstats
path: root/configd/src/apps/sentinel/cmdq.h
blob: 27bdf2ce195afb46d618a3bd7a1f0e572bbfdcb0 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <memory>
#include <mutex>
#include <vector>

class FRT_RPCRequest;

namespace config::sentinel {

class Cmd {
public:
    using UP = std::unique_ptr<Cmd>;
    enum CmdType { LIST, RESTART, START, STOP };

    Cmd(FRT_RPCRequest *req, CmdType cmdType, const char *service = "")
      : _req(req), _cmdType(cmdType), _serviceName(service)
    {}

    CmdType type() const { return _cmdType; }
    const char *serviceName() const { return _serviceName; }

    void retError(const char *errorString) const;
    void retValue(const char *valueString) const;

    ~Cmd();
private:
    FRT_RPCRequest *_req;
    CmdType _cmdType;
    const char *_serviceName;
};

class CommandQueue
{
private:
    std::mutex _lock;
    std::vector<Cmd::UP> _queue;
public:
    CommandQueue() = default;
    ~CommandQueue() = default;

    void enqueue(Cmd::UP cmd) {
        std::lock_guard guard(_lock);
        _queue.push_back(std::move(cmd));
    }

    std::vector<Cmd::UP> drain() {
        std::vector<Cmd::UP> r;
        std::lock_guard guard(_lock);
        r.swap(_queue);
        return r;
    }

};

} // namespace config::sentinel