AEM as Cloud Service - Sling Jobs Triggering Twice | Community
Skip to main content
Asutosh_Jena_
Community Advisor
Community Advisor
December 26, 2022

AEM as Cloud Service - Sling Jobs Triggering Twice

  • December 26, 2022
  • 8 replies
  • 5698 views

Hi All,

 

I have a use case for executing sling jobs only on one of the publish instance (I want this to run only once). But it looks like the job is getting completed on all the publish instances, i.e., twice.

 

In Sling Job I do not see an option for running it on CLUSTER like how we have in Scheduler.

 

Any pointers?

 

@Component(
immediate = true,
service = JobConsumer.class,
property = {
JobConsumer.PROPERTY_TOPICS + "=aem/something"
}
)
@Designate(ocd = ReportGenerationConfig.class)
public class ReportGenerationJobConsumer implements JobConsumer {
// Some code
}

 

Thanks,

Asutosh

8 replies

joerghoh
Adobe Employee
Adobe Employee
December 26, 2022

Hi,

 

this is not possible, because the publish instances do not know each other. Also depending on the type of deployment their number can vary. In would always recommend to execute such activities on author whenever possible.

Asutosh_Jena_
Community Advisor
Community Advisor
December 26, 2022

But here the expectation is to run this on publish instance.

joerghoh
Adobe Employee
Adobe Employee
December 26, 2022

Yes, but you cannot designate a publish instance "to be the one" which executes these jobs, at least not with the tooling provided by AEM itself. 

 

Of course you can create a new runmode "PublishWithSlingJobs", configure one of your publishs to have it and then start of the job based on that runmode criteria. Of course you need to be aware that this publish must always run and that you reconfigure another publish to have this runmode if the designated instance goes down either for a longer period of time or forever.

Shubham_borole
Community Advisor
Community Advisor
December 26, 2022

Hi @asutosh_jena_ , Is it possible to share the task happening in publish or the reason to select in publisher? It doesn't seem to be possible running it on a single pod publisher. If the use case allows, you can try to see if same can be done in author and then publish it.

Monendra_Singh
Level 3
December 26, 2022

Hi @asutosh_jena_  

One way to make a Sling job run only on one instance in a publish cluster is to use the instance-aware parameter of the @Component annotation. You can refer this article for more details - https://medium.com/@monendra80/execute-sling-job-only-on-one-aem-publish-instance-bec62f2b3faa

 

Hope this helps.

Thanks

Monendra

Asutosh_Jena_
Community Advisor
Community Advisor
December 27, 2022

Hi @monendra_singh 

There is no such parameter available as "instance-aware"!

 

 

arunpatidar
Community Advisor
Community Advisor
December 27, 2022

I believe you are trying to post some data from one publish to third party.

You can ask consumer to put a time check e.g. last request time and reject subsequent request from another publishers.

Arun Patidar
rampai
Community Advisor
Community Advisor
January 18, 2023

Hi @asutosh_jena_,

 

Have you tried using Discovery service and check for isLeader instance?

 

Thanks, 

Ram

Asutosh_Jena_
Community Advisor
Community Advisor
January 20, 2023

@rampai Have not tried yet. Let me try and come back if it works for AEM as Cloud.

 

Level 2
January 31, 2023

Did you try to run on author only and publish the result by code, I'm assuming you are creating a file on the dam so after generating that on the dam you can publish (doing the publish on your code not manually), that way you will have able the report on all publish instances as AEM Cloud could have several # of publish instances

bharath_kumark
Level 2
August 12, 2023

Try the below option, if anyone else is still looking for answer:

Use the TopologyEventListener to get the sling id associated with each instance in cluter environment and elect the leader based on the sling id; For example, select the instance which has highest sling id as leader instance.

 

 

public void handleTopologyEvent(TopologyEvent topologyEvent) { String leaderSlingId = StringUtils.EMPTY; Set<InstanceDescription> instances = topologyEvent.getNewView().getInstances(); for (InstanceDescription desc : instances) { if (leaderSlingId.isEmpty()) { leaderSlingId = desc.getSlingId(); } else if (leaderSlingId.compareTo(desc.getSlingId()) > 0) { leaderSlingId = desc.getSlingId(); } } topologyEvent.getNewView().getLocalInstance().getSlingId().equals(leaderSlingId); }

 

 

 

 

 
 
aanchal-sikka
Community Advisor
Community Advisor
November 6, 2023

@bharath_kumark 

 

Its mentioned in other replies that publish instances do not know each other.

 

Out of curiosity, the code that you have shared to elect using slingID, this is for:

  • AEM 6.5 or AEMaaCS?
  • Author/publish?

 

I am confused looking at contradictory statements. Some clarity would help !

Aanchal Sikka
bharath_kumark
Level 2
November 6, 2023

Hi @aanchal-sikka ,

 

The above code is written in AEM 6.5 version but in cloud version we can use the DiscoveryService to get the leader instance information like below. 

 

discoveryService.getTopology().getLocalInstance().isLeader();

 

Please let us know if this works.

 

Thanks!

Level 2
October 25, 2025

Hello. First of all you need to make sure that job is scheduled only once in a cluster, for that you can use 

org.apache.sling.discovery.DiscoveryService#getTopology#getLocalInstance#isLeader.

Also in your queue configuration make sure to set the queue.type=ORDERED and queue.maxparallel=1.0 . Doing so, sling will make sure that the job will be consumed only on LEADER instance.
Hope this helps.