Skip to content

Commit

Permalink
add configuration to disable Scheduled
Browse files Browse the repository at this point in the history
see: micronaut-projects/micronaut-core#7568

it changes fixRate and initialDelay to use kebap-case

beforeAll / afterAll static

better explanation for CrypoUpdateTest

add query value to rfc6570 template
  • Loading branch information
sdelamo committed Jun 11, 2022
1 parent 9152475 commit 59e705e
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example.micronaut

import io.micronaut.core.util.Toggleable
import io.micronaut.context.annotation.ConfigurationProperties
import io.micronaut.core.bind.annotation.Bindable
import io.micronaut.core.util.StringUtils;

@ConfigurationProperties("app.scheduled") // <1>
interface ScheduledConfiguration extends Toggleable {

@Bindable(defaultValue = StringUtils.TRUE) // <2>
@Override
boolean isEnabled()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.micronaut.scheduling.annotation.Scheduled
import jakarta.inject.Singleton

import java.util.concurrent.atomic.AtomicInteger
import example.micronaut.ScheduledConfiguration;

@CompileStatic
@Slf4j
Expand All @@ -19,26 +20,31 @@ class CryptoService {
private final Counter checks
private final Timer time
private final AtomicInteger latestPriceUsd = new AtomicInteger(0)
private final ScheduledConfiguration scheduledConfiguration;

CryptoService(PriceClient priceClient, // <2>
MeterRegistry meterRegistry) {
MeterRegistry meterRegistry,
ScheduledConfiguration scheduledConfiguration) {
this.priceClient = priceClient

this.scheduledConfiguration = scheduledConfiguration;
checks = meterRegistry.counter('bitcoin.price.checks') // <3>
time = meterRegistry.timer('bitcoin.price.time') // <4>
meterRegistry.gauge('bitcoin.price.latest', latestPriceUsd) // <5>
}

@Scheduled(fixedRate = '${crypto.updateFrequency:1h}',
initialDelay = '${crypto.initialDelay:0s}') // <6>
@Scheduled(fixedRate = '${crypto.update-frequency:1h}',
initialDelay = '${crypto.initial-delay:0s}') // <6>
void updatePrice() {
time.record(() -> { // <7>
try {
checks.increment() // <8>
latestPriceUsd.set((int) priceClient.latestInUSD().price) // <9>
} catch (Exception e) {
log.error('Problem checking price', e)
}
})
if (scheduledConfiguration.enabled) {
time.record(() -> { // <7>
try {
checks.increment() // <8>
latestPriceUsd.set((int) priceClient.latestInUSD().price) // <9>
} catch (Exception e) {
log.error('Problem checking price', e)
}
})
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.micronaut.http.annotation.QueryValue
@Client(id = 'kucoin') // <1>
abstract class PriceClient {

@Get("/api/v1/market/orderbook/level1")
@Get("/api/v1/market/orderbook/level1{?symbol}")
abstract BitcoinPrice latest(@QueryValue String symbol)

BitcoinPrice latestInUSD() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

import io.micronaut.core.util.StringUtils;
import static java.util.concurrent.TimeUnit.MILLISECONDS

class CryptoUpdatesSpec extends Specification {

@Shared
@AutoCleanup
EmbeddedServer kucoinEmbeddedServer = ApplicationContext.run(EmbeddedServer.class,
["spec.name": "MetricsTestKucoin"])
["spec.name": "MetricsTestKucoin", // <1>
"app.scheduled.enabled": StringUtils.FALSE])

@Shared
@AutoCleanup
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class,
["micronaut.http.services.kocoin.url": "http://localhost:${kucoinEmbeddedServer.port}"])
["crypto.initial-delay": "10h", // <2>
"micronaut.http.services.kocoin.url": "http://localhost:${kucoinEmbeddedServer.port}", // <3>
"app.scheduled.enabled": StringUtils.TRUE])

void "test crypto updates"() {
given:
Expand All @@ -52,7 +55,7 @@ class CryptoUpdatesSpec extends Specification {
timer.totalTime(MILLISECONDS) > 0
}

@Requires(property = "spec.name", value = "MetricsTestKucoin")
@Requires(property = "spec.name", value = "MetricsTestKucoin") // <1>
@Controller
static class MockKucoinController {
@Get("/api/v1/market/orderbook/level1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import jakarta.inject.Inject
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import spock.lang.Specification

import static io.micronaut.logging.LogLevel.ALL
import static java.util.concurrent.TimeUnit.MILLISECONDS
import io.micronaut.core.util.StringUtils
import io.micronaut.context.annotation.Property

@MicronautTest // <1>
class MetricsSpec extends Specification {
Expand All @@ -28,9 +28,6 @@ class MetricsSpec extends Specification {
@Inject
LoggingSystem loggingSystem // <3>

@Inject
CryptoService cryptoService

@Inject
@Client('/')
HttpClient httpClient // <4>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example.micronaut;

import io.micronaut.core.util.Toggleable;
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.core.util.StringUtils;

@ConfigurationProperties("app.scheduled.enabled") // <1>
public interface ScheduledConfiguration extends Toggleable {

@Bindable(defaultValue = StringUtils.TRUE) // <2>
@Override
public boolean isEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import jakarta.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import example.micronaut.ScheduledConfiguration;
import java.util.concurrent.atomic.AtomicInteger;

@Singleton // <1>
Expand All @@ -19,26 +19,30 @@ public class CryptoService {
private final Counter checks;
private final Timer time;
private final AtomicInteger latestPriceUsd = new AtomicInteger(0);
private final ScheduledConfiguration scheduledConfiguration;

CryptoService(PriceClient priceClient, // <2>
MeterRegistry meterRegistry) {
MeterRegistry meterRegistry,
ScheduledConfiguration scheduledConfiguration) {
this.priceClient = priceClient;

this.scheduledConfiguration = scheduledConfiguration;
checks = meterRegistry.counter("bitcoin.price.checks"); // <3>
time = meterRegistry.timer("bitcoin.price.time"); // <4>
meterRegistry.gauge("bitcoin.price.latest", latestPriceUsd); // <5>
}

@Scheduled(fixedRate = "${crypto.updateFrequency:1h}",
initialDelay = "${crypto.initialDelay:0s}") // <6>
@Scheduled(fixedRate = "${crypto.update-frequency:1h}",
initialDelay = "${crypto.initial-delay:0s}") // <6>
public void updatePrice() {
time.record(() -> { // <7>
try {
checks.increment(); // <8>
latestPriceUsd.set((int) priceClient.latestInUSD().getPrice()); // <9>
} catch (Exception e) {
LOG.error("Problem checking price", e);
}
});
if (scheduledConfiguration.isEnabled()) {
time.record(() -> { // <7>
try {
checks.increment(); // <8>
latestPriceUsd.set((int) priceClient.latestInUSD().getPrice()); // <9>
} catch (Exception e) {
LOG.error("Problem checking price", e);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Client(id = "kucoin") // <1>
public abstract class PriceClient {

@Get("/api/v1/market/orderbook/level1")
@Get("/api/v1/market/orderbook/level1{?symbol}")
abstract BitcoinPrice latest(@QueryValue String symbol);

public BitcoinPrice latestInUSD() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,34 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import io.micronaut.core.util.CollectionUtils;
import java.util.Collections;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import io.micronaut.core.util.StringUtils;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CryptoUpdatesTest {

EmbeddedServer embeddedServer;
public static EmbeddedServer embeddedServer;

EmbeddedServer kucoinEmbeddedServer;
public static EmbeddedServer kucoinEmbeddedServer;

@BeforeAll
void beforeAll() {
public static void beforeAll() {
kucoinEmbeddedServer = ApplicationContext.run(EmbeddedServer.class,
Collections.singletonMap("spec.name", "MetricsTestKucoin"));
CollectionUtils.mapOf(
"spec.name", "MetricsTestKucoin")); // <1>
embeddedServer = ApplicationContext.run(EmbeddedServer.class,
Collections.singletonMap("micronaut.http.services.kucoin.url", "http://localhost:" + kucoinEmbeddedServer.getPort()));
CollectionUtils.mapOf(
"crypto.initial-delay", "10h", // <2>
"micronaut.http.services.kucoin.url", "http://localhost:" + kucoinEmbeddedServer.getPort(), // <3>
"app.scheduled.enabled", StringUtils.TRUE));
}

@AfterAll
void afterAll() {
public static void afterAll() {
embeddedServer.close();
kucoinEmbeddedServer.close();
}
Expand All @@ -63,7 +67,7 @@ void testCryptoUpdates() {
assertTrue(timer.totalTime(MILLISECONDS) > 0);
}

@Requires(property = "spec.name", value = "MetricsTestKucoin")
@Requires(property = "spec.name", value = "MetricsTestKucoin") // <1>
@Controller
static class MockKucoinController {
@Get("/api/v1/market/orderbook/level1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import io.micronaut.core.util.StringUtils;
import io.micronaut.context.annotation.Property;

@MicronautTest // <1>
class MetricsTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package example.micronaut

import io.micronaut.core.util.Toggleable
import io.micronaut.context.annotation.ConfigurationProperties
import io.micronaut.core.bind.annotation.Bindable
import io.micronaut.core.util.StringUtils

@ConfigurationProperties("app.scheduled") // <1>
interface ScheduledConfiguration : Toggleable {
@Bindable(defaultValue = StringUtils.TRUE) // <2>
override fun isEnabled(): Boolean
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package example.micronaut.crypto

import example.micronaut.ScheduledConfiguration
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Timer
Expand All @@ -11,6 +12,7 @@ import java.util.concurrent.atomic.AtomicInteger
@Singleton // <1>
class CryptoService constructor(
private val priceClient: PriceClient, // <2>
private val scheduledConfiguration: ScheduledConfiguration,
meterRegistry: MeterRegistry) {

private val LOG = LoggerFactory.getLogger(javaClass.name)
Expand All @@ -25,15 +27,17 @@ class CryptoService constructor(
meterRegistry.gauge("bitcoin.price.latest", latestPriceUsd) // <5>
}

@Scheduled(fixedRate = "\${crypto.updateFrequency:1h}",
initialDelay = "\${crypto.initialDelay:0s}") // <6>
@Scheduled(fixedRate = "\${crypto.update-frequency:1h}",
initialDelay = "\${crypto.initial-delay:0s}") // <6>
fun updatePrice() {
time.record { // <7>
try {
checks.increment() // <8>
latestPriceUsd.set(priceClient.latestInUSD().price.toInt()) // <9>
} catch (e: Exception) {
LOG.error("Problem checking price", e)
if (scheduledConfiguration.isEnabled()) {
time.record { // <7>
try {
checks.increment() // <8>
latestPriceUsd.set(priceClient.latestInUSD().price.toInt()) // <9>
} catch (e: Exception) {
LOG.error("Problem checking price", e)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.micronaut.http.client.annotation.Client

@Client(id = "kucoin") // <1>
abstract class PriceClient {
@Get("/api/v1/market/orderbook/level1")
@Get("/api/v1/market/orderbook/level1{?symbol}")
abstract fun latest(@QueryValue symbol: String): BitcoinPrice

fun latestInUSD(): BitcoinPrice {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.micronaut.runtime.server.EmbeddedServer
import org.junit.jupiter.api.*
import java.util.*
import java.util.concurrent.TimeUnit
import io.micronaut.core.util.StringUtils

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CryptoUpdatesTest {
Expand All @@ -19,9 +20,13 @@ class CryptoUpdatesTest {
@BeforeAll
fun beforeAll() {
kucoinEmbeddedServer = ApplicationContext.run(EmbeddedServer::class.java,
Collections.singletonMap<String, Any>("spec.name", "MetricsTestKucoin"))
mapOf(
"spec.name" to "MetricsTestKucoin")) // <1>
embeddedServer = ApplicationContext.run(EmbeddedServer::class.java,
Collections.singletonMap<String, Any>("micronaut.http.services.kucoin.url", "http://localhost:" + kucoinEmbeddedServer.getPort()))
mapOf(
"crypto.initial-delay" to "10h", // <2>
"micronaut.http.services.kucoin.url" to "http://localhost:" + kucoinEmbeddedServer.getPort(), // <3>
"app.scheduled.enabled" to StringUtils.TRUE))
}

@AfterAll
Expand Down Expand Up @@ -50,7 +55,7 @@ class CryptoUpdatesTest {
Assertions.assertTrue(timer.totalTime(TimeUnit.MILLISECONDS) > 0)
}

@Requires(property = "spec.name", value = "MetricsTestKucoin")
@Requires(property = "spec.name", value = "MetricsTestKucoin") // <1>
@Controller
internal class MockKucoinController {
@Get("/api/v1/market/orderbook/level1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import java.util.concurrent.TimeUnit.MILLISECONDS

import io.micronaut.core.util.StringUtils
import io.micronaut.context.annotation.Property

@MicronautTest // <1>
class MetricsTest {
Expand All @@ -27,9 +29,6 @@ class MetricsTest {
@Inject
lateinit var loggingSystem: LoggingSystem // <3>

@Inject
lateinit var cryptoService: CryptoService

@Inject
@field:Client("/")
lateinit var httpClient: HttpClient // <4>
Expand Down
Loading

0 comments on commit 59e705e

Please sign in to comment.