From 7ac2c12e3eae6089194e4b37c985b6199af6cb74 Mon Sep 17 00:00:00 2001 From: sjuarez Date: Mon, 13 Jan 2025 16:14:45 -0300 Subject: [PATCH] Azure Functions Fixes --- .../handler/AzureBlobStorageHandler.java | 34 +++++++++-------- .../azure/handler/AzureCosmosDBHandler.java | 23 ++++------- .../handler/AzureEventGridCloudHandler.java | 20 +++------- .../azure/handler/AzureEventGridHandler.java | 17 +-------- .../handler/AzureFunctionConfiguration.java | 4 ++ .../AzureFunctionConfigurationHelper.java | 1 - .../azure/handler/AzureQueueHandler.java | 14 +------ .../handler/AzureServiceBusQueueHandler.java | 12 +----- .../AzureServiceBusQueueSingleMsgHandler.java | 12 +----- .../handler/AzureServiceBusTopicHandler.java | 12 +----- .../AzureServiceBusTopicSingleMsgHandler.java | 12 +----- .../azure/handler/AzureTimerHandler.java | 25 ++---------- .../azure/events/TestAzureQueueHandler.java | 2 - .../ServerlessBaseEventHandler.java | 38 +++++++++++++++---- 14 files changed, 75 insertions(+), 151 deletions(-) diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureBlobStorageHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureBlobStorageHandler.java index 14ae44856..4a29b9f07 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureBlobStorageHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureBlobStorageHandler.java @@ -1,6 +1,6 @@ package com.genexus.cloud.serverless.azure.handler; -import com.genexus.cloud.serverless.model.*; +import com.genexus.cloud.serverless.model.*; import com.microsoft.azure.functions.annotation.*; import com.microsoft.azure.functions.ExecutionContext; @@ -17,14 +17,25 @@ public AzureBlobStorageHandler() throws Exception { super(); } public void run( - @BlobTrigger(name = "content", source = "EventGrid", path = "%blob_path%", dataType = "binary") byte[] content, + @BlobTrigger(name = "content", source = "EventGrid", path = "test-triggerinput-java/{name}", dataType = "binary") byte[] content, @BindingName("name") String name, final ExecutionContext context ) throws Exception { context.getLogger().info("GeneXus Blob Storage trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId()); setupServerlessMappings(context.getFunctionName()); - + String functionName = context.getFunctionName().trim().toUpperCase(); + String storageEnvVar = String.format("GX_AZURE_%s_BLOB_STORAGE", functionName); + String containerEnvVar = String.format("GX_AZURE_%s_BLOB_STORAGE_CONTAINER", functionName); + String storageAccount = System.getenv(storageEnvVar); + String containerName = System.getenv(containerEnvVar); + String blobUri; + if (storageAccount != null && !storageAccount.isEmpty() && containerName != null && !containerName.isEmpty()) + blobUri = String.format("https://%s.blob.core.windows.net/%s/%s", storageAccount, containerName, "name"); + else { + blobUri = "name"; + 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)); + } switch (executor.getMethodSignatureIdx()) { case 0: EventMessage msg = new EventMessage(); @@ -32,26 +43,17 @@ public void run( msg.setMessageSourceType(EventMessageSourceType.BLOB); Instant nowUtc = Instant.now(); msg.setMessageDate(Date.from(nowUtc)); - msg.setMessageData(Base64.getEncoder().encodeToString(content)); List msgAtts = msg.getMessageProperties(); + msg.setMessageData(blobUri); msgAtts.add(new EventMessageProperty("Id", context.getInvocationId())); msgAtts.add(new EventMessageProperty("name", name)); - msgs.add(msg); break; case 1: case 2: - rawMessage = Base64.getEncoder().encodeToString(content); - } - try { - EventMessageResponse response = dispatchEvent(msgs, rawMessage); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. + rawMessage = blobUri; } + ExecuteDynamic(msgs,rawMessage); } } + diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureCosmosDBHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureCosmosDBHandler.java index 628eb4ba1..e4de75c3b 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureCosmosDBHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureCosmosDBHandler.java @@ -1,4 +1,5 @@ package com.genexus.cloud.serverless.azure.handler; + import com.genexus.cloud.serverless.JSONHelper; import com.genexus.cloud.serverless.model.*; import com.microsoft.azure.functions.ExecutionContext; @@ -16,37 +17,26 @@ public void run( final ExecutionContext context) throws Exception { context.getLogger().info("GeneXus CosmosDB trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId()); - UUID eventId = UUID.randomUUID(); EventMessages msgs = new EventMessages(); EventMessagesList eventMessagesList = new EventMessagesList(); String rawMessage= ""; setupServerlessMappings(context.getFunctionName()); - switch (executor.getMethodSignatureIdx()) { case 0: - msgs = setupEventMessages(eventId,TryGetDocuments(items)); + msgs = setupEventMessages(eventId,TryGetDocuments(items, context)); break; case 4: - eventMessagesList = setupEventMessagesList(TryGetDocuments(items)); + eventMessagesList = setupEventMessagesList(TryGetDocuments(items, context)); break; default: rawMessage = JSONHelper.toJSONString(items); break; } - try { - EventMessageResponse response = dispatchEvent(msgs, eventMessagesList, rawMessage); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + ExecuteDynamic(msgs,eventMessagesList,rawMessage); } - private List> TryGetDocuments(List items){ + private List> TryGetDocuments(List items, ExecutionContext context){ List> documents = new ArrayList<>(); if (items != null && !items.isEmpty()) { for (Object item : items) { @@ -57,7 +47,7 @@ private List> TryGetDocuments(List items){ } catch (Exception ex) { - logger.error(String.format("Messages were not handled. Error trying to read Cosmos DB data. %s", ex.toString())); + context.getLogger().severe(String.format("Messages were not handled. Error trying to read Cosmos DB data. %s", ex.toString())); 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. } } @@ -66,6 +56,7 @@ private List> TryGetDocuments(List items){ } return null; } + private EventMessagesList setupEventMessagesList(List> jsonList) { EventMessagesList messagesList = new EventMessagesList(); diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridCloudHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridCloudHandler.java index 3aa86be12..51b2b0ee6 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridCloudHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridCloudHandler.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.genexus.cloud.serverless.model.EventMessage; import com.genexus.cloud.serverless.model.EventMessageProperty; -import com.genexus.cloud.serverless.model.EventMessageResponse; import com.genexus.cloud.serverless.model.EventMessages; import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.annotation.EventGridTrigger; @@ -25,20 +24,11 @@ public void run( final ExecutionContext context) throws Exception { context.getLogger().info("GeneXus Event Grid CloudEvents trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId()); setupServerlessMappings(context.getFunctionName()); - setupEventGridMessage(eventJson); - - try { - EventMessageResponse response = dispatchEvent(msgs, rawMessage); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + setupEventGridMessage(eventJson, context); + ExecuteDynamic(msgs, rawMessage); } - protected void setupEventGridMessage(String eventJson) throws JsonProcessingException { + + protected void setupEventGridMessage(String eventJson, ExecutionContext context) throws JsonProcessingException { switch (executor.getMethodSignatureIdx()) { case 0: try { @@ -67,7 +57,7 @@ protected void setupEventGridMessage(String eventJson) throws JsonProcessingExce msgs.add(msg); } catch (Exception e) { - logger.error("HandleRequest execution error", e); + context.getLogger().severe(String.format("HandleRequest execution error: %s",e.getMessage())); throw e;} break; case 1: diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridHandler.java index df304349a..fa45406a8 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureEventGridHandler.java @@ -19,22 +19,10 @@ public AzureEventGridHandler() throws Exception { public void run( @EventGridTrigger(name = "eventgridEvent") EventGridEvent event, final ExecutionContext context) throws Exception { - context.getLogger().info("GeneXus Event Grid trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId()); - setupServerlessMappings(context.getFunctionName()); setupEventGridMessage(event); - - try { - EventMessageResponse response = dispatchEvent(msgs, rawMessage); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + ExecuteDynamic(msgs, rawMessage); } protected void setupEventGridMessage(EventGridEvent event) { @@ -46,15 +34,12 @@ protected void setupEventGridMessage(EventGridEvent event) { msg.setMessageVersion(event.getDataVersion()); msg.setMessageDate(new Date()); msg.setMessageData(event.getData().toString()); - List msgAtts = msg.getMessageProperties(); msgAtts.add(new EventMessageProperty("Id", event.getId())); - msgAtts.add(new EventMessageProperty("Subject",event.getSubject())); msgAtts.add(new EventMessageProperty("Topic",event.getTopic())); if (event.getEventTime()!= null) msgAtts.add(new EventMessageProperty("EventTime",event.getEventTime().toString())); - msgs.add(msg); break; case 1: diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfiguration.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfiguration.java index db1443e7b..d14456b00 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfiguration.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfiguration.java @@ -12,13 +12,16 @@ public class AzureFunctionConfiguration extends ServerlessFunctionConfiguration public AzureFunctionConfiguration() { } + public AzureFunctionConfiguration(String functionName, String gxEntrypoint) { this.functionName = functionName; this.gxEntrypoint = gxEntrypoint; } + public void setFunctionName(String functionName) { this.functionName = functionName; } + public void setGXEntrypoint(String gxEntrypoint) { this.gxEntrypoint = gxEntrypoint; } @@ -30,6 +33,7 @@ public String getFunctionName() { public String getGXEntrypoint() { return gxEntrypoint; } + @Override public boolean isValidConfiguration () { return functionName != null && !functionName.trim().isEmpty() && gxEntrypoint != null && !gxEntrypoint.trim().isEmpty(); diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfigurationHelper.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfigurationHelper.java index dcda65518..1e06326ff 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfigurationHelper.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureFunctionConfigurationHelper.java @@ -20,7 +20,6 @@ public class AzureFunctionConfigurationHelper { public static List getFunctionsMapConfiguration() throws FunctionConfigurationException { File configFile = new File(FUNCTION_CONFIG_PATH); - if (configFile.exists()) { try { String jsonConfig = new String(Files.readAllBytes(Paths.get(FUNCTION_CONFIG_PATH))); diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureQueueHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureQueueHandler.java index 5409473c6..536e62455 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureQueueHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureQueueHandler.java @@ -1,10 +1,8 @@ package com.genexus.cloud.serverless.azure.handler; import com.genexus.cloud.serverless.model.*; - import com.microsoft.azure.functions.annotation.*; import com.microsoft.azure.functions.ExecutionContext; - import java.time.Instant; import java.util.List; import java.util.*; @@ -13,6 +11,7 @@ public class AzureQueueHandler extends AzureEventHandler { public AzureQueueHandler() throws Exception { super(); } + public void run( @QueueTrigger(name = "message", queueName = "%queue_name%") String message, @BindingName("Id") final String id, @@ -41,15 +40,6 @@ public void run( msgAtts.add(new EventMessageProperty("PopReceipt", popReceipt)); msgs.add(msg); } - try { - EventMessageResponse response = dispatchEvent(msgs, message); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + ExecuteDynamic(msgs, message); } } \ No newline at end of file diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueHandler.java index 750ad7f53..7696872e1 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueHandler.java @@ -2,7 +2,6 @@ import com.genexus.cloud.serverless.helpers.ServiceBusBatchMessageProcessor; import com.genexus.cloud.serverless.helpers.ServiceBusProcessedMessage; -import com.genexus.cloud.serverless.model.*; import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.annotation.*; @@ -22,15 +21,6 @@ public void run( setupServerlessMappings(context.getFunctionName()); ServiceBusBatchMessageProcessor queueBatchMessageProcessor = new ServiceBusBatchMessageProcessor(); ServiceBusProcessedMessage queueMessage = queueBatchMessageProcessor.processQueueMessage(context, executor, messages); - try { - EventMessageResponse response = dispatchEvent(queueMessage.getEventMessages(),queueMessage.getRawMessage()); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + ExecuteDynamic(queueMessage.getEventMessages(),queueMessage.getRawMessage()); } } diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueSingleMsgHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueSingleMsgHandler.java index ee9c45820..66f6e5e6e 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueSingleMsgHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusQueueSingleMsgHandler.java @@ -2,7 +2,6 @@ import com.genexus.cloud.serverless.helpers.ServiceBusProcessedMessage; import com.genexus.cloud.serverless.helpers.ServiceBusSingleMessageProcessor; -import com.genexus.cloud.serverless.model.*; import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.annotation.BindingName; import com.microsoft.azure.functions.annotation.Cardinality; @@ -26,15 +25,6 @@ public void run( setupServerlessMappings(context.getFunctionName()); ServiceBusSingleMessageProcessor queueSingleMessageProcessor = new ServiceBusSingleMessageProcessor(); ServiceBusProcessedMessage queueMessage = queueSingleMessageProcessor.processQueueMessage(executor,messageId,enqueuedTimeUtc,context,message); - try { - EventMessageResponse response = dispatchEvent(queueMessage.getEventMessages(), queueMessage.getRawMessage()); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + ExecuteDynamic(queueMessage.getEventMessages(), queueMessage.getRawMessage()); } } diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicHandler.java index 0242ead8f..1dae77142 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicHandler.java @@ -2,7 +2,6 @@ import com.genexus.cloud.serverless.helpers.ServiceBusBatchMessageProcessor; import com.genexus.cloud.serverless.helpers.ServiceBusProcessedMessage; -import com.genexus.cloud.serverless.model.*; import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.annotation.*; @@ -22,15 +21,6 @@ public void run( setupServerlessMappings(context.getFunctionName()); ServiceBusBatchMessageProcessor queueBatchMessageProcessor = new ServiceBusBatchMessageProcessor(); ServiceBusProcessedMessage queueMessage = queueBatchMessageProcessor.processQueueMessage(context, executor, messages); - try { - EventMessageResponse response = dispatchEvent(queueMessage.getEventMessages(), queueMessage.getRawMessage()); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + ExecuteDynamic(queueMessage.getEventMessages(), queueMessage.getRawMessage()); } } diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicSingleMsgHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicSingleMsgHandler.java index 2e3cc218e..7384a72da 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicSingleMsgHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureServiceBusTopicSingleMsgHandler.java @@ -2,7 +2,6 @@ import com.genexus.cloud.serverless.helpers.ServiceBusProcessedMessage; import com.genexus.cloud.serverless.helpers.ServiceBusSingleMessageProcessor; -import com.genexus.cloud.serverless.model.*; import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.annotation.BindingName; import com.microsoft.azure.functions.annotation.Cardinality; @@ -26,16 +25,7 @@ public void run( setupServerlessMappings(context.getFunctionName()); ServiceBusSingleMessageProcessor queueSingleMessageProcessor = new ServiceBusSingleMessageProcessor(); ServiceBusProcessedMessage queueMessage = queueSingleMessageProcessor.processQueueMessage(executor,messageId,enqueuedTimeUtc,context,message); - try { - EventMessageResponse response = dispatchEvent(queueMessage.getEventMessages(),queueMessage.getRawMessage()); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + ExecuteDynamic(queueMessage.getEventMessages(),queueMessage.getRawMessage()); } } diff --git a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureTimerHandler.java b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureTimerHandler.java index 2f7b17f20..2a965391f 100644 --- a/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureTimerHandler.java +++ b/gxazureserverless/src/main/java/com/genexus/cloud/serverless/azure/handler/AzureTimerHandler.java @@ -3,16 +3,13 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; - -import com.genexus.cloud.serverless.model.*; - import com.microsoft.azure.functions.annotation.*; import com.microsoft.azure.functions.ExecutionContext; - import java.time.Instant; import java.util.List; import java.util.*; +import com.genexus.cloud.serverless.model.*; public class AzureTimerHandler extends AzureEventHandler { @@ -24,10 +21,8 @@ public void run( final ExecutionContext context) throws Exception { context.getLogger().info("GeneXus Timer trigger handler. Function processed: " + context.getFunctionName() + " Invocation Id: " + context.getInvocationId()); - setupServerlessMappings(context.getFunctionName()); EventMessages msgs = new EventMessages(); - if (executor.getMethodSignatureIdx() == 0) { try { TimerObject timerObject = new ObjectMapper().readValue(TimerInfo, new TypeReference() { @@ -35,38 +30,24 @@ public void run( EventMessage msg = new EventMessage(); msg.setMessageId(context.getInvocationId()); msg.setMessageSourceType(EventMessageSourceType.TIMER); - Instant nowUtc = Instant.now(); msg.setMessageDate(Date.from(nowUtc)); - List msgAtts = msg.getMessageProperties(); - msgAtts.add(new EventMessageProperty("Id", context.getInvocationId())); boolean adjustForSDT = timerObject.timerSchedule.getAdjustForDST(); msgAtts.add(new EventMessageProperty("AdjustForDST", Boolean.toString(adjustForSDT))); msgAtts.add(new EventMessageProperty("Next", timerObject.timerScheduleStatus.getNext())); msgAtts.add(new EventMessageProperty("Last", timerObject.timerScheduleStatus.getLast())); msgAtts.add(new EventMessageProperty("LastUpdated", timerObject.timerScheduleStatus.getLastUpdated())); - boolean isPastDue = timerObject.getIsPastDue(); msgAtts.add(new EventMessageProperty("IsPastDue", Boolean.toString(isPastDue))); - msgs.add(msg); } catch (Exception e) { - logger.error("Message could not be processed."); + context.getLogger().severe("Message could not be processed."); throw e; } } - try { - EventMessageResponse response = dispatchEvent(msgs, TimerInfo); - if (response.hasFailed()) { - logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); - throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. - } - } catch (Exception e) { - logger.error("HandleRequest execution error", e); - throw e; //Throw the exception so the runtime can Retry the operation. - } + ExecuteDynamic(msgs, TimerInfo); } public static class TimerObject{ diff --git a/gxazureserverless/src/test/java/com/genexus/cloud/azure/events/TestAzureQueueHandler.java b/gxazureserverless/src/test/java/com/genexus/cloud/azure/events/TestAzureQueueHandler.java index ad4a18d3c..de9e67f99 100644 --- a/gxazureserverless/src/test/java/com/genexus/cloud/azure/events/TestAzureQueueHandler.java +++ b/gxazureserverless/src/test/java/com/genexus/cloud/azure/events/TestAzureQueueHandler.java @@ -28,7 +28,6 @@ public void testQueueTriggerFunctionRaw() throws Exception { context = new MockExecutionContext("TestQueueRaw","13e2d1f9-6838-4927-a6a8-0160e8601ab0"); queueFunction.run(testMessage,id,dequeCount,expirationTime,insertionTime, nextVisibleTime,popReceipt,context); - assertNotNull(context.getLogger()); context.getLogger().info("Logger is not null"); @@ -45,7 +44,6 @@ public void testQueueTriggerFunctionMessage() throws Exception { context = new MockExecutionContext("TestQueueEventMessage","758093bf-68c1-47a5-8f93-cc2882e961e7"); queueFunction.run(testMessage,id,dequeCount,expirationTime,insertionTime, nextVisibleTime,popReceipt,context); - assertNotNull(context.getLogger()); context.getLogger().info("Logger is not null"); } diff --git a/gxserverlesscommon/src/main/java/com/genexus/cloud/serverless/ServerlessBaseEventHandler.java b/gxserverlesscommon/src/main/java/com/genexus/cloud/serverless/ServerlessBaseEventHandler.java index 69ddeeb1b..fe032f462 100644 --- a/gxserverlesscommon/src/main/java/com/genexus/cloud/serverless/ServerlessBaseEventHandler.java +++ b/gxserverlesscommon/src/main/java/com/genexus/cloud/serverless/ServerlessBaseEventHandler.java @@ -1,18 +1,21 @@ package com.genexus.cloud.serverless; +import com.genexus.Application; import com.genexus.ApplicationContext; +import com.genexus.ConfigFileFinder; import com.genexus.ModelContext; import com.genexus.cloud.serverless.model.EventMessageResponse; import com.genexus.cloud.serverless.model.EventMessages; import com.genexus.cloud.serverless.model.EventMessagesList; -import com.genexus.diagnostics.core.ILogger; import com.genexus.specific.java.Connect; import com.genexus.util.IniFile; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public abstract class ServerlessBaseEventHandler { + protected static Logger logger = LogManager.getLogger(ServerlessBaseEventHandler.class); protected T functionConfiguration; - protected static ILogger logger = null; protected Class entryPointClass = null; protected static GXProcedureExecutor executor; private static final String GX_APPLICATION_CLASS = "GXcfg"; @@ -35,18 +38,15 @@ public ServerlessBaseEventHandler(String functionName, String className) throws initialize(); } private void initialize() throws Exception { - - logger = com.genexus.specific.java.LogManager.initialize(".", ServerlessBaseEventHandler.class); Connect.init(); - - IniFile config = com.genexus.ConfigFileFinder.getConfigFile(null, "client.cfg", null); + IniFile config = ConfigFileFinder.getConfigFile(null, "client.cfg", null); String packageName = config.getProperty("Client", "PACKAGE", null); String cfgClassName = packageName.isEmpty() ? GX_APPLICATION_CLASS : String.format("%s.%s", packageName, GX_APPLICATION_CLASS); try { Class cfgClass; cfgClass = Class.forName(cfgClassName); logger.debug("Finished loading cfgClassName " + cfgClassName); - com.genexus.Application.init(cfgClass); + Application.init(cfgClass); ApplicationContext.getInstance().setPoolConnections(true); } catch (Exception e) { logger.error(String.format("Failed to initialize GX AppConfig Class: %s", cfgClassName), e); @@ -60,6 +60,30 @@ protected EventMessageResponse dispatchEvent(EventMessages eventMessages, String return dispatchEvent(eventMessages,eventMessagesList,rawMessageBody); } + protected void ExecuteDynamic(EventMessages msgs, String rawMessage) throws Exception { + try { + EventMessageResponse response = dispatchEvent(msgs, rawMessage); + if (response.hasFailed()) { + logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); + throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. + } + } catch (Exception e) { + logger.error(String.format("HandleRequest execution error: %s",e)); + throw e; //Throw the exception so the runtime can Retry the operation. + } + } + protected void ExecuteDynamic(EventMessages msgs, EventMessagesList eventMessagesList, String rawMessage) throws Exception { + try { + EventMessageResponse response = dispatchEvent(msgs, eventMessagesList, rawMessage); + if (response.hasFailed()) { + logger.error(String.format("Messages were not handled. Error: %s", response.getErrorMessage())); + throw new RuntimeException(response.getErrorMessage()); //Throw the exception so the runtime can Retry the operation. + } + } catch (Exception e) { + logger.error(String.format("HandleRequest execution error: %s",e)); + throw e; //Throw the exception so the runtime can Retry the operation. + } + } protected EventMessageResponse dispatchEvent(EventMessages eventMessages, EventMessagesList eventMessagesList, String rawMessageBody) throws Exception { String jsonStringMessages = JSONHelper.toJSONString(eventMessages);