public JobInstance updateStateAndResult(final JobInstance jobInstance) { return (JobInstance) transactionTemplate.execute( new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { transactionSynchronizationManager.registerSynchronization( new TransactionSynchronizationAdapter() { @Override public void afterCommit() { // Methods not extracted in order to make synchronization visible. synchronized (cacheKeyForJobPlan(jobInstance.getId())) { removeCachedJobPlan(jobInstance); } synchronized (cacheKeyForActiveJobIds()) { goCache.remove(cacheKeyForActiveJobIds()); } String activeJobKey = cacheKeyForActiveJob(jobInstance.getId()); synchronized (activeJobKey) { goCache.remove(activeJobKey); } removeCachedJobInstance(jobInstance); } }); logIfJobIsCompleted(jobInstance); updateStatus(jobInstance); updateResult(jobInstance); return jobInstance; } }); }
@Before public void setup() throws Exception { diskSpaceSimulator = new DiskSpaceSimulator(); new HgTestRepo("testHgRepo"); svnRepository = new SvnTestRepo("testSvnRepo"); dbHelper.onSetUp(); configHelper.onSetUp(); configHelper.usingCruiseConfigDao(goConfigDao).initializeConfigFile(); repository = new SvnCommand(null, svnRepository.projectRepositoryUrl()); goParentPipelineConfig = configHelper.addPipeline( GO_PIPELINE_UPSTREAM, STAGE_NAME, new MaterialConfigs(new GitMaterialConfig("foo-bar")), "unit"); goPipelineConfig = configHelper.addPipeline(GO_PIPELINE_NAME, STAGE_NAME, repository, "unit"); svnMaterialRevs = new MaterialRevisions(); SvnMaterial svnMaterial = SvnMaterial.createSvnMaterialWithMock(repository); svnMaterialRevs.addRevision( svnMaterial, svnMaterial.latestModification( null, new ServerSubprocessExecutionContext(goConfigService, new SystemEnvironment()))); final MaterialRevisions materialRevisions = new MaterialRevisions(); SvnMaterial anotherSvnMaterial = SvnMaterial.createSvnMaterialWithMock(repository); materialRevisions.addRevision( anotherSvnMaterial, anotherSvnMaterial.latestModification(null, subprocessExecutionContext)); transactionTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { materialRepository.save(svnMaterialRevs); } }); BuildCause buildCause = BuildCause.createWithModifications(svnMaterialRevs, ""); mingleConfig = configHelper.addPipeline( MINGLE_PIPELINE_NAME, STAGE_NAME, repository, new Filter(new IgnoredFiles("**/*.doc")), "unit", "functional"); latestPipeline = PipelineMother.schedule(this.mingleConfig, buildCause); latestPipeline = pipelineDao.saveWithStages(latestPipeline); dbHelper.passStage(latestPipeline.getStages().first()); pipelineScheduleQueue.clear(); }
private void updateResultInTransaction(final Stage stage, final StageResult stageResult) { transactionTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { stageDao.updateResult(stage, stageResult); } }); }
public void transaction(final Runnable txn) { txnTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { txn.run(); } }); }
public void deleteAllOauthGrants() { txnTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { getHibernateTemplate().bulkUpdate("DELETE OauthAuthorization"); getHibernateTemplate().bulkUpdate("DELETE OauthToken"); } }); }
public JobInstance updateAssignedInfo(final JobInstance jobInstance) { return (JobInstance) transactionTemplate.execute( new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { getSqlMapClientTemplate().update("updateAssignedInfo", jobInstance); updateStateAndResult(jobInstance); return jobInstance; } }); }
public void removeNotificationFilter(final long userId, final long filterId) { transactionTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { User user = userDao.load(userId); user.removeNotificationFilter(filterId); userDao.saveOrUpdate(user); } }); }
public void addNotificationFilter(final long userId, final NotificationFilter filter) { transactionTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { User user = userDao.load(userId); user.addNotificationFilter(filter); synchronized (enableUserMutex) { userDao.saveOrUpdate(user); } } }); }
public MaterialRevision addRevisionsWithModifications( Material material, Modification... modifications) { final MaterialRevision revision = filterUnsaved(new MaterialRevision(material, modifications)); if (revision.getModifications().isEmpty()) { return revision; } transactionTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { materialRepository.saveMaterialRevision(revision); } }); return revision; }
public void disable(final List<String> usersToBeDisabled, LocalizedOperationResult result) { synchronized (disableUserMutex) { if (willDisableAllAdmins(usersToBeDisabled)) { result.badRequest(LocalizedMessage.string("CANNOT_DISABLE_LAST_ADMIN")); return; } transactionTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { userDao.disableUsers(usersToBeDisabled); oauthRepository.deleteUsersOauthGrants(usersToBeDisabled); } }); } }
@Test public void shouldUpdateScmConfigurationOfPluggableScmMaterialsOnPipeline() { String jobName = "job-one"; PipelineConfig pipelineConfig = setupPipelineWithScmMaterial("pipeline_with_pluggable_scm_mat", "stage", jobName); final Pipeline previousSuccessfulBuildWithOlderScmConfig = simulateSuccessfulPipelineRun(pipelineConfig); PipelineConfig updatedPipelineConfig = configHelper.updatePipeline( pipelineConfig.name(), new GoConfigFileHelper.Updater<PipelineConfig>() { @Override public void update(PipelineConfig config) { PluggableSCMMaterialConfig materialConfig = (PluggableSCMMaterialConfig) config.materialConfigs().first(); materialConfig .getSCMConfig() .getConfiguration() .getProperty("password") .setConfigurationValue(new ConfigurationValue("new_value")); } }); final long jobId = rerunJob(jobName, pipelineConfig, previousSuccessfulBuildWithOlderScmConfig); Pipeline loadedPipeline = (Pipeline) transactionTemplate.execute( new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { return loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId); } }); MaterialRevisions revisions = loadedPipeline.getBuildCause().getMaterialRevisions(); Configuration updatedConfiguration = ((PluggableSCMMaterial) revisions .findRevisionFor(updatedPipelineConfig.materialConfigs().first()) .getMaterial()) .getScmConfig() .getConfiguration(); assertThat(updatedConfiguration.size(), is(2)); assertThat( updatedConfiguration.getProperty("password").getConfigurationValue(), is(new ConfigurationValue("new_value"))); }
public void saveRevs(final MaterialRevisions materialRevisions) { final MaterialRevisions unsavedRevisions = new MaterialRevisions(); for (MaterialRevision materialRevision : materialRevisions) { unsavedRevisions.addRevision(filterUnsaved(materialRevision)); } if (unsavedRevisions.isEmpty()) { return; } transactionTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { materialRepository.save(unsavedRevisions); } }); }
public void deleteUsersOauthGrants(final List<String> userIds) { txnTemplate.execute( new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { getHibernateTemplate() .execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { deleteEntitiesByUserIds(OauthAuthorization.class, session, userIds); deleteEntitiesByUserIds(OauthToken.class, session, userIds); return true; } }); } }); }
private Pipeline createAndLoadModifyOneFilePipeline(PipelineConfig pipelineConfig) { MaterialConfigs expandedConfigs = materialExpansionService.expandMaterialConfigsForScheduling( pipelineConfig.materialConfigs()); MaterialRevisions materialRevisions = ModificationsMother.modifyOneFile( MaterialsMother.createMaterialsFromMaterialConfigs(expandedConfigs)); Pipeline building = PipelineMother.buildingWithRevisions(pipelineConfig, materialRevisions); Pipeline pipeline = dbHelper.savePipelineWithMaterials(building); final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId(); return (Pipeline) transactionTemplate.execute( new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { return loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId); } }); }
@Test public void shouldLoadPipelineAlongwithBuildCauseHavingMaterialPasswordsPopulated() { PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig( "last", new StageConfig( new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one")))); pipelineConfig.materialConfigs().clear(); SvnMaterial onDirOne = MaterialsMother.svnMaterial("google.com", "dirOne", "loser", "boozer", false, "**/*.html"); P4Material onDirTwo = MaterialsMother.p4Material( "host:987654321", "zoozer", "secret", "through-the-window", true); onDirTwo.setFolder("dirTwo"); pipelineConfig.addMaterialConfig(onDirOne.config()); pipelineConfig.addMaterialConfig(onDirTwo.config()); configHelper.addPipeline(pipelineConfig); Pipeline building = PipelineMother.building(pipelineConfig); Pipeline pipeline = dbHelper.savePipelineWithMaterials(building); final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId(); Pipeline loadedPipeline = (Pipeline) transactionTemplate.execute( new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { return loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId); } }); MaterialRevisions revisions = loadedPipeline.getBuildCause().getMaterialRevisions(); assertThat( ((SvnMaterial) revisions.findRevisionFor(onDirOne).getMaterial()).getPassword(), is("boozer")); assertThat( ((P4Material) revisions.findRevisionFor(onDirTwo).getMaterial()).getPassword(), is("secret")); }
@Test public void shouldSetAServerHealthMessageWhenMaterialForPipelineWithBuildCauseIsNotFound() throws IllegalArtifactLocationException, IOException { PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig( "last", new StageConfig( new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one")))); pipelineConfig.materialConfigs().clear(); SvnMaterialConfig onDirOne = MaterialConfigsMother.svnMaterialConfig( "google.com", "dirOne", "loser", "boozer", false, "**/*.html"); final P4MaterialConfig onDirTwo = MaterialConfigsMother.p4MaterialConfig( "host:987654321", "zoozer", "secret", "through-the-window", true); onDirTwo.setConfigAttributes(Collections.singletonMap(ScmMaterialConfig.FOLDER, "dirTwo")); pipelineConfig.addMaterialConfig(onDirOne); pipelineConfig.addMaterialConfig(onDirTwo); configHelper.addPipeline(pipelineConfig); Pipeline building = PipelineMother.building(pipelineConfig); final Pipeline pipeline = dbHelper.savePipelineWithMaterials(building); CruiseConfig cruiseConfig = configHelper.currentConfig(); PipelineConfig cfg = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("last")); cfg.removeMaterialConfig(cfg.materialConfigs().get(1)); configHelper.writeConfigFile(cruiseConfig); assertThat( serverHealthService.filterByScope(HealthStateScope.forPipeline("last")).size(), is(0)); final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId(); Date currentTime = new Date(System.currentTimeMillis() - 1); Pipeline loadedPipeline = (Pipeline) transactionTemplate.execute( new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { Pipeline loadedPipeline = null; try { loadedPipeline = loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId); fail( "should not have loaded pipeline with build-cause as one of the necessary materials was not found"); } catch (Exception e) { assertThat(e, is(instanceOf(StaleMaterialsOnBuildCause.class))); assertThat( e.getMessage(), is( "Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config.")); } return loadedPipeline; } }); assertThat(loadedPipeline, is(nullValue())); JobInstance reloadedJobInstance = jobInstanceService.buildById(jobId); assertThat(reloadedJobInstance.getState(), is(JobState.Completed)); assertThat(reloadedJobInstance.getResult(), is(JobResult.Failed)); assertThat( serverHealthService .filterByScope(HealthStateScope.forJob("last", "stage", "job-one")) .size(), is(1)); ServerHealthState error = serverHealthService .filterByScope(HealthStateScope.forJob("last", "stage", "job-one")) .get(0); assertThat( error, is( ServerHealthState.error( "Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config.", "Job for pipeline 'last/" + pipeline.getCounter() + "/stage/1/job-one' has been failed as one or more material configurations were either changed or removed.", HealthStateType.general(HealthStateScope.forJob("last", "stage", "job-one"))))); DateTime expiryTime = (DateTime) ReflectionUtil.getField(error, "expiryTime"); assertThat(expiryTime.toDate().after(currentTime), is(true)); assertThat( expiryTime.toDate().before(new Date(System.currentTimeMillis() + 5 * 60 * 1000 + 1)), is(true)); String logText = FileUtil.readToEnd(consoleService.findConsoleArtifact(reloadedJobInstance.getIdentifier())); assertThat( logText, containsString( "Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config.")); assertThat( logText, containsString( "Job for pipeline 'last/" + pipeline.getCounter() + "/stage/1/job-one' has been failed as one or more material configurations were either changed or removed.")); }