Skip to content

Commit

Permalink
feat(client-java): Add support to backoff failed jobs into the Java c…
Browse files Browse the repository at this point in the history
…lient
  • Loading branch information
aivinog1 committed May 14, 2022
1 parent 2c52c6a commit 78f67ef
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.camunda.zeebe.client.api.command;

import io.camunda.zeebe.client.api.response.FailJobResponse;
import java.time.Duration;

public interface FailJobCommandStep1 {

Expand All @@ -34,6 +35,18 @@ public interface FailJobCommandStep1 {
interface FailJobCommandStep2 extends FinalCommandStep<FailJobResponse> {
// the place for new optional parameters

/**
* Set the backoff timeout for failing this job.
*
* <p>If the backoff timeout is greater than zero and retries are greater than zero then after
* the job this job will be picked up again after this backoff timeout will pass.
*
* @param backoffTimeout the backoff timeout of this job
* @return the builder for this command. Call {@link #send()} to complete the command and send *
* it to the broker.
*/
FailJobCommandStep2 retryBackoff(final Duration backoffTimeout);

/**
* Provide an error message describing the reason for the job failure. If failing the job
* creates an incident, this error message will be used as incident message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public FailJobCommandStep2 retries(final int retries) {
return this;
}

@Override
public FailJobCommandStep2 retryBackoff(final Duration backoffTimeout) {
builder.setRetryBackOff(backoffTimeout.toMillis());
return this;
}

@Override
public FailJobCommandStep2 errorMessage(final String errorMsg) {
builder.setErrorMessage(errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,52 @@ public void shouldFailJobWithMessage() {
rule.verifyDefaultRequestTimeout();
}

@Test
public void shouldFailJobWithBackoff() {
// given
final long jobKey = 12;
final int newRetries = 23;

// when
final Duration backoffTimeout = Duration.ofSeconds(1);
client.newFailCommand(jobKey).retries(newRetries).retryBackoff(backoffTimeout).send().join();

// then
final FailJobRequest request = gatewayService.getLastRequest();
assertThat(request.getJobKey()).isEqualTo(jobKey);
assertThat(request.getRetries()).isEqualTo(newRetries);
assertThat(request.getRetryBackOff()).isEqualTo(backoffTimeout.toMillis());

rule.verifyDefaultRequestTimeout();
}

@Test
public void shouldFailJobWithBackoffAndMessage() {
// given
final long jobKey = 12;
final int newRetries = 23;
final String message = "failed message";

// when
final Duration backoffTimeout = Duration.ofSeconds(1);
client
.newFailCommand(jobKey)
.retries(newRetries)
.retryBackoff(backoffTimeout)
.errorMessage(message)
.send()
.join();

// then
final FailJobRequest request = gatewayService.getLastRequest();
assertThat(request.getJobKey()).isEqualTo(jobKey);
assertThat(request.getRetries()).isEqualTo(newRetries);
assertThat(request.getRetryBackOff()).isEqualTo(backoffTimeout.toMillis());
assertThat(request.getErrorMessage()).isEqualTo(message);

rule.verifyDefaultRequestTimeout();
}

@Test
public void shouldSetRequestTimeout() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.camunda.zeebe.protocol.record.intent.JobIntent;
import io.camunda.zeebe.protocol.record.value.JobRecordValue;
import io.camunda.zeebe.test.util.BrokerClassRuleHelper;
import java.time.Duration;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
Expand Down Expand Up @@ -73,6 +74,26 @@ public void shouldFailJobWithErrorMessage() {
Assertions.assertThat(record.getValue()).hasRetries(0).hasErrorMessage("test");
}

@Test
public void shouldFailJobWithRetryBackOff() {
// when
final Duration backoffTimeout = Duration.ofSeconds(30);
CLIENT_RULE
.getClient()
.newFailCommand(jobKey)
.retries(1)
.retryBackoff(backoffTimeout)
.send()
.join();

// then
final Record<JobRecordValue> beforeRecurRecord =
jobRecords(JobIntent.FAILED).withRecordKey(jobKey).getFirst();
Assertions.assertThat(beforeRecurRecord.getValue())
.hasRetries(1)
.hasRetryBackoff(backoffTimeout.toMillis());
}

@Test
public void shouldRejectIfJobIsAlreadyCompleted() {
// given
Expand Down

0 comments on commit 78f67ef

Please sign in to comment.