add workflow

This commit is contained in:
leon.li 2015-12-04 09:27:16 +08:00
parent 9108578dfb
commit 9593cfa282
2 changed files with 125 additions and 0 deletions

View file

@ -0,0 +1,44 @@
package com.dianping.platform.slb.agent.core.workflow;
import com.dianping.platform.slb.agent.core.task.Task;
import java.io.OutputStream;
/**
* dianping.com @2015
* slb - soft load balance
* <p/>
* Created by leon.li(Li Yang)
*/
public class Context {
private Task m_task;
private OutputStream m_output;
private boolean m_isKilled;
public Task getTask() {
return m_task;
}
public void setTask(Task task) {
m_task = task;
}
public OutputStream getOutput() {
return m_output;
}
public void setOutput(OutputStream output) {
m_output = output;
}
public boolean isKilled() {
return m_isKilled;
}
public void kill() {
m_isKilled = true;
}
}

View file

@ -0,0 +1,81 @@
package com.dianping.platform.slb.agent.core.workflow;
import com.dianping.platform.slb.agent.core.task.LogFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.OutputStream;
import java.util.Map;
/**
* dianping.com @2015
* slb - soft load balance
* <p/>
* Created by leon.li(Li Yang)
*/
@Service
public class Engine {
private static Logger logger = LoggerFactory.getLogger(Engine.class);
@Autowired
LogFormatter logFormatter;
public int start(Step startStep, Context ctx) {
Step curStep = startStep;
int exitCode = 0;
OutputStream logOut = ctx.getOutput();
while (curStep != null) {
if (ctx.isKilled()) {
break;
}
writeLogChunkHeader(curStep.getLogChunkHeader(), logOut);
logger.info(String.format(">>>>> Current Step Is [%s] <<<<<", curStep));
try {
exitCode = curStep.doStep(ctx);
} catch (Exception e) {
logger.error(String.format(">>>>> Exception When Doing Step %s <<<<<", curStep), e);
exitCode = Step.CODE_ERROR;
}
logger.info(String.format("##### Current Step [%s] Is [%s] ####", curStep,
exitCode == Step.CODE_OK ? "SUCCESS" : "FAILED"));
writeLogChunkTerminator(logOut);
curStep = curStep.getNextStep(exitCode);
}
writeLogTerminator(logOut);
return exitCode;
}
private void writeLogChunkHeader(Map<String, String> headers, OutputStream logOut) {
try {
logFormatter.writeHeader(logOut, headers);
} catch (Exception e) {
logger.error("error write log chunk header", e);
}
}
private void writeLogChunkTerminator(OutputStream logOut) {
try {
logFormatter.writeChunkTerminator(logOut);
} catch (Exception e) {
logger.error("error write log chunk terminator", e);
}
}
private void writeLogTerminator(OutputStream logOut) {
try {
logFormatter.writeTerminator(logOut);
} catch (Exception e) {
logger.error("error write log terminator", e);
}
}
public void kill(Context ctx) {
ctx.kill();
}
}