summaryrefslogtreecommitdiffstats
path: root/logd/src/apps/logd/main.cpp
blob: 62f1e48b233bf946c7c4d26954dee3b49ffbc17d (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
94
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <logd/errhandle.h>
#include <logd/forward.h>
#include <logd/conf.h>
#include <logd/watch.h>
#include <logd/state.h>
#include <logd/metrics.h>
#include <vespa/config/common/exceptions.h>
#include <csignal>
#include <unistd.h>
#include <vespa/vespalib/util/sig_catch.h>

#include <vespa/log/log.h>
LOG_SETUP("logdemon");

using namespace logdemon;
using config::FileSpec;

int main(int, char**)
{
    StateReporter stateReporter;
    Metrics metrics(stateReporter.metrics());
    Forwarder fwd(metrics);

    EV_STARTED("logdemon");

    vespalib::SigCatch catcher;

    const char *cfid = getenv("VESPA_CONFIG_ID");

    try {
        ConfSub subscriber(fwd, config::ConfigUri(cfid));

        int sleepcount = 0;
        while (true) {
            Watcher watcher(subscriber, fwd);

            try {
                subscriber.latch();
                stateReporter.setStatePort(subscriber.getStatePort());
                stateReporter.gotConf(subscriber.generation());
                int fd = subscriber.getservfd();
                if (fd >= 0) {
                    sleepcount = 0 ; // connection OK, reset sleep time
                    watcher.watchfile();
                } else {
                    LOG(spam, "bad fd in subscriber");
                }
            } catch (ConnectionException& ex) {
                LOG(debug, "connection exception: %s", ex.what());
                subscriber.closeConn();
            }
            if (catcher.receivedStopSignal()) {
                throw SigTermException("caught signal");
            }
            if (sleepcount < 60) {
                ++sleepcount;
            } else {
                sleepcount = 60;
            }
            LOG(debug, "sleep %d...", sleepcount);
            for (int i = 0; i < sleepcount; i++) {
                sleep(1);
                if (catcher.receivedStopSignal()) {
                    throw SigTermException("caught signal");
                }
            }
        }
    } catch (config::ConfigRuntimeException & ex) {
        LOG(error, "Configuration failed: %s", ex.what());
        EV_STOPPING("logdemon", "bad config");
        return 1;
    } catch (config::InvalidConfigException & ex) {
        LOG(error, "Configuration failed: %s", ex.what());
        EV_STOPPING("logdemon", "bad config");
        return 1;
    } catch (SigTermException& ex) {
        LOG(debug, "stopping on SIGTERM");
        EV_STOPPING("logdemon", "done ok.");
        return 0;
    } catch (MsgException& ex) {
        LOG(error, "stopping on error: %s", ex.what());
        EV_STOPPING("logdemon", "fatal error");
        return 1;
    } catch (...) {
        LOG(error, "unknown exception");
        EV_STOPPING("logdemon", "unknown error");
        return 1;
    }
    LOG(error, "connecting to logserver failed");
    EV_STOPPING("logdemon", "giving up after endless retries");
    return 1;
}