aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/helper/configpoller.cpp
blob: be0f00c1d9c6b202924212d89adee5f55de813b5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "configpoller.h"
#include <vespa/config/common/exceptions.h>
#include <vespa/config/subscription/configsubscriber.h>

#include <vespa/log/log.h>
LOG_SETUP(".config.helper.configpoller");

namespace config {

ConfigPoller::ConfigPoller(std::shared_ptr<IConfigContext> context)
    : _generation(-1),
      _subscriber(std::make_unique<ConfigSubscriber>(std::move(context))),
      _handleList(),
      _callbackList()
{
}

ConfigPoller::~ConfigPoller() = default;

void
ConfigPoller::run()
{
    try {
        while (!_subscriber->isClosed()) {
            poll();
        }
    } catch (config::InvalidConfigException & e) {
        LOG(fatal, "Got exception, will just exit quickly : %s", e.what());
        std::_Exit(17);
    }
}

void
ConfigPoller::poll()
{
    LOG(debug, "Checking for new config");
    if (_subscriber->nextGeneration()) {
        if (_subscriber->isClosed())
            return;
        LOG(debug, "Got new config, reconfiguring");
        _generation = _subscriber->getGeneration();
        for (size_t i = 0; i < _handleList.size(); i++) {
            ICallback * callback(_callbackList[i]);
            if (_handleList[i]->isChanged())
                callback->configure(_handleList[i]->getConfig());
        }
    } else {
        LOG(debug, "No new config available");
    }
}

void
ConfigPoller::close()
{
    _subscriber->close();
}

}