Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Rollout and activate" action #19

Merged
merged 16 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.exadel.etoolbox.rolloutmanager.core.models;

public class RolloutItem {
private String master;
private String target;
private int depth;
boolean autoRolloutTrigger;

public String getMaster() {
return master;
}

public String getTarget() {
return target;
}

public int getDepth() {
return depth;
}

public boolean isAutoRolloutTrigger() {
return autoRolloutTrigger;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.exadel.etoolbox.rolloutmanager.core.models;

public class RolloutStatus {
private boolean isSuccess;
private final String target;

public RolloutStatus(String target) {
this.target = target;
}

public boolean isSuccess() {
return isSuccess;
}

public void setSuccess(boolean success) {
isSuccess = success;
}

public String getTarget() {
return target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.exadel.etoolbox.rolloutmanager.core.services;

import com.day.cq.wcm.api.PageManager;
import com.exadel.etoolbox.rolloutmanager.core.models.RolloutItem;
import com.exadel.etoolbox.rolloutmanager.core.models.RolloutStatus;
import org.apache.sling.api.resource.ResourceResolver;

import java.util.List;

/**
* Provides methods for checking if a live relationship can be synchronized with a blueprint in scope of usage
* the rollout manager tool.
*/
public interface PageReplicationService {
List<RolloutStatus> replicateItems(ResourceResolver resourceResolver, RolloutItem[] items, PageManager pageManager);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.exadel.etoolbox.rolloutmanager.core.services.impl;

import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.exadel.etoolbox.rolloutmanager.core.models.RolloutItem;
import com.exadel.etoolbox.rolloutmanager.core.models.RolloutStatus;
import com.exadel.etoolbox.rolloutmanager.core.services.PageReplicationService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.event.jobs.JobManager;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Session;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component(service = PageReplicationService.class)
@Designate(ocd = PageReplicationServiceImpl.Configuration.class)
public class PageReplicationServiceImpl implements PageReplicationService {
private static final Logger LOG = LoggerFactory.getLogger(PageReplicationServiceImpl.class);

@ObjectClassDefinition(name = "EToolbox Page Replication Service Configuration")
@interface Configuration {

@AttributeDefinition(
name = "Pool size",
description = "The number of Threads in the pool")
int poolSize() default 5;
}

@Activate
private PageReplicationServiceImpl.Configuration config;

@Reference
private LiveRelationshipManager liveRelationshipManager;

@Reference
private JobManager jobManager;

@Reference
private Replicator replicator;

public List<RolloutStatus> replicateItems(ResourceResolver resourceResolver, RolloutItem[] items, PageManager pageManager) {
return Arrays.stream(items)
.collect(Collectors.groupingBy(RolloutItem::getDepth))
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.map(Map.Entry::getValue)
.flatMap(sortedByDepthItems -> replicateSortedByDepthItems(resourceResolver, sortedByDepthItems, pageManager))
.collect(Collectors.toList());
}

private Stream<RolloutStatus> replicateSortedByDepthItems(ResourceResolver resourceResolver, List<RolloutItem> items, PageManager pageManager) {
ExecutorService executorService = Executors.newFixedThreadPool(config.poolSize());
return items.stream()
.filter(item -> StringUtils.isNotBlank(item.getTarget()))
.map(item -> CompletableFuture.supplyAsync(() -> replicate(resourceResolver, item, pageManager), executorService))
.collect(Collectors.toList())
.stream()
.map(CompletableFuture::join);
}

private RolloutStatus replicate(ResourceResolver resourceResolver, RolloutItem targetItem, PageManager pageManager) {

String targetPath = targetItem.getTarget();
RolloutStatus status = new RolloutStatus(targetPath);

Optional<Page> targetPage = Optional.ofNullable(pageManager.getPage(targetPath));
Session session = resourceResolver.adaptTo(Session.class);
if (!targetPage.isPresent() || ObjectUtils.isEmpty(session)) {
status.setSuccess(false);
LOG.warn("Replication failed - target page is null, page path: {}", targetPath);
return status;
}
try {
replicator.replicate(session, ReplicationActionType.ACTIVATE, targetPath);
} catch (ReplicationException ex) {
status.setSuccess(false);
LOG.error("Exception during page replication", ex);
}
status.setSuccess(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗ Need to move this inside try, or else the status success will be reset to "true" after set to "false"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class CollectLiveCopiesServlet extends SlingAllMethodsServlet {
private static final String IS_NEW_JSON_FIELD = "isNew";
private static final String HAS_ROLLOUT_TRIGGER_JSON_FIELD = "autoRolloutTrigger";
private static final String LAST_ROLLED_OUT_JSON_FIELD = "lastRolledOut";
private static final String IS_DISABLED_JSON_FIELD = "disabled";

@Reference
private transient LiveRelationshipManager liveRelationshipManager;
Expand Down Expand Up @@ -137,11 +138,10 @@ private JsonObject relationshipToJson(LiveRelationship relationship,
String targetPath = buildTargetPath(relationship, syncPath);

LiveCopy liveCopy = relationship.getLiveCopy();
if (liveCopy == null
|| (StringUtils.isNotBlank(syncPath) && !liveCopy.isDeep())
|| !relationshipCheckerService.isAvailableForSync(syncPath, targetPath, liveCopy.getExclusions(), resourceResolver)) {
if (liveCopy == null || (StringUtils.isNotBlank(syncPath) && !liveCopy.isDeep())) {
return JsonValue.EMPTY_JSON_OBJECT;
}
boolean isDisabled = !relationshipCheckerService.isAvailableForSync(syncPath, targetPath, liveCopy.getExclusions(), resourceResolver);

String liveCopyPath = liveCopy.getPath();
boolean isNew = !resourceExists(resourceResolver, liveCopyPath + syncPath);
Expand All @@ -154,6 +154,7 @@ private JsonObject relationshipToJson(LiveRelationship relationship,
.add(IS_NEW_JSON_FIELD, isNew)
.add(HAS_ROLLOUT_TRIGGER_JSON_FIELD, !isNew && hasAutoTrigger(liveCopy))
.add(LAST_ROLLED_OUT_JSON_FIELD, getStringDate(resourceResolver, liveCopyPath + syncPath))
.add(IS_DISABLED_JSON_FIELD, isDisabled)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.exadel.etoolbox.rolloutmanager.core.models.RolloutItem;
import com.exadel.etoolbox.rolloutmanager.core.models.RolloutStatus;
import com.exadel.etoolbox.rolloutmanager.core.services.PageReplicationService;
import com.exadel.etoolbox.rolloutmanager.core.servlets.util.ServletUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.CollectionUtils;
Expand All @@ -39,6 +42,7 @@
import javax.json.Json;
import javax.servlet.Servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -67,11 +71,15 @@ public class RolloutServlet extends SlingAllMethodsServlet {

private static final String SELECTION_JSON_ARRAY_PARAM = "selectionJsonArray";
private static final String IS_DEEP_ROLLOUT_PARAM = "isDeepRollout";
private static final String SHOULD_ACTIVATE_PARAM = "shouldActivate";
private static final String FAILED_TARGETS_RESPONSE_PARAM = "failedTargets";

@Reference
private transient RolloutManager rolloutManager;

@Reference
private transient PageReplicationService pageReplicationService;

@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) {
StopWatch sw = StopWatch.createStarted();
Expand Down Expand Up @@ -103,7 +111,16 @@ protected void doPost(final SlingHttpServletRequest request, final SlingHttpServ
LOG.debug("Is deep rollout (include subpages): {}", isDeepRollout);

List<RolloutStatus> rolloutStatuses = doItemsRollout(rolloutItems, pageManager, isDeepRollout);
writeStatusesIfFailed(rolloutStatuses, response);

boolean shouldActivate = ServletUtil.getRequestParamBoolean(request, SHOULD_ACTIVATE_PARAM);
LOG.debug("Should activate pages: {}", shouldActivate);
List<RolloutStatus> activationStatuses = new ArrayList<>();
if (shouldActivate) {
activationStatuses = pageReplicationService.replicateItems(request.getResourceResolver(), rolloutItems, request.getResourceResolver().adaptTo(PageManager.class));
}

writeStatusesIfFailed(Stream.concat(rolloutStatuses.stream(), activationStatuses.stream())
.collect(Collectors.toList()), response);
LOG.debug("Rollout of selected items is completed in {} ms", sw.getTime(TimeUnit.MILLISECONDS));
}

Expand Down Expand Up @@ -192,48 +209,4 @@ private RolloutItem[] jsonArrayToRolloutItems(String jsonArray) {
}
return new RolloutItem[0];
}

private static class RolloutItem {
private String master;
private String target;
private int depth;
boolean autoRolloutTrigger;

public String getMaster() {
return master;
}

public String getTarget() {
return target;
}

public int getDepth() {
return depth;
}

public boolean isAutoRolloutTrigger() {
return autoRolloutTrigger;
}
}

private static class RolloutStatus {
private boolean isSuccess;
private final String target;

public RolloutStatus(String target) {
this.target = target;
}

public boolean isSuccess() {
return isSuccess;
}

public void setSuccess(boolean success) {
isSuccess = success;
}

public String getTarget() {
return target;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class CollectLiveCopiesServletTest {
private static final String EXPECTED_RESPONSE_JSON =
"src/test/resources/com/exadel/etoolbox/rolloutmanager/core/servlets/collect-expected-items.json";

private static final String EXPECTED_EMPTY_RESPONSE_JSON =
"src/test/resources/com/exadel/etoolbox/rolloutmanager/core/servlets/collect-expected-items-with-no-valid-live-copy.json";

private final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

@Mock
Expand Down Expand Up @@ -142,12 +145,13 @@ void doPost_RelationshipExcludeChildren_EmptyArrayResponse() throws WCMException
}

@Test
void doPost_NotAvailableForRollout_EmptyArrayResponse() throws WCMException {
void doPost_NotAvailableForRollout_EmptyResponse() throws WCMException, IOException {
createSourceResource();

LiveRelationship relationship = mockSingleLiveRelationship(TEST_SOURCE_PATH);

LiveCopy liveCopy = mock(LiveCopy.class);
when(liveCopy.getPath()).thenReturn(TEST_LIVE_COPY_PATH);
when(relationship.getLiveCopy()).thenReturn(liveCopy);
when(relationship.getSyncPath()).thenReturn(TEST_SYNC_PATH);

Expand All @@ -156,7 +160,10 @@ void doPost_NotAvailableForRollout_EmptyArrayResponse() throws WCMException {
.thenReturn(false);

fixture.doPost(request, response);
assertEquals(JsonValue.EMPTY_JSON_ARRAY.toString(), response.getOutputAsString());

String expected = new String(Files.readAllBytes(Paths.get(EXPECTED_EMPTY_RESPONSE_JSON)))
.replaceAll("(\\r|\\n|\\t|\\s)", StringUtils.EMPTY);
assertEquals(expected, response.getOutputAsString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"master": "/content/my-site/language-masters/en/testResource",
"path": "/content/my-site/fr/en/testResource",
"depth": 0,
"liveCopies": [],
"isNew": true,
"autoRolloutTrigger": false,
"lastRolledOut": "",
"disabled": true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"liveCopies": [],
"isNew": true,
"autoRolloutTrigger": false,
"lastRolledOut":""
"lastRolledOut": "",
"disabled": false
}
],
"isNew": false,
"autoRolloutTrigger": false,
"lastRolledOut":""
"lastRolledOut": "",
"disabled": false
}
]
Loading
Loading