I have a long-running workflow with the following code structure:
public class MyWorkflowImp implements MyWorkflow {
public void myWorkflowMethod() {
while(true) {
if (exit) return;
StatusResponse resp = myWorkFlowQueue.take();//myWorkFlowQueue = Workflow.newWorkflowQueue(100); the input is coming from the signal method
String state = resp.getState();
MyObject result = activity0.func();//database operation
switch(state) {
case "A":
processCaseA(result);
break;
case "B":
processCaseB(result);
break;
case "C":
processCaseC(result);
break;
}
}
private void processCaseA(MyObject result) { //private mechod in MyWorkflowImp
if (result.equals("XXX")) {
activity1.updateDatabase1();
dataProcess1();
activity1.callUpstreamServiceA();
} else if (result.equals("ZZZ")) {
activity2.callUpstreamServiceB();
activity1.updateDatabase1();
dataProcess2();
activity1.updateDatabase2();
}
}
private void dataProcess1(String result) {};//private mechod in MyWorkflowImp
private void dataProcess2(String result) {};//private mechod in MyWorkflowImp
}
If I wanna refactor code logic or fix bugs in the methods processCaseA(), processCaseB(), processCaseC(), dataProcess1(), or dataProcess2(), I must fix them and re-deploy but it would affect the existing running workflows. I’ve watched the Versioning tutorial you posted on youtube. but seems like it would be very hard to apply it in my current code structure in a clean way.
So I’m thinking instead of having my code as shown above, would it be better to refactor it in the following way:
Class MyActivityImp implements MyActivity {
private void updateDatabase1(MyObject myObject) {...};
private void updateDatabase2(MyObject myObject) {.....};
private void dataProcess1(MyObject myObject) {.....};
private void dataProcess2(MyObject myObject) {......};
private void callDownstreamServiceA() {......};
private void callDownstreamServiceB() {......};
@Override
processCaseA(MyObject myObject) {
if (result.equals("XXX")) {
updateDatabase1(myObject)
dataProcess1(myObject);
callDownstreamServiceA();
} else if (result.equals("ZZZ")) {
callDownstreamServiceB()
updateDatabase1(myObject);
dataProcess2(myObject);
updateDatabase2(myObject);
}
}
}
public class MyWorkflowImp implements MyWorkflow {
public void myWorkflowMethod() {
while(true) {
if (exit) return;
StatusResponse resp = myWorkFlowQueue.take();//myWorkFlowQueue = Workflow.newWorkflowQueue(100); the input is coming from the signal method
String state = resp.getState();
MyObject myObject = activity0.func();//database operation
switch(state) {
case "A":
myActivity.processCaseA(myObject);//this activity method includes multiple operations including DB operations and calling downstream service.
break;
case "B":
myActivity.processCaseB(myObject);
break;
case "C":
myActivity.processCaseC(myObject);
break;
}
}
}
The primary change was: to change the processCaseA() method from a private helper function in MyWorkflowImp class to a single ActivityMethod in MyActivity. With this change, this activity method includes multiple operations including DB operations and calling downstream service. Ideally, I wanna these operations (DB operation + downstream service) to be transactional.
so that whenever there’s any code refactor needed in these methods such as processCaseA(), we can make the code fix and re-deploy it to prod without Versioning in the workflow method.
Just wondering if is this the right way to deal with it?