Skip to content

Commit

Permalink
simplify and fix threadlocal context stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Apr 6, 2011
1 parent c6f86a9 commit f1190e4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 180 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,86 +15,29 @@
*/

/*
* StepContext.java
* WFStepContext.java
*
* User: Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
* Created: 3/31/11 9:08 AM
* Created: 4/6/11 12:58 PM
*
*/
package com.dtolabs.rundeck.core.execution.workflow;

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.ExecutionItem;

import java.util.*;

/**
* WFStepContext contains context about a workflow step, and can generate logging context from the details of the step.
* Node and step number/item are independent.
* WFStepContext holds current step info.
*
* @author Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
*/
class WFStepContext {
private Map<String, String> loggingContext;
private ExecutionItem stepItem;
private int step = -1;
private INodeEntry node;
ExecutionItem stepItem;
int step = -1;

public int getStep() {
return step;
}

public void setStep(final int step, final ExecutionItem executionItem) {
WFStepContext(final ExecutionItem stepItem, final int step) {
this.stepItem = stepItem;
this.step = step;
this.stepItem = executionItem;
clearContext();
}

public void clearStep() {
setStep(-1, null);
}

public INodeEntry getNode() {
return node;
}

public void setNode(final INodeEntry node) {
this.node = node;
clearContext();
}

public void clearNode() {
setNode(null);
}

private void clearContext() {
loggingContext = null;
}

public Map<String, String> getContext() {
if (null != loggingContext) {
return loggingContext;
}
loggingContext = new HashMap<String, String>();
if (null != node) {
loggingContext.put("node", node.getNodename());
loggingContext.put("user", node.extractUserName());
}
if (null != stepItem) {
loggingContext.put("command", stepItem.getType() + "." + step);
}
if (step > -1) {
loggingContext.put("step", Integer.toString(step));
}
return loggingContext;
}

@Override
public String toString() {
return "StepContext{" +
"stepItem=" + stepItem +
", step=" + step +
", node=" + node +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,9 @@ public class WorkflowExecutionListenerImpl extends ContextualExecutionListener i
/**
* Thread local context stack, inherited by sub threads.
*/
private InheritableThreadLocal<Deque<WFExecContext>> localContextStack =
new InheritableThreadLocal<Deque<WFExecContext>>();
private InheritableThreadLocal<WFStepContext> localStep = new InheritableThreadLocal<WFStepContext>();
private InheritableThreadLocal<INodeEntry> localNode = new InheritableThreadLocal<INodeEntry>();

private Deque<WFExecContext> getLocalContextStack() {
if (null == localContextStack.get()) {
localContextStack.set(new ArrayDeque<WFExecContext>());
}
return localContextStack.get();
}

private void pushContext(final WFExecContext context) {
getLocalContextStack().push(context);
}

private WFExecContext popContext() {
return getLocalContextStack().pollFirst();
}

private WFExecContext peekContext() {
return getLocalContextStack().peekFirst();
}

public WorkflowExecutionListenerImpl(final FailedNodesListener failedNodesListener,
final ContextLogger logger, final boolean terse, final String logFormat) {
Expand All @@ -70,10 +52,7 @@ public WorkflowExecutionListenerImpl(final FailedNodesListener failedNodesListen
@Override
public void beginInterpretCommand(final ExecutionContext context, final ExecutionItem item, final INodeEntry node) {
super.beginInterpretCommand(context, item, node);
final WFExecContext wfExecContext = peekContext();
if (null != wfExecContext) {
wfExecContext.getStepContext().setNode(node);
}
localNode.set(node);
context.getExecutionListener().log(Constants.DEBUG_LEVEL,
"beginInterpretCommand(" + node.getNodename() + "): " + item.getType() + ": " + item);
}
Expand All @@ -82,54 +61,66 @@ public void beginInterpretCommand(final ExecutionContext context, final Executio
public void finishInterpretCommand(final InterpreterResult result, final ExecutionContext context,
final ExecutionItem item, final INodeEntry node) {
super.finishInterpretCommand(result, context, item, node);
final WFExecContext wfExecContext = peekContext();
if (null != wfExecContext) {
wfExecContext.getStepContext().clearNode();
}
localNode.set(null);
log(Constants.DEBUG_LEVEL,
"finishInterpretCommand(" + node.getNodename() + "): " + item.getType() + ": " + result);
}


@Override
public Map<String, String> getLoggingContext() {
final WFExecContext wfExecContext = peekContext();
if (null != wfExecContext) {
return wfExecContext.getLoggingContext();

if (null != localStep.get() || null != localNode.get()) {
final HashMap<String, String> loggingContext = new HashMap<String, String>();
if (null != localNode.get()) {
final INodeEntry node = localNode.get();
loggingContext.put("node", node.getNodename());
loggingContext.put("user", node.extractUserName());
}
if (null != localStep.get()) {
final WFStepContext wfStepInfo = localStep.get();
final int step = wfStepInfo.step;
loggingContext.put("command", wfStepInfo.stepItem.getType() + "." + step);

if (step > -1) {
loggingContext.put("step", Integer.toString(step));
}
}
return loggingContext;
} else {
return null;
}
}

public void beginWorkflowExecution(final ExecutionContext executionContext, final WorkflowExecutionItem item) {
pushContext(new WFExecContext(executionContext, item));
localStep.set(null);
localNode.set(null);
log(Constants.DEBUG_LEVEL,
"beginWorkflowExecution(): " + item.getType() /*+ "; " + peekContext()*/);
"[workflow] Begin execution: " + item.getType()
);
}


public void finishWorkflowExecution(final WorkflowExecutionResult result, final ExecutionContext executionContext,
final WorkflowExecutionItem item) {
popContext();
localStep.set(null);
localNode.set(null);
log(Constants.DEBUG_LEVEL,
"finishWorkflowExecution(): " + item.getType() + ": " + result /* + "; " + peekContext()*/);
"[workflow] Finish execution: " + item.getType() + ": " + result
);
}

public void beginWorkflowItem(final int step, final ExecutionItem item) {
final WFExecContext wfExecContext = peekContext();
if (null != wfExecContext) {
wfExecContext.getStepContext().setStep(step, item);
}
localStep.set(new WFStepContext(item, step));
log(Constants.DEBUG_LEVEL,
"beginWorkflowItem(" + step + "," + item.getType() + ")"/* + "; " + peekContext()*/);
"[workflow] Begin step: " + step + "," + item.getType()
);
}

public void finishWorkflowItem(final int step, final ExecutionItem item) {
final WFExecContext wfExecContext = peekContext();
if (null != wfExecContext) {
wfExecContext.getStepContext().clearStep();
}
localStep.set(null);
log(Constants.DEBUG_LEVEL,
"finishWorkflowItem(" + step + "," + item.getType() + ")" /*+ "; " + peekContext()*/);
"[workflow] Finish step: " + step + "," + item.getType()
);
}
}

0 comments on commit f1190e4

Please sign in to comment.