Skip to content

Commit 9a4fb65

Browse files
conker84mneedham
authored andcommitted
fixes #200: Move the streamsTopicService into the initSinkModule method
1 parent 67d5815 commit 9a4fb65

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

common/src/main/kotlin/streams/utils/Neo4jUtils.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,16 @@ object Neo4jUtils {
8989
}
9090
}
9191

92+
fun waitForTheLeader(db: GraphDatabaseAPI, log: Log, timeout: Long = 120000, action: () -> Unit) {
93+
GlobalScope.launch(Dispatchers.IO) {
94+
val start = System.currentTimeMillis()
95+
val delay: Long = 2000
96+
while (!clusterHasLeader(db) && System.currentTimeMillis() - start < timeout) {
97+
log.info("$LEADER not found, new check comes in $delay milliseconds...")
98+
delay(delay)
99+
}
100+
action()
101+
}
102+
}
103+
92104
}

consumer/src/main/kotlin/streams/StreamsEventSinkExtensionFactory.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ class StreamsEventSinkExtensionFactory : KernelExtensionFactory<StreamsEventSink
4343
streamsLog.info("Initialising the Streams Sink module")
4444
val streamsSinkConfiguration = StreamsSinkConfiguration.from(configuration)
4545
val streamsTopicService = StreamsTopicService(db)
46-
streamsTopicService.clearAll()
47-
streamsTopicService.setAll(streamsSinkConfiguration.topics)
4846
val strategyMap = TopicUtils.toStrategyMap(streamsSinkConfiguration.topics,
4947
streamsSinkConfiguration.sourceIdStrategyConfig)
5048
val streamsQueryExecution = StreamsEventSinkQueryExecution(streamsTopicService, db,
@@ -62,10 +60,10 @@ class StreamsEventSinkExtensionFactory : KernelExtensionFactory<StreamsEventSink
6260
// start the Sink
6361
if (Neo4jUtils.isCluster(db)) {
6462
log.info("The Sink module is running in a cluster, checking for the ${Neo4jUtils.LEADER}")
65-
Neo4jUtils.executeInLeader(db, log) { initSinkModule() }
63+
Neo4jUtils.waitForTheLeader(db, log) { initSinkModule(streamsTopicService, streamsSinkConfiguration) }
6664
} else {
6765
// check if is writeable instance
68-
Neo4jUtils.executeInWriteableInstance(db) { initSinkModule() }
66+
Neo4jUtils.executeInWriteableInstance(db) { initSinkModule(streamsTopicService, streamsSinkConfiguration) }
6967
}
7068

7169
// Register required services for the Procedures
@@ -80,7 +78,9 @@ class StreamsEventSinkExtensionFactory : KernelExtensionFactory<StreamsEventSink
8078
}
8179
}
8280

83-
private fun initSinkModule() {
81+
private fun initSinkModule(streamsTopicService: StreamsTopicService, streamsSinkConfiguration: StreamsSinkConfiguration) {
82+
streamsTopicService.clearAll()
83+
streamsTopicService.setAll(streamsSinkConfiguration.topics)
8484
eventSink.start()
8585
streamsLog.info("Streams Sink module initialised")
8686
}

0 commit comments

Comments
 (0)