From 9593cfa282be89ec6d9d4ba657b5186343a28c2e Mon Sep 17 00:00:00 2001 From: "leon.li" Date: Fri, 4 Dec 2015 09:27:16 +0800 Subject: [PATCH] add workflow --- .../slb/agent/core/workflow/Context.java | 44 ++++++++++ .../slb/agent/core/workflow/Engine.java | 81 +++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 slb-agent/src/main/java/com/dianping/platform/slb/agent/core/workflow/Context.java create mode 100644 slb-agent/src/main/java/com/dianping/platform/slb/agent/core/workflow/Engine.java diff --git a/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/workflow/Context.java b/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/workflow/Context.java new file mode 100644 index 0000000..25dbd3b --- /dev/null +++ b/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/workflow/Context.java @@ -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 + *

+ * 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; + } +} diff --git a/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/workflow/Engine.java b/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/workflow/Engine.java new file mode 100644 index 0000000..e7d7be2 --- /dev/null +++ b/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/workflow/Engine.java @@ -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 + *

+ * 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 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(); + } +}