Skip to content

Commit

Permalink
Adding service 'handle-scoreboard-service'
Browse files Browse the repository at this point in the history
  • Loading branch information
Creek-Bot committed Mar 6, 2023
1 parent a6ce0b6 commit 539b91e
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ updates:
directory: /handle-occurrence-service
open-pull-requests-limit: 50
schedule:
interval: monthly
interval: monthly
- package-ecosystem: docker
directory: /handle-scoreboard-service
schedule:
interval: monthly
21 changes: 21 additions & 0 deletions handle-scoreboard-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM amazoncorretto:19@sha256:a197d796640268bd6fcb74507f92efe69274b2c13de63ee36a07c25058d4bd3f
ARG APP_NAME
ARG APP_VERSION
ENV VERSION=$APP_VERSION

LABEL org.opencontainers.image.source=https://github.com/creek-service/connected-services-demo/tree/main/handle-scoreboard-service

RUN yum update -y
RUN yum install -y tar lsof

RUN mkdir -p /opt/creek

COPY bin /bin
COPY log4j /log

COPY ${APP_NAME}-${APP_VERSION}.tar /opt/creek
WORKDIR /opt/creek
RUN tar xf ${APP_NAME}-${APP_VERSION}.tar
RUN ln -s ${APP_NAME}-${APP_VERSION} service

ENTRYPOINT ["/bin/run.sh"]
72 changes: 72 additions & 0 deletions handle-scoreboard-service/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2023 Creek Contributors (https://github.com/creek-service)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.bmuschko.gradle.docker.tasks.image.DockerPushImage

plugins {
application
id("com.bmuschko.docker-remote-api")
}

val creekVersion : String by extra
val kafkaVersion : String by extra
val log4jVersion : String by extra

dependencies {
implementation(project(":services"))
implementation("org.creekservice:creek-service-context:$creekVersion")
implementation("org.creekservice:creek-kafka-streams-extension:$creekVersion")
implementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
runtimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl:$log4jVersion")

testImplementation("org.creekservice:creek-kafka-streams-test:$creekVersion")
}

// Patch Kafka Streams test jar into main Kafka Streams module to avoid split packages:
modularity.patchModule("kafka.streams", "kafka-streams-test-utils-$kafkaVersion.jar")

application {
mainModule.set("connected.services.demo.handle.scoreboard.service")
mainClass.set("io.github.creek.service.connected.services.demo.handle.scoreboard.service.ServiceMain")
}

val buildAppImage = tasks.register<DockerBuildImage>("buildAppImage") {
dependsOn("prepareDocker")
buildArgs.put("APP_NAME", project.name)
buildArgs.put("APP_VERSION", "${project.version}")
images.add("ghcr.io/creek-service/${rootProject.name}-${project.name}:latest")
images.add("ghcr.io/creek-service/${rootProject.name}-${project.name}:${project.version}")
}

tasks.register<Copy>("prepareDocker") {
dependsOn("distTar")

from(
layout.projectDirectory.file("Dockerfile"),
layout.buildDirectory.file("distributions/${project.name}-${project.version}.tar"),
layout.projectDirectory.dir("include"),
)

into(buildAppImage.get().inputDir)
}

tasks.register<DockerPushImage>("pushAppImage") {
dependsOn("buildAppImage")
images.add("ghcr.io/creek-service/${rootProject.name}-${project.name}:latest")
images.add("ghcr.io/creek-service/${rootProject.name}-${project.name}:${project.version}")
}

23 changes: 23 additions & 0 deletions handle-scoreboard-service/include/bin/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

#
# Copyright 2022-2023 Creek Contributors (https://github.com/creek-service)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

exec java \
-Xms64m -Xmx256m \
-Dlog4j.configurationFile=/log/log4j2.xml \
--module-path "/opt/creek/service/lib" \
--module connected.services.demo.handle.scoreboard.service/io.github.creek.service.connected.services.demo.handle.scoreboard.service.ServiceMain
38 changes: 38 additions & 0 deletions handle-scoreboard-service/include/log4j/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2022-2023 Creek Contributors (https://github.com/creek-service)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<Configuration status="WARN">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.creekservice" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Logger name="kafka" level="info" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Logger name="org.apache.kafka" level="info" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2021-2023 Creek Contributors (https://github.com/creek-service)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.creek.service.connected.services.demo.handle.scoreboard.service;

import io.github.creek.service.connected.services.demo.handle.scoreboard.service.kafka.streams.TopologyBuilder;
import io.github.creek.service.connected.services.demo.services.HandleScoreboardServiceDescriptor;
import org.apache.kafka.streams.Topology;
import org.creekservice.api.kafka.streams.extension.KafkaStreamsExtension;
import org.creekservice.api.kafka.streams.extension.KafkaStreamsExtensionOptions;
import org.creekservice.api.service.context.CreekContext;
import org.creekservice.api.service.context.CreekServices;

/** Entry point of the service */
public final class ServiceMain {

private ServiceMain() {}

public static void main(final String... args) {
final CreekContext ctx =
CreekServices.builder(new HandleScoreboardServiceDescriptor())
.with(
KafkaStreamsExtensionOptions.builder()
// Customize the Kafka streams extension here...
.build())
.build();

final KafkaStreamsExtension ext = ctx.extension(KafkaStreamsExtension.class);
final Topology topology = new TopologyBuilder(ext).build();
ext.execute(topology);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022-2023 Creek Contributors (https://github.com/creek-service)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.creek.service.connected.services.demo.handle.scoreboard.service.kafka.streams;

import static java.util.Objects.requireNonNull;
import static org.creekservice.api.kafka.metadata.KafkaTopicDescriptor.DEFAULT_CLUSTER_NAME;

import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.Topology;
import org.creekservice.api.kafka.streams.extension.KafkaStreamsExtension;
import org.creekservice.api.kafka.streams.extension.util.Name;

public final class TopologyBuilder {

private final KafkaStreamsExtension ext;
private final Name name = Name.root();

public TopologyBuilder(final KafkaStreamsExtension ext) {
this.ext = requireNonNull(ext, "ext");
}

public Topology build() {
final StreamsBuilder builder = new StreamsBuilder();

return builder.build(ext.properties(DEFAULT_CLUSTER_NAME));
}
}
6 changes: 6 additions & 0 deletions handle-scoreboard-service/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module connected.services.demo.handle.scoreboard.service {
requires connected.services.demo.services;
requires creek.service.context;
requires creek.kafka.streams.extension;
requires org.apache.logging.log4j;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2022-2023 Creek Contributors (https://github.com/creek-service)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.creek.service.connected.services.demo.example.streams;

import static org.creekservice.api.kafka.metadata.KafkaTopicDescriptor.DEFAULT_CLUSTER_NAME;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.creek.service.connected.services.demo.handle.scoreboard.service.kafka.streams.TopologyBuilder;
import io.github.creek.service.connected.services.demo.services.HandleScoreboardServiceDescriptor;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.TopologyTestDriver;
import org.creekservice.api.kafka.streams.extension.KafkaStreamsExtension;
import org.creekservice.api.kafka.streams.test.TestKafkaStreamsExtensionOptions;
import org.creekservice.api.service.context.CreekContext;
import org.creekservice.api.service.context.CreekServices;
import org.creekservice.api.test.util.TestPaths;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class TopologyBuilderTest {

private static CreekContext ctx;

private TopologyTestDriver testDriver;
private Topology topology;

@BeforeAll
public static void classSetup() {
ctx =
CreekServices.builder(new HandleScoreboardServiceDescriptor())
.with(TestKafkaStreamsExtensionOptions.defaults())
.build();
}

@BeforeEach
public void setUp() {
final KafkaStreamsExtension ext = ctx.extension(KafkaStreamsExtension.class);

topology = new TopologyBuilder(ext).build();
testDriver = new TopologyTestDriver(topology, ext.properties(DEFAULT_CLUSTER_NAME));
}

@AfterEach
public void tearDown() {
testDriver.close();
}

/**
* A test that intentionally fails when ever the topology changes.
*
* <p>This is to make it less likely that unintentional changes to the topology are committed
* and that thought is given to any intentional changes to ensure they won't break any deployed
* instances.
*
* <p>Care must be taken when changing a deployed topology to ensure either:
*
* <ol>
* <li>Changes are backwards compatible and won't leave data stranded in unused topics, or
* <li>The existing topology is drained before the new topology is deployed
* </ol>
*
* <p>Option #1 allows for the simplest deployment, but is not always possible or desirable.
*/
@Test
void shouldNotChangeTheTopologyUnintentionally() {
// Given:
final String expectedTopology =
TestPaths.readString(
TestPaths.moduleRoot("handle-scoreboard-service")
.resolve("src/test/resources/kafka/streams/expected_topology.txt"));

// When:
final String currentTopology = topology.describe().toString();

// Then:
assertThat(currentTopology.trim(), is(expectedTopology.trim()));
}
}
8 changes: 8 additions & 0 deletions handle-scoreboard-service/src/test/java/module-info.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--add-modules
org.junitpioneer,org.hamcrest,guava.testlib,creek.test.util,creek.test.hamcrest,creek.kafka.streams.test

--add-reads
connected.services.demo.handle.scoreboard.service=org.junitpioneer,org.hamcrest,guava.testlib,creek.test.util,creek.test.hamcrest,creek.kafka.streams.test

--add-opens
org.junitpioneer/org.junitpioneer.jupiter=org.junit.platform.commons
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Topologies:
Loading

0 comments on commit 539b91e

Please sign in to comment.