private void clearCachedCompletedStageFeeds(String pipelineName) { String key = cacheKeyForLatestStageFeedForPipeline(pipelineName); synchronized (key) { goCache.remove(key); } }
public synchronized void cancelStage(final Stage stage, String username) { cancel(stage, username); notifyStageStatusChangeListeners(stage); //Send a notification only if none of the jobs are assigned. // If any of the jobs are assigned. JobStatusListener.onMessage will send the stage cancel notification. transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override public void afterCommit() { JobInstances jobInstances = stage.getJobInstances(); boolean noJobIsAssigned = jobInstances.stream().noneMatch(JobInstance::isAssignedToAgent); if (noJobIsAssigned) { stageStatusTopic.post(new StageStatusMessage(stage.getIdentifier(), stage.stageState(), stage.getResult(), SessionUtils.currentUsername())); } } }); }
@Override public void doInTransactionWithoutResult(TransactionStatus status) { stageService.cancelStage(stage, user); } });
private Optional<Stage> getStageFromRequestParam(Request request, HttpOperationResult operationResult) { String pipelineName = request.params("pipeline_name"); String pipelineCounter = request.params("pipeline_counter"); String stageName = request.params("stage_name"); String stageCounter = request.params("stage_counter"); Stage stage = stageService.findStageWithIdentifier(pipelineName, Integer.parseInt(pipelineCounter), stageName, stageCounter, currentUsername().getUsername().toString(), operationResult); if (!operationResult.isSuccess()) { return Optional.empty(); } if (stage == null || stage instanceof NullStage) { String message = String.format("Stage '%s' with counter '%s' not found. Please make sure specified stage or stage run with specified counter exists.", stageName, stageCounter); operationResult.notFound("Not Found", message, HealthStateType.general(HealthStateScope.GLOBAL)); return Optional.empty(); } return Optional.ofNullable(stage); }
public FeedEntries feed(String pipelineName, Username username) { String key = cacheKeyForLatestStageFeedForPipeline(pipelineName); List<StageFeedEntry> feedEntries = (List<StageFeedEntry>) goCache.get(key); if (feedEntries == null) { synchronized (key) { feedEntries = (List<StageFeedEntry>) goCache.get(key);//Double check locking is done because the query is expensive (takes about 2 seconds) if (feedEntries == null) { feedEntries = stageDao.findCompletedStagesFor(pipelineName, FeedModifier.Latest, -1, FEED_PAGE_SIZE); populateAuthorsAndMingleCards(feedEntries, pipelineName, username); goCache.put(key, feedEntries); } } } return cloner.deepClone(new FeedEntries(new ArrayList<>(feedEntries))); }
/** * @deprecated don't call this directly, go through ScheduleService.failJob so that stageLevel synchronization is done */ public void failJob(final JobInstance jobInstance) { changeJob(() -> jobInstanceService.failJob(jobInstance), jobInstance.getIdentifier()); }
public FeedEntries feed(Username user, LocalizedOperationResult operationResult) { if (userDoesNotHaveViewPermission(user, operationResult)) { return null; } return stageService.feed(pipelineName, user); }
public FeedEntries feedBefore(Username user, long entryId, LocalizedOperationResult operationResult) { if (userDoesNotHaveViewPermission(user, operationResult)) { return null; } return stageService.feedBefore(entryId, pipelineName, user); }
public void failJob(JobInstance instance) { synchronized (mutexForStageInstance(instance.getIdentifier())) { stageService.failJob(instance); } }
public void cancelJob(final JobInstance instance) { synchronized (mutexForStageInstance(instance.getIdentifier())) { stageService.cancelJob(instance); } }
@Override public void initialize() { goConfigService.register(this); goConfigService.register(pipelineConfigChangedListener()); goConfigService.register(securityConfigChangeListener()); goConfigService.register(templateConfigChangedListener()); stageService.addStageStatusListener(stageStatusChangedListener()); pipelinePauseService.registerListener(this); pipelineLockService.registerListener(this); }
public void sendNotifications(StageIdentifier stageIdentifier, StageEvent event, Username cancelledBy) { Users users = userService.findValidSubscribers(stageIdentifier.stageConfigIdentifier()); if (users.isEmpty()) { return; } Stage stage = stageService.findStageWithIdentifier(stageIdentifier); Pipeline pipeline = pipelineService.fullPipelineById(stage.getPipelineId()); MaterialRevisions materialRevisions = pipeline.getMaterialRevisions(); String emailBody = new EmailBodyGenerator(materialRevisions, cancelledBy, systemEnvironment, stageIdentifier).getContent(); String subject = "Stage [" + stageIdentifier.stageLocator() + "]" + event.describe(); LOGGER.debug("Processing notification titled [{}]", subject); for (User user : users) { if (user.matchNotification(stageIdentifier.stageConfigIdentifier(), event, materialRevisions)) { StringBuilder emailWithSignature = new StringBuilder(emailBody) .append("\n\n") .append("Sent by Go on behalf of ") .append(user.getName()); SendEmailMessage sendEmailMessage = new SendEmailMessage(subject, emailWithSignature.toString(), user.getEmail()); emailNotificationTopic.post(sendEmailMessage); } } LOGGER.debug("Finished processing notification titled [{}]", subject); }
/** * @deprecated don't call this directly, go through ScheduleService.cancelJob so that stageLevel synchronization is done */ public void cancelJob(final JobInstance jobInstance) { changeJob(() -> jobInstanceService.cancelJob(jobInstance), jobInstance.getIdentifier()); }
public String instanceByCounter(Request req, Response res) throws IOException { String pipelineName = req.params("pipeline_name"); String pipelineCounter = req.params("pipeline_counter"); String stageName = req.params("stage_name"); String stageCounter = req.params("stage_counter"); HttpOperationResult result = new HttpOperationResult(); Stage stageModel = stageService.findStageWithIdentifier(pipelineName, Integer.parseInt(pipelineCounter), stageName, stageCounter, currentUsername().getUsername().toString(), result); if (result.canContinue()) { return writerForTopLevelObject(req, res, writer -> StageRepresenter.toJSON(writer, stageModel)); } else { return renderHTTPOperationResult(result, req, res); } }
public Stage findStageWithIdentifier(String pipelineName, int pipelineCounter, String stageName, String stageCounter, String username, OperationResult result) { if (!goConfigService.currentCruiseConfig().hasPipelineNamed(new CaseInsensitiveString(pipelineName))) { String message = String.format("Pipeline '%s' not found", pipelineName); result.notFound("Not Found", message, HealthStateType.general(HealthStateScope.GLOBAL)); return null; } if (!securityService.hasViewPermissionForPipeline(Username.valueOf(username), pipelineName)) { result.forbidden("Unauthorized", NOT_AUTHORIZED_TO_VIEW_PIPELINE, HealthStateType.general(HealthStateScope.forPipeline(pipelineName))); return null; } Pipeline pipeline = pipelineDao.findPipelineByNameAndCounter(pipelineName, pipelineCounter); if (pipeline == null) { String message = String.format("Pipeline '%s' with counter '%s' not found", pipelineName, pipelineCounter); result.notFound("Not Found", message, HealthStateType.general(HealthStateScope.GLOBAL)); return null; } return findStageWithIdentifier(new StageIdentifier(pipelineName, pipelineCounter, stageName, stageCounter)); }
public void onMessage(final JobStatusMessage message) { if (message.getJobState().isCompleted()) { final Stage stage = stageService.findStageWithIdentifier(message.getStageIdentifier()); if (stage.isCompleted()) { stageStatusTopic.post(new StageStatusMessage(message.getStageIdentifier(), stage.stageState(), stage.getResult())); } elasticAgentPluginService.jobCompleted(stage.findJob(message.getJobIdentifier().getBuildName())); } } }