From 262900ba77925c0a431ad49c5950762f8f15c251 Mon Sep 17 00:00:00 2001 From: jim Date: Tue, 26 Sep 2023 08:12:51 -0500 Subject: [PATCH] Add change event to service. --- .github/workflows/gradle-check.yml | 2 - build.gradle | 2 +- .../event/service/PagerDutyService.java | 27 +++++++ .../event/service/PagerDutyServiceVal.java | 76 ++++++++++++++++++- 4 files changed, 103 insertions(+), 4 deletions(-) diff --git a/.github/workflows/gradle-check.yml b/.github/workflows/gradle-check.yml index 8a072ec..d0e79f0 100644 --- a/.github/workflows/gradle-check.yml +++ b/.github/workflows/gradle-check.yml @@ -5,8 +5,6 @@ on: branches: - master pull_request: - schedule: - - cron: '0 0 * * 1,4' jobs: check: diff --git a/build.gradle b/build.gradle index e008c17..375232a 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ buildscript { VERSION = getAppVersion() VCS_URL = 'https://github.com/comodal/pagerduty-client' // Used by systems.comodal.pagerduty_event_json_iterator_adapter - jsoniter = "2.9.+" // https://github.com/comodal/json-iterator/releases + jsoniter = "2.10.+" // https://github.com/comodal/json-iterator/releases } } diff --git a/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/service/PagerDutyService.java b/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/service/PagerDutyService.java index e5fc641..e9deb45 100644 --- a/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/service/PagerDutyService.java +++ b/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/service/PagerDutyService.java @@ -1,6 +1,7 @@ package systems.comodal.pagerduty.event.service; import systems.comodal.pagerduty.event.client.PagerDutyEventClient; +import systems.comodal.pagerduty.event.data.PagerDutyChangeEventPayload; import systems.comodal.pagerduty.event.data.PagerDutyEventPayload; import systems.comodal.pagerduty.event.data.PagerDutyEventResponse; @@ -102,6 +103,32 @@ CompletableFuture triggerEvent(final PagerDutyEventPaylo final LongUnaryOperator retryDelayFn, final TimeUnit timeUnit); + CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final long stepDelay, + final long maxDelay, + final TimeUnit timeUnit); + + CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final Duration giveUpAfter, + final long stepDelay, + final long maxDelay, + final TimeUnit timeUnit); + + CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final int maxRetries, + final long stepDelay, + final long maxDelay, + final TimeUnit timeUnit); + + CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final LongUnaryOperator retryDelayFn, + final TimeUnit timeUnit); + + CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final int retry, + final LongUnaryOperator retryDelayFn, + final TimeUnit timeUnit); + interface Builder { PagerDutyService create(); diff --git a/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/service/PagerDutyServiceVal.java b/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/service/PagerDutyServiceVal.java index 48a5811..6ff2bd9 100644 --- a/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/service/PagerDutyServiceVal.java +++ b/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/service/PagerDutyServiceVal.java @@ -1,6 +1,7 @@ package systems.comodal.pagerduty.event.service; import systems.comodal.pagerduty.event.client.PagerDutyEventClient; +import systems.comodal.pagerduty.event.data.PagerDutyChangeEventPayload; import systems.comodal.pagerduty.event.data.PagerDutyEventPayload; import systems.comodal.pagerduty.event.data.PagerDutyEventResponse; import systems.comodal.pagerduty.exceptions.PagerDutyClientException; @@ -124,8 +125,21 @@ private static boolean canBeRetried(final Throwable throwable) { }; } + private static RuntimeException throwRuntimeException(final Throwable throwable) { + if (throwable instanceof final RuntimeException runtimeException) { + throw runtimeException; + } else if (throwable.getCause() instanceof final RuntimeException runtimeException) { + throw runtimeException; + } else { + throw new RuntimeException(throwable.getCause() == null ? throwable : throwable.getCause()); + } + } + @Override - public CompletableFuture triggerEvent(final PagerDutyEventPayload payload, final long stepDelay, final long maxDelay, final TimeUnit timeUnit) { + public CompletableFuture triggerEvent(final PagerDutyEventPayload payload, + final long stepDelay, + final long maxDelay, + final TimeUnit timeUnit) { return triggerEvent(payload, 0, PagerDutyService.createRetryDelayFn(stepDelay, maxDelay), timeUnit); } @@ -184,6 +198,66 @@ private CompletableFuture triggerEvent(final PagerDutyEv } } + @Override + public CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final long stepDelay, + final long maxDelay, + final TimeUnit timeUnit) { + return changeEvent(payload, 0, PagerDutyService.createRetryDelayFn(stepDelay, maxDelay), timeUnit); + } + + @Override + public CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final Duration giveUpAfter, + final long stepDelay, + final long maxDelay, + final TimeUnit timeUnit) { + final int maxRetries = (int) Math.min(Integer.MAX_VALUE, giveUpAfter.toMillis() / timeUnit.toMillis(stepDelay)); + return changeEvent(payload, 0, PagerDutyService.createRetryDelayFn(maxRetries, stepDelay, maxDelay), timeUnit); + } + + @Override + public CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final int maxRetries, + final long stepDelay, + final long maxDelay, + final TimeUnit timeUnit) { + return changeEvent(payload, 0, PagerDutyService.createRetryDelayFn(maxRetries, stepDelay, maxDelay), timeUnit); + } + + @Override + public CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final LongUnaryOperator retryDelayFn, + final TimeUnit timeUnit) { + return changeEvent(payload, 0, retryDelayFn, timeUnit); + } + + @Override + public CompletableFuture changeEvent(final PagerDutyChangeEventPayload payload, + final int retry, + final LongUnaryOperator retryDelayFn, + final TimeUnit timeUnit) { + final long retryDelay = retryDelayFn.applyAsLong(retry); + if (retryDelay < 0) { + return null; + } + final var responseFuture = client.defaultRouteChangeEvent(payload); + final Function> exceptionally = throwable -> { + final int numFailures = retry + 1; + logFailure(throwable, numFailures, retryDelay, timeUnit, String.format("to send change event:%n %s", payload)); + if (canBeRetried(throwable)) { + return changeEvent(payload, numFailures, retryDelayFn, timeUnit); + } else { + throw throwRuntimeException(throwable); + } + }; + if (retryDelay > 0) { + return responseFuture.exceptionallyComposeAsync(exceptionally, delayedExecutor(retryDelay, timeUnit)); + } else { + return responseFuture.exceptionallyCompose(exceptionally); + } + } + @Override public String toString() { return "PagerdutyServiceVal{client=" + client + ", eventPrototype=" + eventPrototype + '}';