Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fail a job #446

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 2 additions & 51 deletions src/main/java/io/zeebe/monitor/rest/JobResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,16 @@
package io.zeebe.monitor.rest;

import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.client.api.response.ActivatedJob;
import io.zeebe.monitor.entity.JobEntity;
import io.zeebe.monitor.repository.JobRepository;
import io.zeebe.monitor.rest.dto.ThrowErrorDto;
import java.time.Duration;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/jobs")
public class JobResource {

private static final String WORKER_NAME = "zeebe-simple-monitor";

@Autowired private ZeebeClient zeebeClient;

@Autowired private JobRepository jobRepository;

@RequestMapping(path = "/{key}/complete", method = RequestMethod.PUT)
public void completeJob(
@PathVariable("key") final long key, @RequestBody final String variables) {
Expand All @@ -49,9 +36,8 @@ public void completeJob(
@RequestMapping(path = "/{key}/fail", method = RequestMethod.PUT)
public void failJob(@PathVariable("key") final long key) {

final ActivatedJob activatedJob = activateJob(key, zeebeClient);
zeebeClient
.newFailCommand(activatedJob.getKey())
.newFailCommand(key)
.retries(0)
.errorMessage("Failed by user.")
.send()
Expand All @@ -65,39 +51,4 @@ public void throwError(
zeebeClient.newThrowErrorCommand(key).errorCode(dto.getErrorCode()).send().join();
}

private ActivatedJob activateJob(final long key, final ZeebeClient client) {
final JobEntity job = getJob(key);
final String jobType = job.getJobType();

return activateJob(client, key, jobType);
}

private JobEntity getJob(final long key) {
return jobRepository
.findByKey(key)
.orElseThrow(() -> new RuntimeException("no job found with key: " + key));
}

private ActivatedJob activateJob(final ZeebeClient client, final long key, final String jobType) {

final List<ActivatedJob> jobs =
client
.newActivateJobsCommand()
.jobType(jobType)
.maxJobsToActivate(10)
.timeout(Duration.ofSeconds(10))
.workerName(WORKER_NAME)
.send()
.join()
.getJobs();

if (jobs.isEmpty()) {
throw new RuntimeException("no activatable job found with key: " + key);
} else {
return jobs.stream()
.filter(activatedJob -> activatedJob.getKey() == key)
.findFirst()
.orElseGet(() -> activateJob(client, key, jobType));
}
}
}