-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DefaultStreamMigrationPeriodical.java
138 lines (122 loc) · 5.06 KB
/
DefaultStreamMigrationPeriodical.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.periodical;
import com.google.common.collect.ImmutableMap;
import org.bson.types.ObjectId;
import org.graylog2.events.ClusterEventBus;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.database.ValidationException;
import org.graylog2.plugin.periodical.Periodical;
import org.graylog2.plugin.streams.Stream;
import org.graylog2.plugin.streams.StreamRule;
import org.graylog2.plugin.streams.StreamRuleType;
import org.graylog2.streams.StreamImpl;
import org.graylog2.streams.StreamRuleImpl;
import org.graylog2.streams.StreamRuleService;
import org.graylog2.streams.StreamService;
import org.graylog2.streams.config.DefaultStreamCreated;
import org.graylog2.streams.events.StreamsChangedEvent;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.util.Collections;
import java.util.Map;
/**
* Periodical creating the default stream if it doesn't exist.
*/
public class DefaultStreamMigrationPeriodical extends Periodical {
private static final Logger LOG = LoggerFactory.getLogger(DefaultStreamMigrationPeriodical.class);
private final StreamService streamService;
private final StreamRuleService streamRuleService;
private final ClusterEventBus clusterEventBus;
private final ClusterConfigService clusterConfigService;
@Inject
public DefaultStreamMigrationPeriodical(final StreamService streamService,
final StreamRuleService streamRuleService,
final ClusterEventBus clusterEventBus,
final ClusterConfigService clusterConfigService) {
this.streamService = streamService;
this.streamRuleService = streamRuleService;
this.clusterEventBus = clusterEventBus;
this.clusterConfigService = clusterConfigService;
}
@Override
public void doRun() {
final ObjectId id = new ObjectId(Stream.DEFAULT_STREAM_ID);
final Map<String, Object> fields = ImmutableMap.<String, Object>builder()
.put(StreamImpl.FIELD_TITLE, "All messages")
.put(StreamImpl.FIELD_DESCRIPTION, "Stream containing all messages")
.put(StreamImpl.FIELD_DISABLED, false)
.put(StreamImpl.FIELD_CREATED_AT, DateTime.now(DateTimeZone.UTC))
.put(StreamImpl.FIELD_CREATOR_USER_ID, "local:admin")
.put(StreamImpl.FIELD_MATCHING_TYPE, StreamImpl.MatchingType.DEFAULT.name())
.put(StreamImpl.FIELD_DEFAULT_STREAM, true)
.build();
final Stream stream = new StreamImpl(id, fields, Collections.emptyList(), Collections.emptySet(), Collections.emptySet());
final StreamRule streamRule = new StreamRuleImpl(
ImmutableMap.<String, Object>builder()
.put(StreamRuleImpl.FIELD_TYPE, StreamRuleType.ALWAYS_MATCH.getValue())
.put(StreamRuleImpl.FIELD_FIELD, "timestamp")
.put(StreamRuleImpl.FIELD_INVERTED, false)
.put(StreamRuleImpl.FIELD_STREAM_ID, id)
.put(StreamRuleImpl.FIELD_DESCRIPTION, "Match all messages")
.build());
try {
streamService.save(stream);
streamRuleService.save(streamRule);
LOG.info("Successfully created default stream: {}", stream.getTitle());
clusterConfigService.write(DefaultStreamCreated.create());
clusterEventBus.post(StreamsChangedEvent.create(stream.getId()));
} catch (ValidationException e) {
LOG.error("Couldn't create default stream", e);
}
}
@Override
public boolean runsForever() {
return true;
}
@Override
public boolean stopOnGracefulShutdown() {
return false;
}
@Override
public boolean masterOnly() {
return true;
}
@Override
public boolean startOnThisNode() {
return clusterConfigService.get(DefaultStreamCreated.class) == null;
}
@Override
public boolean isDaemon() {
return false;
}
@Override
public int getInitialDelaySeconds() {
return 0;
}
@Override
public int getPeriodSeconds() {
return 0;
}
@Override
protected Logger getLogger() {
return LOG;
}
}