Skip to content

Commit

Permalink
feature: update workflow instance state event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqi committed Mar 30, 2024
1 parent 7353b33 commit a15ff2f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void run() {
workflowTaskInstanceService.updateTaskId(event.getWorkflowTaskInstanceId(), taskId);
workflowTaskInstanceService.updateState(event.getWorkflowTaskInstanceId(), event.getState(), event.getNextState(), null);
if (RandomUtils.nextInt(0, 100) > 80) {
throw new RuntimeException("部署失败");
throw new RuntimeException("任务执行失败");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,25 @@

package cn.sliew.scaleph.workflow.listener.taskinstance;

import cn.sliew.scaleph.workflow.service.WorkflowTaskInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.annotation.RInject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Component
public class WorkflowTaskInstanceFailureEventListener extends AbstractWorkflowTaskInstanceEventListener {

@Override
protected CompletableFuture handleEventAsync(WorkflowTaskInstanceEventDTO event) {
return executorService.submit(new FailureRunner(event.getWorkflowTaskInstanceId(), event.getThrowable())).toCompletableFuture();
return CompletableFuture.runAsync(new FailureRunner(event.getWorkflowTaskInstanceId(), event.getThrowable())).toCompletableFuture();
}

public static class FailureRunner implements Runnable, Serializable {
private class FailureRunner implements Runnable, Serializable {

private Long workflowTaskInstanceId;
private Optional<Throwable> throwable;

@RInject
private String taskId;
@Autowired
private WorkflowTaskInstanceService workflowTaskInstanceService;

public FailureRunner(Long workflowTaskInstanceId, Optional<Throwable> throwable) {
this.workflowTaskInstanceId = workflowTaskInstanceId;
this.throwable = throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,23 @@

package cn.sliew.scaleph.workflow.listener.taskinstance;

import cn.sliew.scaleph.workflow.service.WorkflowTaskInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.annotation.RInject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Component
public class WorkflowTaskInstanceSuccessEventListener extends AbstractWorkflowTaskInstanceEventListener {

@Override
protected CompletableFuture handleEventAsync(WorkflowTaskInstanceEventDTO event) {
return executorService.submit(new SuccessRunner(event.getWorkflowTaskInstanceId())).toCompletableFuture();
return CompletableFuture.runAsync(new SuccessRunner(event.getWorkflowTaskInstanceId())).toCompletableFuture();
}

public static class SuccessRunner implements Runnable, Serializable {
private class SuccessRunner implements Runnable, Serializable {

private Long workflowTaskInstanceId;

@RInject
private String taskId;
@Autowired
private WorkflowTaskInstanceService workflowTaskInstanceService;

public SuccessRunner(Long workflowTaskInstanceId) {
this.workflowTaskInstanceId = workflowTaskInstanceId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class AbstractWorkflowInstanceEventListener implements WorkflowI
@Autowired
protected WorkflowInstanceService workflowInstanceService;
@Autowired
private WorkflowInstanceStateMachine stateMachine;
protected WorkflowInstanceStateMachine stateMachine;
@Autowired
private RedissonClient redissonClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ protected CompletableFuture handleEventAsync(WorkflowInstanceEventDTO event) {
future.whenCompleteAsync((unused, throwable) -> {
if (throwable != null) {
onFailure(event.getWorkflowInstanceId(), throwable);
} else {
stateMachine.onSuccess(workflowInstanceService.get(event.getWorkflowInstanceId()));
}
});
return future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,38 @@

package cn.sliew.scaleph.workflow.listener.workflowinstance;

import cn.sliew.milky.common.util.JacksonUtil;
import cn.sliew.scaleph.workflow.service.WorkflowInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
import java.io.Serializable;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Component
public class WorkflowInstanceFailureEventListener implements WorkflowInstanceEventListener {
public class WorkflowInstanceFailureEventListener extends AbstractWorkflowInstanceEventListener {

@Autowired
private WorkflowInstanceService workflowInstanceService;

@Override
public void onEvent(WorkflowInstanceEventDTO event) {
log.info("on event, {}", JacksonUtil.toJsonString(event));
protected CompletableFuture handleEventAsync(WorkflowInstanceEventDTO event) {
return CompletableFuture.runAsync(new FailureRunner(event.getWorkflowInstanceId(), event.getThrowable()));
}

private class FailureRunner implements Runnable, Serializable {

private Long workflowInstanceId;
private Optional<Throwable> throwable;

public FailureRunner(Long workflowInstanceId, Optional<Throwable> throwable) {
this.workflowInstanceId = workflowInstanceId;
this.throwable = throwable;
}

@Override
public void run() {
workflowInstanceService.updateFailure(workflowInstanceId, throwable.orElse(null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@

package cn.sliew.scaleph.workflow.listener.workflowinstance;

import cn.sliew.milky.common.util.JacksonUtil;
import cn.sliew.scaleph.workflow.service.WorkflowInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WorkflowInstanceSuccessEventListener implements WorkflowInstanceEventListener {
import java.io.Serializable;
import java.util.concurrent.CompletableFuture;

@Autowired
private WorkflowInstanceService workflowInstanceService;
@Component
public class WorkflowInstanceSuccessEventListener extends AbstractWorkflowInstanceEventListener {

@Override
public void onEvent(WorkflowInstanceEventDTO event) {
log.info("on event, {}", JacksonUtil.toJsonString(event));
protected CompletableFuture handleEventAsync(WorkflowInstanceEventDTO event) {
return CompletableFuture.runAsync(new SuccessRunner(event.getWorkflowInstanceId()));
}

private class SuccessRunner implements Runnable, Serializable {

private Long workflowInstanceId;

public SuccessRunner(Long workflowInstanceId) {
this.workflowInstanceId = workflowInstanceId;
}

@Override
public void run() {
workflowInstanceService.updateSuccess(workflowInstanceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public interface WorkflowInstanceService {

void updateState(Long id, WorkflowInstanceState state, WorkflowInstanceState nextState, String message);

void updateSuccess(Long id);

void updateFailure(Long id, Throwable throwable);

void updateTaskId(Long id, String taskId);

WorkflowInstanceDTO deploy(Long workflowDefinitionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public void updateState(Long id, WorkflowInstanceState state, WorkflowInstanceSt
workflowInstanceMapper.update(record, updateWrapper);
}

@Override
public void updateSuccess(Long id) {

}

@Override
public void updateFailure(Long id, Throwable throwable) {

}

@Override
public void updateTaskId(Long id, String taskId) {
WorkflowInstance record = new WorkflowInstance();
Expand Down

0 comments on commit a15ff2f

Please sign in to comment.