Expand my Community achievements bar.

SOLVED

Task in AEM as a Cloud Service (only on author) is running twice

Avatar

Level 2

I’m starting to feel desperate and really need your help with something on AEM as a Cloud Service. I’ve set up an email reminder task that operates exclusively on the author instance once a day. This task looks for something specific on a page and sends out an email.

 

The problem I’m facing is that it keeps sending emails twice!

 

Now I understand that the author server operates within a cluster and requires specific configuration/annotation. However, despite my attempts, it continues to send emails twice. On my local machine (which of course isn’t a cluster), it works correctly.

Attempt #1: nok

@Component(
service = Runnable.class,
property = {
SERVICE_DESCRIPTION + "=Email Reminder Task",
Scheduler.PROPERTY_SCHEDULER_RUN_ON + "=" + Scheduler.VALUE_RUN_ON_LEADER, //LEADER
Scheduler.PROPERTY_SCHEDULER_CONCURRENT + ":Boolean=false",
},
configurationPid = "com.customername.core.services.emailreminder.EmailReminderTask")
@ProviderType
@Designate(ocd = EmailReminderTaskConfig.class)

Attempt #2: nok

ScheduleOptions scheduleOptions = scheduler.EXPR(emailReminderTaskConfig.scheduler_expression());
scheduleOptions.name(schedulerJobName);
scheduleOptions.canRunConcurrently(false);
scheduleOptions.onLeaderOnly(true);
scheduler.schedule(this, scheduleOptions);

 

I also searched online, but found no proper solution. Anyone of you have a guess, why it behaves like that in the cloud?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 2

Just FYI: I didn't find any solution, so I refactored the implementation from org.apache.sling.commons.scheduler.Scheduler [1] to org.apache.sling.event.jobs.Job [2].

So, same solution as here: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-as-cloud-service-sched...

 

[1] https://sling.apache.org/documentation/bundles/scheduler-service-commons-scheduler.html
[2] https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html

 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 2

Just FYI: I didn't find any solution, so I refactored the implementation from org.apache.sling.commons.scheduler.Scheduler [1] to org.apache.sling.event.jobs.Job [2].

So, same solution as here: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-as-cloud-service-sched...

 

[1] https://sling.apache.org/documentation/bundles/scheduler-service-commons-scheduler.html
[2] https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html

 

Avatar

Level 3

Hi @TrisM_ch ,

Your answer is correct. I too faced same issue when using Commons Scheduler. I explained the problem with Commons Scheduler in my article here https://medium.com/p/c5d688154f58.

Short answer, commons scheduler fires for each cluster in cloud. When cloud scales up/down, more instances run. To resolve this problem, Sling introduced Scheduled Jobs. Main difference, its persisted under /var/eventing/scheduled-jobs. This way, they prevent duplicate executions. 

 

For AEM Cloud service, should switch to Scheduled Jobs instead of Commons Scheduler.

Hope this helps.