Skip to content

Commit a3dbd21

Browse files
committed
fixes #200: Move the streamsTopicService into the initSinkModule method
1 parent 02783ac commit a3dbd21

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
@@ -45,8 +45,6 @@ class StreamsEventSinkExtensionFactory : KernelExtensionFactory<StreamsEventSink
4545
streamsLog.info("Initialising the Streams Sink module")
4646
val streamsSinkConfiguration = StreamsSinkConfiguration.from(configuration)
4747
val streamsTopicService = StreamsTopicService(db)
48-
streamsTopicService.clearAll()
49-
streamsTopicService.setAll(streamsSinkConfiguration.topics)
5048
val strategyMap = TopicUtils.toStrategyMap(streamsSinkConfiguration.topics,
5149
streamsSinkConfiguration.sourceIdStrategyConfig)
5250
val streamsQueryExecution = StreamsEventSinkQueryExecution(streamsTopicService, db,
@@ -64,10 +62,10 @@ class StreamsEventSinkExtensionFactory : KernelExtensionFactory<StreamsEventSink
6462
// start the Sink
6563
if (Neo4jUtils.isCluster(db)) {
6664
log.info("The Sink module is running in a cluster, checking for the ${Neo4jUtils.LEADER}")
67-
Neo4jUtils.executeInLeader(db, log) { initSinkModule() }
65+
Neo4jUtils.waitForTheLeader(db, log) { initSinkModule(streamsTopicService, streamsSinkConfiguration) }
6866
} else {
6967
// check if is writeable instance
70-
Neo4jUtils.executeInWriteableInstance(db) { initSinkModule() }
68+
Neo4jUtils.executeInWriteableInstance(db) { initSinkModule(streamsTopicService, streamsSinkConfiguration) }
7169
}
7270

7371
// Register required services for the Procedures
@@ -82,7 +80,9 @@ class StreamsEventSinkExtensionFactory : KernelExtensionFactory<StreamsEventSink
8280
}
8381
}
8482

85-
private fun initSinkModule() {
83+
private fun initSinkModule(streamsTopicService: StreamsTopicService, streamsSinkConfiguration: StreamsSinkConfiguration) {
84+
streamsTopicService.clearAll()
85+
streamsTopicService.setAll(streamsSinkConfiguration.topics)
8686
eventSink.start()
8787
streamsLog.info("Streams Sink module initialised")
8888
}

0 commit comments

Comments
 (0)