How to know if asset was replicated to preview or publish instance | Community
Skip to main content
Ankan_Ghosh
Level 3
September 12, 2024
Solved

How to know if asset was replicated to preview or publish instance

  • September 12, 2024
  • 5 replies
  • 1661 views

I have a requirement to know after an asset is replicated whether it was replicated to the preview instance or the publish instance. Anyone has any idea how can I get this inside the Event handler?

The code below was implemented for another purpose. How can I implement the above-mentioned requirement in this?

 

@9944223 public void handleEvent(Event event) { processEventIfNeeded(event); } private void processEventIfNeeded(Event event) { ReplicationAction replicationAction = ReplicationAction.fromEvent(event); String eventType = replicationAction.getType().getName(); String eventPath = replicationAction.getPath(); if ((eventType.equals(REPLICATIONACTION_ACTIVATE) || eventType.equals(REPLICATIONACTION_DEACTIVATE)) && eventPath.startsWith("/content/dam")) { processAssets(eventPath, eventType); } }

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Ankan_Ghosh
logger.info("START: Block one for replication agent check"); ReplicationStatus afterStatus = resource.adaptTo(ReplicationStatus.class); ReplicationStatus previewStatus = afterStatus.getStatusForAgent("preview"); ReplicationStatus publisherStatus = afterStatus.getStatusForAgent("publish"); if(previewStatus!=null){ logger.info("Asset {} is replicated to preview instance {}", assetPath, previewStatus.isActivated()); } if(publisherStatus!=null) { logger.info("Asset {} is replicated to publish instance {}", assetPath, publisherStatus.isActivated()); } logger.info("END: Block one for replication agent check"); logger.info("START: Block two for replication agent check"); List<String> agentIds = (List<String>) event.getProperty("agentIds"); logger.info("Asset {} is replicated to preview instance {}", assetPath, agentIds.contains("preview")); logger.info("Asset {} is replicated to publish instance {}", assetPath, agentIds.contains("publish")); logger.info("END: Block two for replication agent check");

 

In the above mentioned code I tried two approached to identify the replication agent. The first block does not work as expected. It basically tells the current replication status of the resource for a certain instance ( preview/publisher). I needed to know during the replication trigger which instance it was triggered for. The second block does the exact thing (verified from the logs in the dev environment). So the code that worked for me is 

List<String> agentIds = (List<String>) event.getProperty("agentIds"); boolean isPreviewInstance = agentIds.contains("preview");

5 replies

arunpatidar
Community Advisor
Community Advisor
September 12, 2024

Hi @ankan_ghosh 
Please check https://experienceleague.adobe.com/en/docs/experience-manager-cloud-service/content/operations/replication#replication-api 

 

ReplicationStatus afterStatus = enResource.adaptTo(ReplicationStatus.class); // afterStatus.isActivated == false ReplicationStatus previewStatus = afterStatus.getStatusForAgent(PREVIEW_AGENT); // previewStatus.isActivated == true
Arun Patidar
Harwinder-singh
Community Advisor
Community Advisor
September 12, 2024
Ankan_Ghosh
Level 3
September 12, 2024

The code I wrote based on the article you provided works well on my local setup, but will it function properly in the cloud environment?

List<String> agentIds = (List<String>) event.getProperty("agentIds"); logger.info("It is published to the PUBLISH isntance: {}", agentIds.contains("publish"));

 

pulkitvashisth
Community Advisor
Community Advisor
September 12, 2024

@ankan_ghosh For your above usecase , you can assign the replication of assets task from your Sling Event Handler to a Sling Job


The job returns a status of the job on completion. That way you can check if the replication operation was carried out successfully.

 

Also as @arunpatidar mentioned, you can directly check for an asset using ReplicationStatus api.

Ankan_Ghosh
Level 3
September 13, 2024

Hey @pulkitvashisth I have already used some jobs; my concern is to perform two different jobs based on which instance it was replicated. If the CF or any other asset is replicated to the preview instance, I need to perform a different job than if it is replicated to the publish instance. I am sorry if there has been any confusion in my question. 

 

And yes, @arunpatidar 's solution gives me the replication status of the resource. But even with this solution how will I know if the current replication Event was triggered for publish instance or preview? 

 

ReplicationStatus publishStatus = afterStatus.getStatusForAgent("publish"); // true on local ReplicationStatus previewStatus = afterStatus.getStatusForAgent("preview"); // not true

 

This line returns true if the resource is published. Now, if the user replicates the resource to both the instance, it should return true for both. Now some modification happens and they decide to replicate it to preview, before pushing to publish. How will I know inside the event handler that this time the replication happened to preview and not publish? I need to index the data to a different source in the 3rd party search provider if the current replication was done to the preview instance than in case of publish instance. I am trying to understand if this is even possible. 

 

arunpatidar
Community Advisor
Community Advisor
September 13, 2024

Hi @ankan_ghosh 
There are jcr properties, which will give you the hint, what publish where and when and you can check based on that of replication happend on preview first or publish.

Arun Patidar
kautuk_sahni
Community Manager
Community Manager
September 13, 2024

@ankan_ghosh Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni
Ankan_Ghosh
Ankan_GhoshAuthorAccepted solution
Level 3
September 25, 2024
logger.info("START: Block one for replication agent check"); ReplicationStatus afterStatus = resource.adaptTo(ReplicationStatus.class); ReplicationStatus previewStatus = afterStatus.getStatusForAgent("preview"); ReplicationStatus publisherStatus = afterStatus.getStatusForAgent("publish"); if(previewStatus!=null){ logger.info("Asset {} is replicated to preview instance {}", assetPath, previewStatus.isActivated()); } if(publisherStatus!=null) { logger.info("Asset {} is replicated to publish instance {}", assetPath, publisherStatus.isActivated()); } logger.info("END: Block one for replication agent check"); logger.info("START: Block two for replication agent check"); List<String> agentIds = (List<String>) event.getProperty("agentIds"); logger.info("Asset {} is replicated to preview instance {}", assetPath, agentIds.contains("preview")); logger.info("Asset {} is replicated to publish instance {}", assetPath, agentIds.contains("publish")); logger.info("END: Block two for replication agent check");

 

In the above mentioned code I tried two approached to identify the replication agent. The first block does not work as expected. It basically tells the current replication status of the resource for a certain instance ( preview/publisher). I needed to know during the replication trigger which instance it was triggered for. The second block does the exact thing (verified from the logs in the dev environment). So the code that worked for me is 

List<String> agentIds = (List<String>) event.getProperty("agentIds"); boolean isPreviewInstance = agentIds.contains("preview");