Search in sources :

Example 26 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class ScheduledPipelineLoader method pipelineWithPasswordAwareBuildCauseByBuildId.

// TODO: Do we need to do this differently than PipelineService#fullPipeline?
public Pipeline pipelineWithPasswordAwareBuildCauseByBuildId(final long buildId) {
    Pipeline pipeline = pipelineDao.pipelineWithMaterialsAndModsByBuildId(buildId);
    MaterialRevisions scheduledRevs = pipeline.getBuildCause().getMaterialRevisions();
    MaterialConfigs knownMaterials = knownMaterials(pipeline, scheduledRevs);
    for (MaterialRevision materialRevision : scheduledRevs) {
        MaterialConfig materialConfig = materialFrom(knownMaterials, materialRevision);
        Material usedMaterial = materialRevision.getMaterial();
        if (materialConfig == null) {
            final JobInstance jobInstance = jobInstanceService.buildByIdWithTransitions(buildId);
            scheduleService.failJob(jobInstance);
            final String message = "Cannot load job '" + jobInstance.buildLocator() + "' because material " + usedMaterial.config() + " was not found in config.";
            final String description = "Job for pipeline '" + jobInstance.buildLocator() + "' has been failed as one or more material configurations were either changed or removed.";
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    final ServerHealthState error = ServerHealthState.error(message, description, HealthStateType.general(HealthStateScope.forJob(jobInstance.getPipelineName(), jobInstance.getStageName(), jobInstance.getName())));
                    error.setTimeout(Timeout.FIVE_MINUTES);
                    serverHealthService.update(error);
                    appendToConsoleLog(jobInstance, message);
                    appendToConsoleLog(jobInstance, description);
                }
            });
            throw new StaleMaterialsOnBuildCause(message);
        }
        usedMaterial.updateFromConfig(materialConfig);
    }
    return pipeline;
}
Also used : MaterialConfigs(com.thoughtworks.go.config.materials.MaterialConfigs) JobInstance(com.thoughtworks.go.domain.JobInstance) MaterialRevisions(com.thoughtworks.go.domain.MaterialRevisions) StaleMaterialsOnBuildCause(com.thoughtworks.go.server.materials.StaleMaterialsOnBuildCause) MaterialConfig(com.thoughtworks.go.domain.materials.MaterialConfig) Material(com.thoughtworks.go.domain.materials.Material) ServerHealthState(com.thoughtworks.go.serverhealth.ServerHealthState) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) MaterialRevision(com.thoughtworks.go.domain.MaterialRevision) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Pipeline(com.thoughtworks.go.domain.Pipeline)

Example 27 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class StageService method updateStageWithoutNotifications.

private void updateStageWithoutNotifications(final Stage stage) {
    stage.calculateResult();
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    clearCachedCompletedStageFeeds(stage.getIdentifier().getPipelineName());
                }
            });
            stageDao.updateResult(stage, stage.getResult());
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 28 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class PipelineSqlMapDao method save.

public Pipeline save(final Pipeline pipeline) {
    return (Pipeline) transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    goCache.remove(cacheKeyForLatestPipelineIdByPipelineName(pipeline.getName()));
                    invalidateCacheConditionallyForPipelineInstancesTriggeredWithDependencyMaterial(pipeline);
                }
            });
            Long pipelineId = (Long) getSqlMapClientTemplate().insert("insertPipeline", pipeline);
            savePipelineMaterialRevisions(pipeline, pipelineId);
            environmentVariableDao.save(pipelineId, EnvironmentVariableType.Trigger, pipeline.scheduleTimeVariables());
            return pipeline;
        }
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 29 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class StageSqlMapDao method save.

public Stage save(final Pipeline pipeline, final Stage stage) {
    return (Stage) transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    String pipelineName = pipeline.getName();
                    String stageName = stage.getName();
                    clearStageHistoryPageCaches(stage, pipelineName, false);
                    clearCachedStage(stage.getIdentifier());
                    clearCachedAllStages(pipelineName, pipeline.getCounter(), stageName);
                    removeFromCache(cacheKeyForStageCountForGraph(pipelineName, stageName));
                }
            });
            stage.setPipelineId(pipeline.getId());
            int maxStageCounter = getMaxStageCounter(pipeline.getId(), stage.getName());
            stage.setCounter(maxStageCounter + 1);
            getSqlMapClientTemplate().update("markPreviousStageRunsAsNotLatest", arguments("stageName", stage.getName()).and("pipelineId", pipeline.getId()).asMap());
            getSqlMapClientTemplate().insert("insertStage", stage);
            stage.setIdentifier(new StageIdentifier(pipeline, stage));
            return stage;
        }
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 30 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class GoCache method stopServingForTransaction.

public void stopServingForTransaction() {
    if (transactionSynchronizationManager.isTransactionBodyExecuting() && !doNotServeForTransaction()) {
        doNotServeForTransaction.set(true);
        transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

            @Override
            public void beforeCompletion() {
                doNotServeForTransaction.set(false);
            }
        });
    }
}
Also used : TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Aggregations

TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)34 Test (org.junit.Test)16 TransactionStatus (org.springframework.transaction.TransactionStatus)15 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)14 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 UserTransaction (javax.transaction.UserTransaction)7 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)7 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)6 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5 Pipeline (com.thoughtworks.go.domain.Pipeline)3 SystemException (javax.transaction.SystemException)3 PipelineState (com.thoughtworks.go.domain.PipelineState)2 GitCommand (com.thoughtworks.go.domain.materials.git.GitCommand)2 TransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TransactionSynchronizationManager)2 File (java.io.File)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 SimpleTransactionStatus (org.springframework.transaction.support.SimpleTransactionStatus)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2