Search in sources :

Example 1 with TransactionCallback

use of com.thoughtworks.go.server.transaction.TransactionCallback in project gocd by gocd.

the class ScheduleServiceStageTriggerTest method shouldNotNotifyListenersForWhenCancelStageTransactionRollsback.

@Test
public void shouldNotNotifyListenersForWhenCancelStageTransactionRollsback() throws Exception {
    Pipeline oldest = preCondition.createPipelineWithFirstStagePassedAndSecondStageRunning();
    preCondition.createPipelineWithFirstStagePassedAndSecondStageHasNotStarted();
    preCondition.createPipelineWithFirstStagePassedAndSecondStageHasNotStarted();
    final Stage stage = oldest.getStages().byName(preCondition.ftStage);
    final StageIdentifier identifier = stage.getIdentifier();
    StageStatusTopic stageStatusTopic = mock(StageStatusTopic.class);
    JobResultTopic jobResultTopic = mock(JobResultTopic.class);
    StageStatusListener stageStatusListener = mock(StageStatusListener.class);
    JobInstanceService jobInstanceService = jobInstanceService(jobResultTopic);
    StageService stageService = new StageService(stageDao, jobInstanceService, stageStatusTopic, stageStatusCache, securityService, pipelineDao, changesetService, goConfigService, transactionTemplate, transactionSynchronizationManager, goCache, stageStatusListener);
    SchedulingPerformanceLogger schedulingPerformanceLogger = mock(SchedulingPerformanceLogger.class);
    scheduleService = new ScheduleService(goConfigService, pipelineService, stageService, schedulingCheckerService, pipelineScheduledTopic, pipelineDao, stageDao, stageOrderService, securityService, pipelineScheduleQueue, this.jobInstanceService, jobInstanceDao, agentAssignment, environmentConfigService, pipelineLockService, serverHealthService, transactionTemplate, null, transactionSynchronizationManager, null, null, null, null, schedulingPerformanceLogger, null);
    try {
        transactionTemplate.executeWithExceptionHandling(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) throws Exception {
                scheduleService.cancelAndTriggerRelevantStages(stage.getId(), null, null);
                throw new GoUnauthorizedException();
            }
        });
    } catch (Exception e) {
    //ignore
    }
    verify(stageStatusTopic, never()).post(any(StageStatusMessage.class));
    verify(jobResultTopic, never()).post(any(JobResultMessage.class));
    verify(stageStatusListener, never()).stageStatusChanged(any(Stage.class));
}
Also used : StageStatusTopic(com.thoughtworks.go.server.messaging.StageStatusTopic) JobResultTopic(com.thoughtworks.go.server.messaging.JobResultTopic) TransactionStatus(org.springframework.transaction.TransactionStatus) SchedulingPerformanceLogger(com.thoughtworks.go.server.perf.SchedulingPerformanceLogger) GoUnauthorizedException(com.thoughtworks.go.server.GoUnauthorizedException) CannotScheduleException(com.thoughtworks.go.domain.CannotScheduleException) Pipeline(com.thoughtworks.go.domain.Pipeline) StageStatusListener(com.thoughtworks.go.server.domain.StageStatusListener) StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) TransactionCallback(com.thoughtworks.go.server.transaction.TransactionCallback) GoUnauthorizedException(com.thoughtworks.go.server.GoUnauthorizedException) JobResultMessage(com.thoughtworks.go.server.messaging.JobResultMessage) Stage(com.thoughtworks.go.domain.Stage) StageStatusMessage(com.thoughtworks.go.server.messaging.StageStatusMessage) Test(org.junit.Test)

Example 2 with TransactionCallback

use of com.thoughtworks.go.server.transaction.TransactionCallback in project gocd by gocd.

the class MaterialDatabaseUpdater method updateMaterial.

public void updateMaterial(final Material material) throws Exception {
    String materialMutex = mutexForMaterial(material);
    HealthStateScope scope = HealthStateScope.forMaterial(material);
    try {
        MaterialInstance materialInstance = materialRepository.findMaterialInstance(material);
        if (materialInstance == null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(String.format("[Material Update] Material repository not found, creating with latest revision from %s", material));
            }
            synchronized (materialMutex) {
                if (materialRepository.findMaterialInstance(material) == null) {
                    transactionTemplate.executeWithExceptionHandling(new TransactionCallback() {

                        @Override
                        public Object doInTransaction(TransactionStatus status) throws Exception {
                            initializeMaterialWithLatestRevision(material);
                            return null;
                        }
                    });
                }
            }
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(String.format("[Material Update] Existing material repository, fetching new revisions from %s in flyweight %s", material, materialInstance.getFlyweightName()));
            }
            synchronized (materialMutex) {
                transactionTemplate.executeWithExceptionHandling(new TransactionCallback() {

                    @Override
                    public Object doInTransaction(TransactionStatus status) throws Exception {
                        updateMaterialWithNewRevisions(material);
                        return null;
                    }
                });
            }
        }
        healthService.removeByScope(scope);
    } catch (Exception e) {
        String message = "Modification check failed for material: " + material.getLongDescription();
        String errorDescription = e.getMessage() == null ? "Unknown error" : e.getMessage();
        healthService.update(ServerHealthState.error(message, errorDescription, HealthStateType.general(scope)));
        LOGGER.warn(String.format("[Material Update] %s", message), e);
        throw e;
    }
}
Also used : HealthStateScope(com.thoughtworks.go.serverhealth.HealthStateScope) TransactionCallback(com.thoughtworks.go.server.transaction.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) MaterialInstance(com.thoughtworks.go.domain.MaterialInstance)

Example 3 with TransactionCallback

use of com.thoughtworks.go.server.transaction.TransactionCallback in project gocd by gocd.

the class ModificationIntegrationTest method shouldPersistAdditionalDataIntoModificationsTable.

@Test
public void shouldPersistAdditionalDataIntoModificationsTable() throws Exception {
    String revision = "revision";
    HashMap<String, String> additionalDataMap = new HashMap<>();
    additionalDataMap.put("key", "value");
    String additionalData = JsonHelper.toJsonString(additionalDataMap);
    final Modification modification = new Modification("user", "comment", "foo@bar.fooadsf", new Date(), revision, additionalData);
    final GitMaterial git = new GitMaterial("url", "branch");
    transactionTemplate.executeWithExceptionHandling(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) throws Exception {
            MaterialInstance gitInstance = materialRepository.findOrCreateFrom(git);
            materialRepository.saveModification(gitInstance, modification);
            return null;
        }
    });
    Modification actual = materialRepository.findModificationWithRevision(git, revision);
    /*
            Checking if time are the same, because actual has SQLTime whereas we send in java.util.Date. No idea how we were testing this ever
            because Modification#equals fails instance check (ShilpaG & Sachin)
         */
    assertThat(actual.getModifiedTime().getTime(), is(modification.getModifiedTime().getTime()));
    assertThat(actual.getAdditionalData(), is(additionalData));
    assertThat(actual.getAdditionalDataMap(), is(additionalDataMap));
}
Also used : TransactionCallback(com.thoughtworks.go.server.transaction.TransactionCallback) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) HashMap(java.util.HashMap) TransactionStatus(org.springframework.transaction.TransactionStatus) MaterialInstance(com.thoughtworks.go.domain.MaterialInstance) Date(java.util.Date) Test(org.junit.Test)

Aggregations

TransactionCallback (com.thoughtworks.go.server.transaction.TransactionCallback)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 MaterialInstance (com.thoughtworks.go.domain.MaterialInstance)2 Test (org.junit.Test)2 GitMaterial (com.thoughtworks.go.config.materials.git.GitMaterial)1 CannotScheduleException (com.thoughtworks.go.domain.CannotScheduleException)1 Pipeline (com.thoughtworks.go.domain.Pipeline)1 Stage (com.thoughtworks.go.domain.Stage)1 StageIdentifier (com.thoughtworks.go.domain.StageIdentifier)1 GoUnauthorizedException (com.thoughtworks.go.server.GoUnauthorizedException)1 StageStatusListener (com.thoughtworks.go.server.domain.StageStatusListener)1 JobResultMessage (com.thoughtworks.go.server.messaging.JobResultMessage)1 JobResultTopic (com.thoughtworks.go.server.messaging.JobResultTopic)1 StageStatusMessage (com.thoughtworks.go.server.messaging.StageStatusMessage)1 StageStatusTopic (com.thoughtworks.go.server.messaging.StageStatusTopic)1 SchedulingPerformanceLogger (com.thoughtworks.go.server.perf.SchedulingPerformanceLogger)1 HealthStateScope (com.thoughtworks.go.serverhealth.HealthStateScope)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1