Search in sources :

Example 1 with StepContext

use of org.jenkinsci.plugins.workflow.steps.StepContext in project selenium_java by sergueik.

the class Audit2DbStepTests method testStartWithStepContextReturnsAudit2DbExecution.

@Test
public void testStartWithStepContextReturnsAudit2DbExecution() throws Exception {
    StepContext context = Mockito.mock(StepContext.class);
    Audit2DbStep step = new Audit2DbStep();
    StepExecution execution = step.start(context);
    assertNotNull("Null StepExecution returned.", execution);
    assertTrue("Expected Audit2DbExecution", Audit2DbExecution.class.isInstance(execution));
    assertThat("StepContext should be injected into execution.", execution, hasProperty("context", sameInstance(context)));
    assertThat("Step should be injected into execution.", execution, hasProperty("step", sameInstance(step)));
}
Also used : StepContext(org.jenkinsci.plugins.workflow.steps.StepContext) StepExecution(org.jenkinsci.plugins.workflow.steps.StepExecution) Test(org.junit.Test)

Example 2 with StepContext

use of org.jenkinsci.plugins.workflow.steps.StepContext in project workflow-cps-plugin by jenkinsci.

the class CpsBodyExecution method launch.

/**
 * Starts evaluating the body.
 *
 * If the body is a synchronous closure, this method evaluates the closure synchronously.
 * Otherwise, the body is asynchronous and the method schedules another thread to evaluate the body.
 *
 * @param currentThread
 *      The thread whose context the new thread will inherit.
 */
@CpsVmThreadOnly
/*package*/
void launch(CpsBodyInvoker params, CpsThread currentThread, FlowHead head) {
    if (isLaunched())
        throw new IllegalStateException("Already launched");
    StepStartNode sn = addBodyStartFlowNode(head);
    for (Action a : params.startNodeActions) {
        if (a != null)
            sn.addAction(a);
    }
    head.setNewHead(sn);
    CpsFlowExecution.maybeAutoPersistNode(sn);
    StepContext sc = new CpsBodySubContext(context, sn);
    for (BodyExecutionCallback c : callbacks) {
        c.onStart(sc);
    }
    try {
        // TODO: handle arguments to closure
        Object x = params.body.getBody(currentThread).call();
        // pointless synchronization to make findbugs happy. This is already done, so there's no cancelling this anyway.
        synchronized (this) {
            this.thread = currentThread;
        }
        onSuccess.receive(x);
    } catch (CpsCallableInvocation e) {
        // execute this closure asynchronously
        // TODO: does it make sense that the new thread shares the same head?
        CpsThread t = currentThread.group.addThread(createContinuable(currentThread, e), head, ContextVariableSet.from(currentThread.getContextVariables(), params.contextOverrides));
        // due to earlier cancellation
        synchronized (this) {
            t.resume(new Outcome(null, stopped));
            assert this.thread == null;
            this.thread = t;
        }
    } catch (Throwable t) {
        // body has completed synchronously and abnormally
        synchronized (this) {
            this.thread = currentThread;
        }
        onFailure.receive(t);
    }
}
Also used : ErrorAction(org.jenkinsci.plugins.workflow.actions.ErrorAction) Action(hudson.model.Action) BodyInvocationAction(org.jenkinsci.plugins.workflow.actions.BodyInvocationAction) StepStartNode(org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode) StepContext(org.jenkinsci.plugins.workflow.steps.StepContext) CpsCallableInvocation(com.cloudbees.groovy.cps.impl.CpsCallableInvocation) Outcome(com.cloudbees.groovy.cps.Outcome) BodyExecutionCallback(org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback)

Example 3 with StepContext

use of org.jenkinsci.plugins.workflow.steps.StepContext in project selenium_java by sergueik.

the class Audit2DbExecution method run.

@Override
protected Void run() throws Exception {
    StepContext context = getContext();
    Run<?, ?> run = context.get(Run.class);
    LOGGER.fine("StepContext.Run: " + run);
    Properties props = HibernateUtil.getExtraProperties(step.getJdbcDriver(), step.getJdbcUrl(), step.getJdbcUsername(), step.getJdbcPassword());
    if (StringUtils.isNotBlank(step.getHibernateDialect())) {
        props.put("hibernate.dialect", step.getHibernateDialect());
    }
    LOGGER.fine("props: " + props);
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory(props);
    LOGGER.fine("sessionFactory: " + sessionFactory);
    BuildDetailsRepository repository = new BuildDetailsHibernateRepository(sessionFactory);
    BuildDetails details = BuildDetailsResolver.resolveBuildDetails(run, context.get(Computer.class));
    Audit2DbGraphListener listener = new Audit2DbGraphListener(repository, details);
    context.get(FlowExecution.class).addListener(listener);
    return null;
}
Also used : SessionFactory(org.hibernate.SessionFactory) StepContext(org.jenkinsci.plugins.workflow.steps.StepContext) BuildDetails(org.jenkins.plugins.audit2db.model.BuildDetails) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) BuildDetailsRepository(org.jenkins.plugins.audit2db.data.BuildDetailsRepository) Computer(hudson.model.Computer) Properties(java.util.Properties) BuildDetailsHibernateRepository(org.jenkins.plugins.audit2db.internal.data.BuildDetailsHibernateRepository)

Aggregations

StepContext (org.jenkinsci.plugins.workflow.steps.StepContext)3 Outcome (com.cloudbees.groovy.cps.Outcome)1 CpsCallableInvocation (com.cloudbees.groovy.cps.impl.CpsCallableInvocation)1 Action (hudson.model.Action)1 Computer (hudson.model.Computer)1 Properties (java.util.Properties)1 SessionFactory (org.hibernate.SessionFactory)1 BuildDetailsRepository (org.jenkins.plugins.audit2db.data.BuildDetailsRepository)1 BuildDetailsHibernateRepository (org.jenkins.plugins.audit2db.internal.data.BuildDetailsHibernateRepository)1 BuildDetails (org.jenkins.plugins.audit2db.model.BuildDetails)1 BodyInvocationAction (org.jenkinsci.plugins.workflow.actions.BodyInvocationAction)1 ErrorAction (org.jenkinsci.plugins.workflow.actions.ErrorAction)1 StepStartNode (org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode)1 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)1 BodyExecutionCallback (org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback)1 StepExecution (org.jenkinsci.plugins.workflow.steps.StepExecution)1 Test (org.junit.Test)1