aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/retriever/simpleconfigurer.h
blob: 95fa12610cb4adec6d8511e3715a0cc37e69ea58 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "simpleconfigretriever.h"
#include "configsnapshot.h"

#include <vespa/vespalib/util/thread.h>
#include <vespa/vespalib/util/runnable.h>

#include <atomic>

namespace config {

class SimpleConfigurable
{
public:
    virtual ~SimpleConfigurable() { }
    virtual void configure(const ConfigSnapshot & snapshot) = 0;
};

/**
 * A SimpleConfigurer runs in its own thread, uses a SimpleConfigRetriever to retrieve configs, and
 * performs a callback whenever a newsnapshot is ready.
 */
class SimpleConfigurer : public vespalib::Runnable
{
public:
    SimpleConfigurer(SimpleConfigRetriever::UP retriever, SimpleConfigurable * const configurable);
    ~SimpleConfigurer();

    /**
     * Start the configurer thread. configure() is guaranteed to be called
     * before this method returns.
     */
    void start();

    /**
     * Close the configurer. This will close the retriever as well!
     */
    void close();

    void run() override;

private:
    void runConfigure();

    SimpleConfigRetriever::UP _retriever;
    SimpleConfigurable * const _configurable;
    std::thread _thread;
    std::atomic<bool> _started;
};

} // namespace config