Skip to content

Commit

Permalink
Add change event to service.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpe42 committed Sep 26, 2023
1 parent 97df14f commit 262900b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/gradle-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches:
- master
pull_request:
schedule:
- cron: '0 0 * * 1,4'

jobs:
check:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -102,6 +103,32 @@ CompletableFuture<PagerDutyEventResponse> triggerEvent(final PagerDutyEventPaylo
final LongUnaryOperator retryDelayFn,
final TimeUnit timeUnit);

CompletableFuture<PagerDutyEventResponse> changeEvent(final PagerDutyChangeEventPayload payload,
final long stepDelay,
final long maxDelay,
final TimeUnit timeUnit);

CompletableFuture<PagerDutyEventResponse> changeEvent(final PagerDutyChangeEventPayload payload,
final Duration giveUpAfter,
final long stepDelay,
final long maxDelay,
final TimeUnit timeUnit);

CompletableFuture<PagerDutyEventResponse> changeEvent(final PagerDutyChangeEventPayload payload,
final int maxRetries,
final long stepDelay,
final long maxDelay,
final TimeUnit timeUnit);

CompletableFuture<PagerDutyEventResponse> changeEvent(final PagerDutyChangeEventPayload payload,
final LongUnaryOperator retryDelayFn,
final TimeUnit timeUnit);

CompletableFuture<PagerDutyEventResponse> changeEvent(final PagerDutyChangeEventPayload payload,
final int retry,
final LongUnaryOperator retryDelayFn,
final TimeUnit timeUnit);

interface Builder {

PagerDutyService create();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<PagerDutyEventResponse> triggerEvent(final PagerDutyEventPayload payload, final long stepDelay, final long maxDelay, final TimeUnit timeUnit) {
public CompletableFuture<PagerDutyEventResponse> triggerEvent(final PagerDutyEventPayload payload,
final long stepDelay,
final long maxDelay,
final TimeUnit timeUnit) {
return triggerEvent(payload, 0, PagerDutyService.createRetryDelayFn(stepDelay, maxDelay), timeUnit);
}

Expand Down Expand Up @@ -184,6 +198,66 @@ private CompletableFuture<PagerDutyEventResponse> triggerEvent(final PagerDutyEv
}
}

@Override
public CompletableFuture<PagerDutyEventResponse> 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<PagerDutyEventResponse> 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<PagerDutyEventResponse> 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<PagerDutyEventResponse> changeEvent(final PagerDutyChangeEventPayload payload,
final LongUnaryOperator retryDelayFn,
final TimeUnit timeUnit) {
return changeEvent(payload, 0, retryDelayFn, timeUnit);
}

@Override
public CompletableFuture<PagerDutyEventResponse> 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<Throwable, CompletableFuture<PagerDutyEventResponse>> 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 + '}';
Expand Down

0 comments on commit 262900b

Please sign in to comment.