Skip to content

Commit 4bf592a

Browse files
sjuarezBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:AzureFunctionsFixes' into beta
1 parent 2479c26 commit 4bf592a

File tree

14 files changed

+75
-151
lines changed

14 files changed

+75
-151
lines changed
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.genexus.cloud.serverless.azure.handler;
2-
import com.genexus.cloud.serverless.model.*;
32

3+
import com.genexus.cloud.serverless.model.*;
44
import com.microsoft.azure.functions.annotation.*;
55
import com.microsoft.azure.functions.ExecutionContext;
66

@@ -17,41 +17,43 @@ public AzureBlobStorageHandler() throws Exception {
1717
super();
1818
}
1919
public void run(
20-
@BlobTrigger(name = "content", source = "EventGrid", path = "%blob_path%", dataType = "binary") byte[] content,
20+
@BlobTrigger(name = "content", source = "EventGrid", path = "test-triggerinput-java/{name}", dataType = "binary") byte[] content,
2121
@BindingName("name") String name,
2222
final ExecutionContext context
2323
) throws Exception {
2424

2525
context.getLogger().info("GeneXus Blob Storage trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId());
2626
setupServerlessMappings(context.getFunctionName());
27-
27+
String functionName = context.getFunctionName().trim().toUpperCase();
28+
String storageEnvVar = String.format("GX_AZURE_%s_BLOB_STORAGE", functionName);
29+
String containerEnvVar = String.format("GX_AZURE_%s_BLOB_STORAGE_CONTAINER", functionName);
30+
String storageAccount = System.getenv(storageEnvVar);
31+
String containerName = System.getenv(containerEnvVar);
32+
String blobUri;
33+
if (storageAccount != null && !storageAccount.isEmpty() && containerName != null && !containerName.isEmpty())
34+
blobUri = String.format("https://%s.blob.core.windows.net/%s/%s", storageAccount, containerName, "name");
35+
else {
36+
blobUri = "name";
37+
context.getLogger().warning(String.format("Could not return complete URI. Please configure GX_AZURE_%s_BLOB_STORAGE and GX_AZURE_%s_BLOB_STORAGE_CONTAINER app settings.",functionName,functionName));
38+
}
2839
switch (executor.getMethodSignatureIdx()) {
2940
case 0:
3041
EventMessage msg = new EventMessage();
3142
msg.setMessageId(context.getInvocationId());
3243
msg.setMessageSourceType(EventMessageSourceType.BLOB);
3344
Instant nowUtc = Instant.now();
3445
msg.setMessageDate(Date.from(nowUtc));
35-
msg.setMessageData(Base64.getEncoder().encodeToString(content));
3646
List<EventMessageProperty> msgAtts = msg.getMessageProperties();
47+
msg.setMessageData(blobUri);
3748
msgAtts.add(new EventMessageProperty("Id", context.getInvocationId()));
3849
msgAtts.add(new EventMessageProperty("name", name));
39-
4050
msgs.add(msg);
4151
break;
4252
case 1:
4353
case 2:
44-
rawMessage = Base64.getEncoder().encodeToString(content);
45-
}
46-
try {
47-
EventMessageResponse response = dispatchEvent(msgs, rawMessage);
48-
if (response.hasFailed()) {
49-
logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage()));
50-
throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation.
51-
}
52-
} catch (Exception e) {
53-
logger.error("HandleRequest execution error", e);
54-
throw e; //Throw the exception so the runtime can Retry the operation.
54+
rawMessage = blobUri;
5555
}
56+
ExecuteDynamic(msgs,rawMessage);
5657
}
5758
}
59+

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureCosmosDBHandler.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.genexus.cloud.serverless.azure.handler;
2+
23
import com.genexus.cloud.serverless.JSONHelper;
34
import com.genexus.cloud.serverless.model.*;
45
import com.microsoft.azure.functions.ExecutionContext;
@@ -16,37 +17,26 @@ public void run(
1617
final ExecutionContext context) throws Exception {
1718

1819
context.getLogger().info("GeneXus CosmosDB trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId());
19-
2020
UUID eventId = UUID.randomUUID();
2121
EventMessages msgs = new EventMessages();
2222
EventMessagesList eventMessagesList = new EventMessagesList();
2323
String rawMessage= "";
2424
setupServerlessMappings(context.getFunctionName());
25-
2625
switch (executor.getMethodSignatureIdx()) {
2726
case 0:
28-
msgs = setupEventMessages(eventId,TryGetDocuments(items));
27+
msgs = setupEventMessages(eventId,TryGetDocuments(items, context));
2928
break;
3029
case 4:
31-
eventMessagesList = setupEventMessagesList(TryGetDocuments(items));
30+
eventMessagesList = setupEventMessagesList(TryGetDocuments(items, context));
3231
break;
3332
default:
3433
rawMessage = JSONHelper.toJSONString(items);
3534
break;
3635
}
37-
try {
38-
EventMessageResponse response = dispatchEvent(msgs, eventMessagesList, rawMessage);
39-
if (response.hasFailed()) {
40-
logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage()));
41-
throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation.
42-
}
43-
} catch (Exception e) {
44-
logger.error("HandleRequest execution error", e);
45-
throw e; //Throw the exception so the runtime can Retry the operation.
46-
}
36+
ExecuteDynamic(msgs,eventMessagesList,rawMessage);
4737
}
4838

49-
private List<Map<String,Object>> TryGetDocuments(List<Object> items){
39+
private List<Map<String,Object>> TryGetDocuments(List<Object> items, ExecutionContext context){
5040
List<Map<String,Object>> documents = new ArrayList<>();
5141
if (items != null && !items.isEmpty()) {
5242
for (Object item : items) {
@@ -57,7 +47,7 @@ private List<Map<String,Object>> TryGetDocuments(List<Object> items){
5747
}
5848
catch (Exception ex)
5949
{
60-
logger.error(String.format("Messages were not handled. Error trying to read Cosmos DB data. %s", ex.toString()));
50+
context.getLogger().severe(String.format("Messages were not handled. Error trying to read Cosmos DB data. %s", ex.toString()));
6151
throw new RuntimeException(String.format("Error trying to read Cosmos DB data. %s", ex.toString())); //Throw the exception so the runtime can Retry the operation.
6252
}
6353
}
@@ -66,6 +56,7 @@ private List<Map<String,Object>> TryGetDocuments(List<Object> items){
6656
}
6757
return null;
6858
}
59+
6960
private EventMessagesList setupEventMessagesList(List<Map<String,Object>> jsonList)
7061
{
7162
EventMessagesList messagesList = new EventMessagesList();

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridCloudHandler.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.genexus.cloud.serverless.model.EventMessage;
66
import com.genexus.cloud.serverless.model.EventMessageProperty;
7-
import com.genexus.cloud.serverless.model.EventMessageResponse;
87
import com.genexus.cloud.serverless.model.EventMessages;
98
import com.microsoft.azure.functions.ExecutionContext;
109
import com.microsoft.azure.functions.annotation.EventGridTrigger;
@@ -25,20 +24,11 @@ public void run(
2524
final ExecutionContext context) throws Exception {
2625
context.getLogger().info("GeneXus Event Grid CloudEvents trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId());
2726
setupServerlessMappings(context.getFunctionName());
28-
setupEventGridMessage(eventJson);
29-
30-
try {
31-
EventMessageResponse response = dispatchEvent(msgs, rawMessage);
32-
if (response.hasFailed()) {
33-
logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage()));
34-
throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation.
35-
}
36-
} catch (Exception e) {
37-
logger.error("HandleRequest execution error", e);
38-
throw e; //Throw the exception so the runtime can Retry the operation.
39-
}
27+
setupEventGridMessage(eventJson, context);
28+
ExecuteDynamic(msgs, rawMessage);
4029
}
41-
protected void setupEventGridMessage(String eventJson) throws JsonProcessingException {
30+
31+
protected void setupEventGridMessage(String eventJson, ExecutionContext context) throws JsonProcessingException {
4232
switch (executor.getMethodSignatureIdx()) {
4333
case 0:
4434
try {
@@ -67,7 +57,7 @@ protected void setupEventGridMessage(String eventJson) throws JsonProcessingExce
6757
msgs.add(msg);
6858
}
6959
catch (Exception e) {
70-
logger.error("HandleRequest execution error", e);
60+
context.getLogger().severe(String.format("HandleRequest execution error: %s",e.getMessage()));
7161
throw e;}
7262
break;
7363
case 1:

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridHandler.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,10 @@ public AzureEventGridHandler() throws Exception {
1919
public void run(
2020
@EventGridTrigger(name = "eventgridEvent") EventGridEvent event,
2121
final ExecutionContext context) throws Exception {
22-
2322
context.getLogger().info("GeneXus Event Grid trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId());
24-
2523
setupServerlessMappings(context.getFunctionName());
2624
setupEventGridMessage(event);
27-
28-
try {
29-
EventMessageResponse response = dispatchEvent(msgs, rawMessage);
30-
if (response.hasFailed()) {
31-
logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage()));
32-
throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation.
33-
}
34-
} catch (Exception e) {
35-
logger.error("HandleRequest execution error", e);
36-
throw e; //Throw the exception so the runtime can Retry the operation.
37-
}
25+
ExecuteDynamic(msgs, rawMessage);
3826
}
3927

4028
protected void setupEventGridMessage(EventGridEvent event) {
@@ -46,15 +34,12 @@ protected void setupEventGridMessage(EventGridEvent event) {
4634
msg.setMessageVersion(event.getDataVersion());
4735
msg.setMessageDate(new Date());
4836
msg.setMessageData(event.getData().toString());
49-
5037
List<EventMessageProperty> msgAtts = msg.getMessageProperties();
5138
msgAtts.add(new EventMessageProperty("Id", event.getId()));
52-
5339
msgAtts.add(new EventMessageProperty("Subject",event.getSubject()));
5440
msgAtts.add(new EventMessageProperty("Topic",event.getTopic()));
5541
if (event.getEventTime()!= null)
5642
msgAtts.add(new EventMessageProperty("EventTime",event.getEventTime().toString()));
57-
5843
msgs.add(msg);
5944
break;
6045
case 1:

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ public class AzureFunctionConfiguration extends ServerlessFunctionConfiguration
1212

1313
public AzureFunctionConfiguration() {
1414
}
15+
1516
public AzureFunctionConfiguration(String functionName, String gxEntrypoint) {
1617
this.functionName = functionName;
1718
this.gxEntrypoint = gxEntrypoint;
1819
}
20+
1921
public void setFunctionName(String functionName) {
2022
this.functionName = functionName;
2123
}
24+
2225
public void setGXEntrypoint(String gxEntrypoint) {
2326
this.gxEntrypoint = gxEntrypoint;
2427
}
@@ -30,6 +33,7 @@ public String getFunctionName() {
3033
public String getGXEntrypoint() {
3134
return gxEntrypoint;
3235
}
36+
3337
@Override
3438
public boolean isValidConfiguration () {
3539
return functionName != null && !functionName.trim().isEmpty() && gxEntrypoint != null && !gxEntrypoint.trim().isEmpty();

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfigurationHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class AzureFunctionConfigurationHelper {
2020

2121
public static List<AzureFunctionConfiguration> getFunctionsMapConfiguration() throws FunctionConfigurationException {
2222
File configFile = new File(FUNCTION_CONFIG_PATH);
23-
2423
if (configFile.exists()) {
2524
try {
2625
String jsonConfig = new String(Files.readAllBytes(Paths.get(FUNCTION_CONFIG_PATH)));

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureQueueHandler.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.genexus.cloud.serverless.azure.handler;
22

33
import com.genexus.cloud.serverless.model.*;
4-
54
import com.microsoft.azure.functions.annotation.*;
65
import com.microsoft.azure.functions.ExecutionContext;
7-
86
import java.time.Instant;
97
import java.util.List;
108
import java.util.*;
@@ -13,6 +11,7 @@ public class AzureQueueHandler extends AzureEventHandler {
1311
public AzureQueueHandler() throws Exception {
1412
super();
1513
}
14+
1615
public void run(
1716
@QueueTrigger(name = "message", queueName = "%queue_name%") String message,
1817
@BindingName("Id") final String id,
@@ -41,15 +40,6 @@ public void run(
4140
msgAtts.add(new EventMessageProperty("PopReceipt", popReceipt));
4241
msgs.add(msg);
4342
}
44-
try {
45-
EventMessageResponse response = dispatchEvent(msgs, message);
46-
if (response.hasFailed()) {
47-
logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage()));
48-
throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation.
49-
}
50-
} catch (Exception e) {
51-
logger.error("HandleRequest execution error", e);
52-
throw e; //Throw the exception so the runtime can Retry the operation.
53-
}
43+
ExecuteDynamic(msgs, message);
5444
}
5545
}

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueHandler.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.genexus.cloud.serverless.helpers.ServiceBusBatchMessageProcessor;
44
import com.genexus.cloud.serverless.helpers.ServiceBusProcessedMessage;
5-
import com.genexus.cloud.serverless.model.*;
65
import com.microsoft.azure.functions.ExecutionContext;
76
import com.microsoft.azure.functions.annotation.*;
87

@@ -22,15 +21,6 @@ public void run(
2221
setupServerlessMappings(context.getFunctionName());
2322
ServiceBusBatchMessageProcessor queueBatchMessageProcessor = new ServiceBusBatchMessageProcessor();
2423
ServiceBusProcessedMessage queueMessage = queueBatchMessageProcessor.processQueueMessage(context, executor, messages);
25-
try {
26-
EventMessageResponse response = dispatchEvent(queueMessage.getEventMessages(),queueMessage.getRawMessage());
27-
if (response.hasFailed()) {
28-
logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage()));
29-
throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation.
30-
}
31-
} catch (Exception e) {
32-
logger.error("HandleRequest execution error", e);
33-
throw e; //Throw the exception so the runtime can Retry the operation.
34-
}
24+
ExecuteDynamic(queueMessage.getEventMessages(),queueMessage.getRawMessage());
3525
}
3626
}

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueSingleMsgHandler.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.genexus.cloud.serverless.helpers.ServiceBusProcessedMessage;
44
import com.genexus.cloud.serverless.helpers.ServiceBusSingleMessageProcessor;
5-
import com.genexus.cloud.serverless.model.*;
65
import com.microsoft.azure.functions.ExecutionContext;
76
import com.microsoft.azure.functions.annotation.BindingName;
87
import com.microsoft.azure.functions.annotation.Cardinality;
@@ -26,15 +25,6 @@ public void run(
2625
setupServerlessMappings(context.getFunctionName());
2726
ServiceBusSingleMessageProcessor queueSingleMessageProcessor = new ServiceBusSingleMessageProcessor();
2827
ServiceBusProcessedMessage queueMessage = queueSingleMessageProcessor.processQueueMessage(executor,messageId,enqueuedTimeUtc,context,message);
29-
try {
30-
EventMessageResponse response = dispatchEvent(queueMessage.getEventMessages(), queueMessage.getRawMessage());
31-
if (response.hasFailed()) {
32-
logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage()));
33-
throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation.
34-
}
35-
} catch (Exception e) {
36-
logger.error("HandleRequest execution error", e);
37-
throw e; //Throw the exception so the runtime can Retry the operation.
38-
}
28+
ExecuteDynamic(queueMessage.getEventMessages(), queueMessage.getRawMessage());
3929
}
4030
}

gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicHandler.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.genexus.cloud.serverless.helpers.ServiceBusBatchMessageProcessor;
44
import com.genexus.cloud.serverless.helpers.ServiceBusProcessedMessage;
5-
import com.genexus.cloud.serverless.model.*;
65
import com.microsoft.azure.functions.ExecutionContext;
76
import com.microsoft.azure.functions.annotation.*;
87

@@ -22,15 +21,6 @@ public void run(
2221
setupServerlessMappings(context.getFunctionName());
2322
ServiceBusBatchMessageProcessor queueBatchMessageProcessor = new ServiceBusBatchMessageProcessor();
2423
ServiceBusProcessedMessage queueMessage = queueBatchMessageProcessor.processQueueMessage(context, executor, messages);
25-
try {
26-
EventMessageResponse response = dispatchEvent(queueMessage.getEventMessages(), queueMessage.getRawMessage());
27-
if (response.hasFailed()) {
28-
logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage()));
29-
throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation.
30-
}
31-
} catch (Exception e) {
32-
logger.error("HandleRequest execution error", e);
33-
throw e; //Throw the exception so the runtime can Retry the operation.
34-
}
24+
ExecuteDynamic(queueMessage.getEventMessages(), queueMessage.getRawMessage());
3525
}
3626
}

0 commit comments

Comments
 (0)