-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,019 additions
and
531 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...arp/module/workflow/internal/engine/dispatch/InternalWorkflowInstanceEventDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cn.sliew.carp.module.workflow.internal.engine.dispatch; | ||
|
||
import cn.sliew.carp.framework.common.dict.workflow.WorkflowInstanceEvent; | ||
import cn.sliew.carp.module.queue.api.Message; | ||
import cn.sliew.carp.module.queue.api.MessageHandler; | ||
import cn.sliew.carp.module.queue.api.MessageListener; | ||
import cn.sliew.carp.module.queue.api.util.Serder; | ||
import cn.sliew.carp.module.workflow.api.engine.dispatch.WorkflowInstanceEventDispatcher; | ||
import cn.sliew.carp.module.workflow.api.engine.dispatch.handler.WorkflowInstanceEventHandler; | ||
import cn.sliew.carp.module.workflow.api.engine.dispatch.event.WorkflowInstanceStatusEvent; | ||
import cn.sliew.carp.module.workflow.internal.engine.dispatch.event.WorkflowInstanceEventDTO; | ||
import cn.sliew.carp.module.workflow.internal.statemachine.WorkflowTaskInstanceStateMachine; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.DisposableBean; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Slf4j | ||
@MessageListener(topic = InternalWorkflowInstanceEventDispatcher.TOPIC, consumerGroup = WorkflowTaskInstanceStateMachine.CONSUMER_GROUP) | ||
public class InternalWorkflowInstanceEventDispatcher implements WorkflowInstanceEventDispatcher, MessageHandler, InitializingBean, DisposableBean { | ||
|
||
public static final String TOPIC = "TOPIC_CARP_INTERNAL_WORKFLOW_INSTANCE_EVENT"; | ||
|
||
@Autowired | ||
private List<WorkflowInstanceEventHandler> handlers; | ||
|
||
private Map<WorkflowInstanceEvent, WorkflowInstanceEventHandler> registry = new HashMap<>(); | ||
private ThreadPoolTaskExecutor taskExecutor; | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
if (CollectionUtils.isEmpty(handlers) == false) { | ||
handlers.stream().forEach(handler -> registry.put(handler.getType(), handler)); | ||
} | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setMaxPoolSize(5); | ||
executor.setCorePoolSize(1); | ||
executor.setThreadNamePrefix("workflow-instance-thread-pool-"); | ||
executor.initialize(); | ||
taskExecutor = executor; | ||
} | ||
|
||
@Override | ||
public void destroy() throws Exception { | ||
if (taskExecutor != null) { | ||
taskExecutor.shutdown(); | ||
} | ||
} | ||
|
||
@Override | ||
public void handler(Message message) throws Exception { | ||
if (message.getBody() != null) { | ||
Object deserialized = Serder.deserializeByJava(message.getBody()); | ||
if (deserialized instanceof WorkflowInstanceEventDTO) { | ||
WorkflowInstanceEventDTO eventDTO = (WorkflowInstanceEventDTO) deserialized; | ||
dispatch(eventDTO); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void dispatch(WorkflowInstanceStatusEvent event) { | ||
if (registry.containsKey(event.getEvent()) == false) { | ||
throw new RuntimeException("unknown workflow instance event: " | ||
+ event.getEvent().getLabel() + "[" + event.getEvent().getValue() + "]"); | ||
} | ||
WorkflowInstanceEventHandler handler = registry.get(event.getEvent()); | ||
CompletableFuture.runAsync(() -> handler.handle(event), taskExecutor) | ||
.whenComplete((unused, throwable) -> { | ||
if (throwable != null) { | ||
log.error("workflow instance event dispatch failed", throwable); | ||
} | ||
}); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...module/workflow/internal/engine/dispatch/InternalWorkflowTaskInstanceEventDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cn.sliew.carp.module.workflow.internal.engine.dispatch; | ||
|
||
import cn.sliew.carp.framework.common.dict.workflow.WorkflowTaskInstanceEvent; | ||
import cn.sliew.carp.module.queue.api.Message; | ||
import cn.sliew.carp.module.queue.api.MessageHandler; | ||
import cn.sliew.carp.module.queue.api.MessageListener; | ||
import cn.sliew.carp.module.queue.api.util.Serder; | ||
import cn.sliew.carp.module.workflow.api.engine.dispatch.WorkflowTaskInstanceEventDispatcher; | ||
import cn.sliew.carp.module.workflow.api.engine.dispatch.event.WorkflowTaskInstanceStatusEvent; | ||
import cn.sliew.carp.module.workflow.api.engine.dispatch.handler.WorkflowTaskInstanceEventHandler; | ||
import cn.sliew.carp.module.workflow.internal.engine.dispatch.event.WorkflowTaskInstanceEventDTO; | ||
import cn.sliew.carp.module.workflow.internal.statemachine.WorkflowInstanceStateMachine; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.DisposableBean; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Slf4j | ||
@MessageListener(topic = InternalWorkflowTaskInstanceEventDispatcher.TOPIC, consumerGroup = WorkflowInstanceStateMachine.CONSUMER_GROUP) | ||
public class InternalWorkflowTaskInstanceEventDispatcher implements WorkflowTaskInstanceEventDispatcher, MessageHandler, InitializingBean, DisposableBean { | ||
|
||
public static final String TOPIC = "TOPIC_CARP_INTERNAL_WORKFLOW_TASK_INSTANCE_EVENT"; | ||
|
||
@Autowired | ||
private List<WorkflowTaskInstanceEventHandler> handlers; | ||
|
||
private Map<WorkflowTaskInstanceEvent, WorkflowTaskInstanceEventHandler> registry = new HashMap<>(); | ||
private ThreadPoolTaskExecutor taskExecutor; | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
if (CollectionUtils.isEmpty(handlers) == false) { | ||
handlers.stream().forEach(handler -> registry.put(handler.getType(), handler)); | ||
} | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setMaxPoolSize(5); | ||
executor.setCorePoolSize(1); | ||
executor.setThreadNamePrefix("workflow-task-instance-thread-pool-"); | ||
executor.initialize(); | ||
taskExecutor = executor; | ||
} | ||
|
||
@Override | ||
public void destroy() throws Exception { | ||
if (taskExecutor != null) { | ||
taskExecutor.shutdown(); | ||
} | ||
} | ||
|
||
@Override | ||
public void handler(Message message) throws Exception { | ||
if (message.getBody() != null) { | ||
Object deserialized = Serder.deserializeByJava(message.getBody()); | ||
if (deserialized instanceof WorkflowTaskInstanceEventDTO) { | ||
WorkflowTaskInstanceEventDTO eventDTO = (WorkflowTaskInstanceEventDTO) deserialized; | ||
dispatch(eventDTO); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void dispatch(WorkflowTaskInstanceStatusEvent event) { | ||
if (registry.containsKey(event.getEvent()) == false) { | ||
throw new RuntimeException("unknown workflow task instance event: " | ||
+ event.getEvent().getLabel() + "[" + event.getEvent().getValue() + "]"); | ||
} | ||
WorkflowTaskInstanceEventHandler handler = registry.get(event.getEvent()); | ||
CompletableFuture.runAsync(() -> handler.handle(event), taskExecutor) | ||
.whenComplete((unused, throwable) -> { | ||
if (throwable != null) { | ||
log.error("workflow task instance event dispatch failed", throwable); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...rkflow/internal/engine/dispatch/handler/task/WorkflowTaskInstanceDeployEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cn.sliew.carp.module.workflow.internal.engine.dispatch.handler.task; | ||
|
||
import cn.sliew.carp.framework.common.dict.workflow.WorkflowTaskInstanceEvent; | ||
import cn.sliew.carp.framework.dag.service.dto.DagStepDTO; | ||
import cn.sliew.carp.module.workflow.internal.engine.dispatch.event.WorkflowTaskInstanceEventDTO; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Date; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Component | ||
public class WorkflowTaskInstanceDeployEventListener extends AbstractWorkflowTaskInstanceEventListener { | ||
|
||
@Override | ||
public WorkflowTaskInstanceEvent getType() { | ||
return WorkflowTaskInstanceEvent.COMMAND_DEPLOY; | ||
} | ||
|
||
@Override | ||
protected CompletableFuture handleEventAsync(WorkflowTaskInstanceEventDTO event) { | ||
CompletableFuture<?> future = CompletableFuture.runAsync(() -> run(event)).toCompletableFuture(); | ||
future.whenCompleteAsync((unused, throwable) -> { | ||
if (throwable != null) { | ||
onFailure(event.getWorkflowTaskInstanceId(), throwable); | ||
} | ||
}); | ||
return future; | ||
} | ||
|
||
private void run(WorkflowTaskInstanceEventDTO event) { | ||
DagStepDTO dagStepUpdateParam = new DagStepDTO(); | ||
dagStepUpdateParam.setId(event.getWorkflowTaskInstanceId()); | ||
dagStepUpdateParam.setStatus(event.getNextState().getValue()); | ||
dagStepUpdateParam.setStartTime(new Date()); | ||
dagStepService.update(dagStepUpdateParam); | ||
stateMachine.onSuccess(workflowInstanceService.getTask(event.getWorkflowTaskInstanceId())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.