Skip to content

Commit c1f986b

Browse files
GH-91 waiting for the JobConsumers to be ready before publishing job
1 parent aebaa55 commit c1f986b

File tree

11 files changed

+123
-11
lines changed

11 files changed

+123
-11
lines changed

aem/aio_aem_events/README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@ configurations, enabling AEM and Adobe I/O Events integration.
55

66
## Configuration
77

8-
* The package expects [`Workspace`](./src/main/java/com/adobe/io/workspace/ocd/WorkspaceConfig.java)
9-
configuration to be resolved from CloudManager/System environment variables
10-
* cf
11-
the [Configuring OSGi for Adobe Experience Manager as a Cloud Service](https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/implementing/deploying/configuring-osgi.html%3Flang%3Den#environment-specific-configuration-values)
12-
guide
8+
This package expects your Adobe Developer Console configured with OSGI configuration
9+
see [core_aem documentation](../core_aem/README.md)
10+
11+
## Status Checks
12+
13+
This packages holds bundles that come with status servlet:
14+
* from [/bin/aio/workspace.json](http://localhost:4502/bin/aio/workspace.json)
15+
you can `GET` the status of your workspace configuration (see [core_aem documentation](../core_aem/README.md))
16+
17+
http://localhost:4502/bin/aio/provider_config.json
18+
http://localhost:4502/bin/aio/provider.json
19+
http://localhost:4502/bin/aio/publish_event.json
20+
http://localhost:4502/bin/aio/event_metadata.json
21+
http://localhost:4502/bin/aio/osgi_event_metadata.json
1322

1423
## Builds
1524

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2017 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
package com.adobe.aio.aem.util;
13+
14+
import com.adobe.aio.exception.AIOException;
15+
import java.time.Instant;
16+
import java.util.concurrent.ThreadLocalRandom;
17+
import java.util.function.Supplier;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
public class Util {
22+
23+
private Util(){}
24+
private static final long TIME_OUT_MILLI = 30000L;
25+
private final static Logger log = LoggerFactory.getLogger(Util.class);
26+
27+
public static void waitFor(Supplier<Boolean> isReady, String supplierDescription) {
28+
long start = Instant.now().toEpochMilli();
29+
while (!isReady.get()){
30+
try {
31+
Thread.sleep(ThreadLocalRandom.current().nextInt( 50,500));
32+
} catch (InterruptedException e) {
33+
throw new AIOException("InterruptedException, while waiting for "+supplierDescription, e);
34+
}
35+
log.debug("waiting for {} to be ready ...", supplierDescription);
36+
if (Instant.now().toEpochMilli()-start> TIME_OUT_MILLI){
37+
throw new AIOException("Timeout error, while waiting for "+supplierDescription);
38+
}
39+
}
40+
}
41+
42+
}

aem/events_mgmt_aem/src/main/java/com/adobe/aio/aem/event/management/EventMetadataRegistrationJobConsumer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.fasterxml.jackson.databind.ObjectMapper;
1616
import org.apache.sling.event.jobs.Job;
1717
import org.apache.sling.event.jobs.consumer.JobConsumer;
18+
import org.osgi.service.component.annotations.Activate;
1819
import org.osgi.service.component.annotations.Component;
1920
import org.osgi.service.component.annotations.Reference;
2021
import org.slf4j.Logger;
@@ -40,6 +41,12 @@ public class EventMetadataRegistrationJobConsumer implements JobConsumer {
4041
EventMetadataStatusSupplier eventMetadataStatusSupplier;
4142
private String lastErrorMessage;
4243

44+
@Activate
45+
protected void activate() {
46+
log.info("activating");
47+
eventMetadataStatusSupplier.setJobConsumerReady(true);
48+
}
49+
4350
@Override
4451
public JobResult process(final Job job) {
4552
if (job.getProperty(AIO_EVENT_CODE_PROPERTY) != null) {

aem/events_mgmt_aem/src/main/java/com/adobe/aio/aem/event/management/EventMetadataStatusSupplier.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@
1818
@ProviderType
1919
public interface EventMetadataStatusSupplier extends StatusSupplier {
2020

21+
Boolean isJobConsumerReady();
22+
void setJobConsumerReady(Boolean ready);
2123
void addStatus(String eventCode, EventMetadataStatus eventMetadataStatus);
2224
}

aem/events_mgmt_aem/src/main/java/com/adobe/aio/aem/event/management/internal/EventMetadataStatusSupplierImpl.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313

1414
import com.adobe.aio.aem.event.management.EventMetadataStatus;
1515
import com.adobe.aio.aem.event.management.EventMetadataStatusSupplier;
16-
import com.adobe.aio.aem.event.management.ocd.ApiManagementConfig;
1716
import com.adobe.aio.aem.status.Status;
1817
import java.util.HashMap;
1918
import java.util.Map;
2019
import java.util.concurrent.ConcurrentHashMap;
21-
import org.osgi.service.component.annotations.Activate;
2220
import org.osgi.service.component.annotations.Component;
2321
import org.slf4j.Logger;
2422
import org.slf4j.LoggerFactory;
@@ -32,10 +30,16 @@ public class EventMetadataStatusSupplierImpl implements EventMetadataStatusSuppl
3230

3331
private final Logger log = LoggerFactory.getLogger(getClass());
3432
private final Map<String, EventMetadataStatus> eventMetadataStatusByEventCode = new ConcurrentHashMap<>();
33+
private Boolean isJobConsumerReady = false;
3534

36-
@Activate
37-
protected void activate(ApiManagementConfig config) {
38-
log.info("activating");
35+
@Override
36+
public Boolean isJobConsumerReady() {
37+
return this.isJobConsumerReady;
38+
}
39+
40+
@Override
41+
public void setJobConsumerReady(Boolean ready) {
42+
this.isJobConsumerReady = ready;
3943
}
4044

4145
@Override

aem/events_mgmt_aem/src/main/java/com/adobe/aio/aem/event/management/internal/EventMetadataSupplierImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
package com.adobe.aio.aem.event.management.internal;
1313

1414
import com.adobe.aio.aem.event.management.EventMetadataRegistrationJobConsumer;
15+
import com.adobe.aio.aem.event.management.EventMetadataStatusSupplier;
1516
import com.adobe.aio.aem.event.management.EventMetadataSupplier;
17+
import com.adobe.aio.aem.event.management.EventProviderRegistrationService;
1618
import com.adobe.aio.aem.event.management.ocd.EventMetadataConfig;
19+
import com.adobe.aio.aem.util.Util;
1720
import com.adobe.aio.event.management.model.EventMetadata;
1821
import com.fasterxml.jackson.databind.ObjectMapper;
1922
import java.util.HashMap;
2023
import java.util.Map;
24+
import java.util.concurrent.ThreadLocalRandom;
2125
import org.apache.sling.event.jobs.JobManager;
2226
import org.osgi.service.component.annotations.Activate;
2327
import org.osgi.service.component.annotations.Component;
@@ -40,12 +44,20 @@ public class EventMetadataSupplierImpl implements EventMetadataSupplier {
4044
@Reference
4145
JobManager jobManager;
4246

47+
@Reference
48+
EventMetadataStatusSupplier eventMetadataStatusSupplier;
49+
50+
@Reference
51+
EventProviderRegistrationService eventProviderRegistrationService;
52+
4353
@Activate
4454
protected void activate(EventMetadataConfig eventMetadataConfig) {
4555
Map<String, Object> jobProperties = new HashMap();
4656
jobProperties.put(EventMetadataRegistrationJobConsumer.AIO_EVENT_CODE_PROPERTY,
4757
eventMetadataConfig.aio_event_code());
4858
try {
59+
Util.waitFor(eventMetadataStatusSupplier::isJobConsumerReady,
60+
"Adobe I/O Events Metadata Registration Job Consumer");
4961
EventMetadata configuredEventMetadata = EventMetadata.builder()
5062
.description(eventMetadataConfig.aio_event_description())
5163
.label(eventMetadataConfig.aio_event_label())
@@ -67,4 +79,5 @@ protected void activate(EventMetadataConfig eventMetadataConfig) {
6779
}
6880
}
6981

82+
7083
}

aem/events_mgmt_aem/src/main/java/com/adobe/aio/aem/event/management/internal/EventProviderRegistrationServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private Provider registerProvider() {
9595
}
9696

9797
@Override
98-
public Provider getRegisteredProvider() {
98+
public synchronized Provider getRegisteredProvider() {
9999
if (!isProviderRegistered()) {
100100
log.warn("The Adobe I/O Events Provider was not registered yet, trying registering it...");
101101
return registerProvider();

aem/events_osgi_mapping/src/main/java/com/adobe/aio/aem/event/osgimapping/EventHandlerRegistrationJobConsumer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class EventHandlerRegistrationJobConsumer implements JobConsumer {
7575
protected void activate(BundleContext context, Map<String, Object> config) {
7676
log.info("activating");
7777
this.bundleContext = context;
78+
osgiEventMappingStatusSupplier.setJobConsumerReady(true);
7879
}
7980

8081
@Override
@@ -87,6 +88,13 @@ public JobResult process(final Job job) {
8788
workspaceSupplier.getWorkspace().validateAll();
8889
// we don't want to register sling event handlers if the provider can't be registered
8990
eventProviderRegistrationService.getRegisteredProvider();
91+
/**
92+
* Return the proper JobResult based on the work done...
93+
*
94+
* > OK : Processed successfully
95+
* > FAILED: Processed unsuccessfully and reschedule
96+
* > CANCEL: Processed unsuccessfully and do NOT reschedule
97+
*/
9098

9199
OsgiEventMapping osgiEventMapping = new ObjectMapper().readValue
92100
((String) job.getProperty(AIO_OSGI_EVENT_MAPPING_PROPERTY), OsgiEventMapping.class);

aem/events_osgi_mapping/src/main/java/com/adobe/aio/aem/event/osgimapping/OsgiEventMappingStatusSupplier.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@
1818
@ProviderType
1919
public interface OsgiEventMappingStatusSupplier extends StatusSupplier {
2020

21+
boolean isJobConsumerReady();
22+
void setJobConsumerReady(boolean ready);
2123
void addStatus(String eventCode, OsgiEventMappingStatus eventMetadataStatus);
2224
}

aem/events_osgi_mapping/src/main/java/com/adobe/aio/aem/event/osgimapping/internal/OsgiEventMappingStatusSupplierImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ public class OsgiEventMappingStatusSupplierImpl implements OsgiEventMappingStatu
3232
private final Logger log = LoggerFactory.getLogger(getClass());
3333
private final Map<String, OsgiEventMappingStatus> osgiEventMappingStatusByEventCode = new ConcurrentHashMap<>();
3434

35+
private boolean isJobConsumerReady = false;
36+
37+
@Override
38+
public boolean isJobConsumerReady() {
39+
return this.isJobConsumerReady;
40+
}
41+
42+
@Override
43+
public void setJobConsumerReady(boolean ready) {
44+
this.isJobConsumerReady =ready;
45+
}
3546

3647
@Override
3748
public void addStatus(String eventCode, OsgiEventMappingStatus eventMetadataStatus) {

0 commit comments

Comments
 (0)