use of org.craftercms.studio.api.v1.exception.ServiceLayerException in project studio by craftercms.
the class StudioPublisherTask method doPublishing.
private void doPublishing(String siteId, List<PublishRequest> itemsToDeploy, String environment) throws DeploymentException, ServiceLayerException {
String statusMessage;
String status;
String author = itemsToDeploy.get(0).getUser();
StringBuilder sbComment = new StringBuilder();
List<DeploymentItemTO> completeDeploymentItemList = new ArrayList<DeploymentItemTO>();
Set<String> processedPaths = new HashSet<String>();
SimpleDateFormat sdf = new SimpleDateFormat(StudioConstants.DATE_PATTERN_WORKFLOW_WITH_TZ);
String currentPackageId = StringUtils.EMPTY;
try {
logger.debug("Mark items as processing for site \"{0}\"", siteId);
Set<String> packageIds = new HashSet<String>();
int idx = 0;
for (PublishRequest item : itemsToDeploy) {
idx++;
if (!StringUtils.equals(currentPackageId, item.getPackageId())) {
currentPackageId = item.getPackageId();
}
statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_PUBLISHING);
statusMessage = statusMessage.replace("{package_id}", currentPackageId).replace("{datetime}", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(sdf.toPattern()))).replace("{x}", Integer.toString(idx)).replace("{y}", Integer.toString(itemsToDeploy.size()));
siteService.updatePublishingStatusMessage(siteId, PUBLISHING, statusMessage);
processPublishingRequest(siteId, environment, item, completeDeploymentItemList, processedPaths);
if (packageIds.add(item.getPackageId())) {
sbComment.append(item.getSubmissionComment()).append("\n");
}
}
deploy(siteId, environment, completeDeploymentItemList, author, sbComment.toString());
StringBuilder sbPackIds = new StringBuilder("Package(s): ");
for (String packageId : packageIds) {
sbPackIds.append(packageId).append(";");
}
generateWorkflowActivity(siteId, environment, packageIds, author, OPERATION_PUBLISHED);
publishingManager.markItemsCompleted(siteId, environment, itemsToDeploy);
logger.debug("Mark deployment completed for processed items for site \"{0}\"", siteId);
logger.info("Finished publishing environment " + environment + " for site " + siteId);
if (publishingManager.isPublishingQueueEmpty(siteId)) {
status = READY;
statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_READY);
statusMessage = statusMessage.replace("{package_id}", currentPackageId).replace("{datetime}", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(sdf.toPattern()))).replace("{package_size}", Integer.toString(itemsToDeploy.size()));
} else {
status = QUEUED;
statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_QUEUED);
}
siteService.updatePublishingStatusMessage(siteId, status, statusMessage);
} catch (DeploymentException err) {
logger.error("Error while executing deployment to environment store " + "for site \"{0}\", number of items \"{1}\"", err, siteId, itemsToDeploy.size());
publishingManager.markItemsReady(siteId, environment, itemsToDeploy);
siteService.enablePublishing(siteId, false);
statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_ERROR);
siteService.updatePublishingStatusMessage(siteId, ERROR, statusMessage);
throw err;
} catch (Exception err) {
logger.error("Unexpected error while executing deployment to environment " + "store for site \"{0}\", number of items \"{1}\"", err, siteId, itemsToDeploy.size());
publishingManager.markItemsReady(siteId, environment, itemsToDeploy);
siteService.enablePublishing(siteId, false);
statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_ERROR);
siteService.updatePublishingStatusMessage(siteId, ERROR, statusMessage);
throw err;
}
}
use of org.craftercms.studio.api.v1.exception.ServiceLayerException in project studio by craftercms.
the class StudioClusterGlobalRepoSyncTask method updateContent.
protected void updateContent(List<ClusterMember> clusterNodes) throws IOException, CryptoException, ServiceLayerException {
logger.debug("Update global repo");
Path siteSandboxPath = Paths.get(studioConfiguration.getProperty(StudioConfiguration.REPO_BASE_PATH), studioConfiguration.getProperty(StudioConfiguration.GLOBAL_REPO_PATH)).resolve(GIT_ROOT);
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(siteSandboxPath.toFile()).readEnvironment().findGitDir().build();
try (Git git = new Git(repo)) {
logger.debug("Update content from each active cluster memeber");
for (ClusterMember remoteNode : clusterNodes) {
updateBranch(git, remoteNode);
}
} catch (GitAPIException e) {
logger.error("Error while syncing cluster node global repo content", e);
}
}
use of org.craftercms.studio.api.v1.exception.ServiceLayerException in project studio by craftercms.
the class StudioClusterPublishedRepoSyncTask method updateContent.
protected void updateContent(long sId, String siteId, List<ClusterMember> clusterNodes, List<ClusterSiteRecord> clusterSiteRecords) throws IOException, CryptoException, ServiceLayerException {
logger.debug("Update published repo for site " + siteId);
Path siteSandboxPath = buildRepoPath(siteId).resolve(GIT_ROOT);
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(siteSandboxPath.toFile()).readEnvironment().findGitDir().build();
String gitLockKey = SITE_PUBLISHED_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
logger.debug("Git Lock Key: " + gitLockKey);
try (Git git = new Git(repo)) {
Set<String> environments = getAllPublishingEnvironments(siteId);
logger.debug("Update published repo from all active cluster members");
if (generalLockService.tryLock(gitLockKey)) {
try {
for (ClusterMember remoteNode : clusterNodes) {
ClusterSiteRecord csr = clusterDao.getClusterSiteRecord(remoteNode.getId(), sId);
if (Objects.nonNull(csr) && csr.getPublishedRepoCreated() > 0) {
try {
logger.debug("Fetch from cluster member " + remoteNode.getLocalAddress());
final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
FetchCommand fetch = git.fetch().setRemote(remoteNode.getGitRemoteName());
fetch = studioClusterUtils.configureAuthenticationForCommand(remoteNode, fetch, tempKey);
fetch.call();
Files.delete(tempKey);
} catch (GitAPIException e) {
logger.error("Error while fetching published repo for site " + siteId + " from remote " + remoteNode.getGitRemoteName());
logger.error(e.getMessage());
}
}
}
for (String branch : environments) {
for (ClusterMember remoteNode : clusterNodes) {
ClusterSiteRecord csr = clusterDao.getClusterSiteRecord(remoteNode.getId(), sId);
if (Objects.nonNull(csr) && csr.getPublishedRepoCreated() > 0) {
try {
updatePublishedBranch(siteId, git, remoteNode, branch);
} catch (GitAPIException e) {
logger.error("Error while updating published repo for site " + siteId + " from remote " + remoteNode.getGitRemoteName() + " environment " + branch);
logger.error(e.getMessage());
}
}
}
}
} finally {
generalLockService.unlock(gitLockKey);
}
} else {
logger.debug("Failed to get lock " + gitLockKey);
}
}
}
use of org.craftercms.studio.api.v1.exception.ServiceLayerException in project studio by craftercms.
the class StudioClusterPublishedRepoSyncTask method updatePublishedBranch.
private void updatePublishedBranch(String siteId, Git git, ClusterMember remoteNode, String branch) throws CryptoException, GitAPIException, IOException, ServiceLayerException {
logger.debug("Update published environment " + branch + " from " + remoteNode.getLocalAddress() + " for site " + siteId);
final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
Repository repo = git.getRepository();
Ref ref = repo.exactRef(Constants.R_HEADS + branch);
boolean createBranch = (ref == null);
logger.debug("Checkout " + branch);
CheckoutCommand checkoutCommand = git.checkout().setName(branch).setCreateBranch(createBranch);
if (createBranch) {
checkoutCommand.setStartPoint(remoteNode.getGitRemoteName() + "/" + branch);
}
checkoutCommand.call();
PullCommand pullCommand = git.pull();
pullCommand.setRemote(remoteNode.getGitRemoteName());
pullCommand.setRemoteBranchName(branch);
pullCommand = studioClusterUtils.configureAuthenticationForCommand(remoteNode, pullCommand, tempKey);
pullCommand.call();
Files.delete(tempKey);
}
use of org.craftercms.studio.api.v1.exception.ServiceLayerException in project studio by craftercms.
the class StudioClusterPublishedRepoSyncTask method executeInternal.
@Override
protected void executeInternal(String siteId) {
// Log start time
long startTime = System.currentTimeMillis();
logger.debug("Worker starts syncing cluster node published for site " + siteId);
try {
HierarchicalConfiguration<ImmutableNode> registrationData = studioClusterUtils.getClusterConfiguration();
if (registrationData != null && !registrationData.isEmpty()) {
String localAddress = studioClusterUtils.getClusterNodeLocalAddress();
ClusterMember localNode = clusterDao.getMemberByLocalAddress(localAddress);
List<ClusterMember> clusterNodes = studioClusterUtils.getClusterNodes(localAddress);
SiteFeed siteFeed = siteService.getSite(siteId);
List<ClusterSiteRecord> clusterSiteRecords = clusterDao.getSiteStateAcrossCluster(siteId);
Optional<ClusterSiteRecord> localNodeRecord = clusterSiteRecords.stream().filter(x -> x.getClusterNodeId() == localNode.getId() && StringUtils.equals(x.getState(), STATE_CREATED)).findFirst();
if (!localNodeRecord.isPresent()) {
return;
}
long nodesCreated = clusterSiteRecords.stream().filter(x -> StringUtils.equals(x.getState(), STATE_CREATED)).count();
if (nodesCreated < 1) {
return;
}
// Check if site exists
logger.debug("Check if site " + siteId + " exists in local repository");
boolean success = true;
int publishedReposCreated = clusterSiteRecords.stream().mapToInt(ClusterSiteRecord::getPublishedRepoCreated).sum();
if (publishedReposCreated > 0 || siteFeed.getPublishedRepoCreated() > 0) {
boolean siteCheck = checkIfSiteRepoExists(siteId);
if (!siteCheck) {
// Site doesn't exist locally, create it
success = createSite(localNode.getId(), siteFeed.getId(), siteId, siteFeed.getSandboxBranch());
} else {
clusterDao.setPublishedRepoCreated(localNode.getId(), siteFeed.getId());
}
} else {
success = false;
}
if (success) {
try {
// Add the remote repositories to the local repository to sync from if not added already
logger.debug("Add remotes for site " + siteId);
addRemotes(siteId, clusterNodes);
} catch (InvalidRemoteUrlException | ServiceLayerException | CryptoException e) {
logger.error("Error while adding remotes on cluster node for site " + siteId);
}
try {
// Sync with remote and update the local cache with the last commit ID to speed things up
logger.debug("Update content for site " + siteId);
updateContent(siteFeed.getId(), siteId, clusterNodes, clusterSiteRecords);
} catch (IOException | CryptoException | ServiceLayerException e) {
logger.error("Error while updating content for site " + siteId + " on cluster node.", e);
}
}
}
} catch (SiteNotFoundException e) {
logger.error("Error while executing Cluster Node Sync Published for site " + siteId, e);
}
// Compute execution duration and log it
long duration = System.currentTimeMillis() - startTime;
logger.debug("Worker finished syncing cluster node for site " + siteId);
logger.debug("Worker performed cluster node sync for site " + siteId + " in " + duration + "ms");
logger.debug("Finished Cluster Node Sync task for site " + siteId);
}
Aggregations