Skip to content

Commit

Permalink
Azure Func : Fixes retry behaviour and change property name for compa…
Browse files Browse the repository at this point in the history
…tibility with lambda (#773)
  • Loading branch information
sjuarezgx authored Mar 14, 2023
1 parent 229d9fd commit 217cb41
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,61 +130,62 @@ private void ProcessMessage(FunctionContext context, ILogger log, QueueMessage q
{
//Initialization

Type EventMessagesType = parameters[0].ParameterType; //SdtEventMessages
GxUserType EventMessages = (GxUserType)Activator.CreateInstance(EventMessagesType, new object[] { gxcontext }); // instance of SdtEventMessages
Type eventMessagesType = parameters[0].ParameterType; //SdtEventMessages
GxUserType EventMessages = (GxUserType)Activator.CreateInstance(eventMessagesType, new object[] { gxcontext }); // instance of SdtEventMessages

IList EventMessage = (IList)ClassLoader.GetPropValue(EventMessages, "gxTpr_Eventmessage");//instance of GXBaseCollection<SdtEventMessage>
Type EventMessageItemType = EventMessage.GetType().GetGenericArguments()[0];//SdtEventMessage
IList eventMessage = (IList)ClassLoader.GetPropValue(EventMessages, "gxTpr_Eventmessage");//instance of GXBaseCollection<SdtEventMessage>
Type EventMessageItemType = eventMessage.GetType().GetGenericArguments()[0];//SdtEventMessage

GxUserType EventMessageItem = (GxUserType)Activator.CreateInstance(EventMessageItemType, new object[] { gxcontext }); // instance of SdtEventMessage
IList CustomPayload = (IList)ClassLoader.GetPropValue(EventMessageItem, "gxTpr_Eventmessagecustompayload");//instance of GXBaseCollection<SdtEventCustomPayload_CustomPayloadItem>
GxUserType eventMessageItem = (GxUserType)Activator.CreateInstance(EventMessageItemType, new object[] { gxcontext }); // instance of SdtEventMessage

Type CustomPayloadItemType = CustomPayload.GetType().GetGenericArguments()[0];//SdtEventCustomPayload_CustomPayloadItem
IList eventMessageProperties = (IList)ClassLoader.GetPropValue(eventMessageItem, "gxTpr_Eventmessageproperties");//instance of GXBaseCollection<GeneXus.Programs.genexusserverlessapi.SdtEventMessageProperty>
Type eventMessPropsItemType = eventMessageProperties.GetType().GetGenericArguments()[0];//SdtEventMessageProperty

//Payload
//Event message properties

GxUserType CustomPayloadItem;
GxUserType eventMessageProperty;

foreach (var messageProp in queueMessage.MessageProperties)
{
CustomPayloadItem = CreateCustomPayloadItem(CustomPayloadItemType, messageProp.key, messageProp.value, gxcontext);
CustomPayload.Add(CustomPayloadItem);
eventMessageProperty = CreateEventMessageProperty(eventMessPropsItemType, messageProp.key, messageProp.value, gxcontext);
eventMessageProperties.Add(eventMessageProperty);
}

//Body

CustomPayloadItem = CreateCustomPayloadItem(CustomPayloadItemType, "Body", queueMessage.Body, gxcontext);
CustomPayload.Add(CustomPayloadItem);
eventMessageProperty = CreateEventMessageProperty(eventMessPropsItemType, "Body", queueMessage.Body, gxcontext);
eventMessageProperties.Add(eventMessageProperty);

//Event

ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessageid", queueMessage.Id);
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessagedata", queueMessage.Body);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessageid", queueMessage.Id);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessagedata", queueMessage.Body);

QueueMessage.MessageProperty InsertionTimeProp = queueMessage.MessageProperties.Find(x => x.key == "InsertionTime");
if (InsertionTimeProp != null)
{
DateTime InsertionTime;
if (DateTime.TryParse(InsertionTimeProp.value, out InsertionTime))
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessagedate", InsertionTime.ToUniversalTime());
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessagedate", InsertionTime.ToUniversalTime());
}
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessagesourcetype", EventSourceType.QueueMessage);
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessageversion", string.Empty);
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessagecustompayload", CustomPayload);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessagesourcetype", EventSourceType.QueueMessage);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessageversion", string.Empty);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessageproperties", eventMessageProperties);

//List of Events
EventMessage.Add(EventMessageItem);
eventMessage.Add(eventMessageItem);
parametersdata = new object[] { EventMessages, null };
}
try
{
method.Invoke(objgxproc, parametersdata);
GxUserType EventMessageResponse = parametersdata[1] as GxUserType;//SdtEventMessageResponse
bool result = (bool)ClassLoader.GetPropValue(EventMessageResponse, "gxTpr_Handled");
bool result = (bool)ClassLoader.GetPropValue(EventMessageResponse, "gxTpr_Handlefailure");


//Error handling

if (result == false) //Must retry if possible.
if (result == true) //Must retry if possible.
{
exMessage = string.Format("{0} {1}", FunctionExceptionType.AppError, ClassLoader.GetPropValue(EventMessageResponse, "gxTpr_Errormessage"));
throw new Exception(exMessage);
Expand Down Expand Up @@ -219,14 +220,15 @@ private void ProcessMessage(FunctionContext context, ILogger log, QueueMessage q
throw new Exception(exMessage);
}
}
private GxUserType CreateCustomPayloadItem(Type customPayloadItemType, string propertyId, object propertyValue, GxContext gxContext)

private GxUserType CreateEventMessageProperty(Type eventMessPropsItemType, string propertyId, object propertyValue, GxContext gxContext)
{
GxUserType CustomPayloadItem = (GxUserType)Activator.CreateInstance(customPayloadItemType, new object[] { gxContext });
ClassLoader.SetPropValue(CustomPayloadItem, "gxTpr_Propertyid", propertyId);
ClassLoader.SetPropValue(CustomPayloadItem, "gxTpr_Propertyvalue", propertyValue);
return CustomPayloadItem;

GxUserType eventMessageProperty = (GxUserType)Activator.CreateInstance(eventMessPropsItemType, new object[] { gxContext }); // instance of SdtEventMessageProperty
ClassLoader.SetPropValue(eventMessageProperty, "gxTpr_Propertyid", propertyId);
ClassLoader.SetPropValue(eventMessageProperty, "gxTpr_Propertyvalue", propertyValue);
return eventMessageProperty;
}

[DataContract]
internal class QueueMessage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ public void Run(string myQueueItem, FunctionContext context)
throw;
}
}
private GxUserType CreateCustomPayloadItem(Type customPayloadItemType, string propertyId, object propertyValue, GxContext gxContext)
private GxUserType CreateEventMessageProperty(Type eventMessPropsItemType, string propertyId, object propertyValue, GxContext gxContext)
{
GxUserType CustomPayloadItem = (GxUserType)Activator.CreateInstance(customPayloadItemType, new object[] { gxContext });
ClassLoader.SetPropValue(CustomPayloadItem, "gxTpr_Propertyid", propertyId);
ClassLoader.SetPropValue(CustomPayloadItem, "gxTpr_Propertyvalue", propertyValue);
return CustomPayloadItem;

GxUserType eventMessageProperty = (GxUserType)Activator.CreateInstance(eventMessPropsItemType, new object[] { gxContext }); // instance of SdtEventMessageProperty
ClassLoader.SetPropValue(eventMessageProperty, "gxTpr_Propertyid", propertyId);
ClassLoader.SetPropValue(eventMessageProperty, "gxTpr_Propertyvalue", propertyValue);
return eventMessageProperty;
}
private Message SetupMessage(FunctionContext context, string item)
{
Expand Down Expand Up @@ -155,41 +154,41 @@ private void ProcessMessage(FunctionContext context, ILogger log, Message messag
{
//Initialization

Type EventMessagesType = parameters[0].ParameterType; //SdtEventMessages
GxUserType EventMessages = (GxUserType)Activator.CreateInstance(EventMessagesType, new object[] { gxcontext }); // instance of SdtEventMessages
Type eventMessagesType = parameters[0].ParameterType; //SdtEventMessages
GxUserType eventMessages = (GxUserType)Activator.CreateInstance(eventMessagesType, new object[] { gxcontext }); // instance of SdtEventMessages

IList EventMessage = (IList)ClassLoader.GetPropValue(EventMessages, "gxTpr_Eventmessage");//instance of GXBaseCollection<SdtEventMessage>
Type EventMessageItemType = EventMessage.GetType().GetGenericArguments()[0];//SdtEventMessage
IList eventMessage = (IList)ClassLoader.GetPropValue(eventMessages, "gxTpr_Eventmessage");//instance of GXBaseCollection<SdtEventMessage>
Type eventMessageItemType = eventMessage.GetType().GetGenericArguments()[0];//SdtEventMessage

GxUserType EventMessageItem = (GxUserType)Activator.CreateInstance(EventMessageItemType, new object[] { gxcontext }); // instance of SdtEventMessage
IList CustomPayload = (IList)ClassLoader.GetPropValue(EventMessageItem, "gxTpr_Eventmessagecustompayload");//instance of GXBaseCollection<SdtEventCustomPayload_CustomPayloadItem>
GxUserType eventMessageItem = (GxUserType)Activator.CreateInstance(eventMessageItemType, new object[] { gxcontext }); // instance of SdtEventMessage

Type CustomPayloadItemType = CustomPayload.GetType().GetGenericArguments()[0];//SdtEventCustomPayload_CustomPayloadItem
IList eventMessageProperties = (IList)ClassLoader.GetPropValue(eventMessageItem, "gxTpr_Eventmessageproperties");//instance of GXBaseCollection<GeneXus.Programs.genexusserverlessapi.SdtEventMessageProperty>
Type eventMessPropsItemType = eventMessageProperties.GetType().GetGenericArguments()[0];//SdtEventMessageProperty

//Payload
GxUserType CustomPayloadItem;
GxUserType eventMessageProperty;

foreach (var messageProp in message.MessageProperties)
{
if ((messageProp.key != "UserProperties") & (messageProp.key != "SystemProperties"))
{
CustomPayloadItem = CreateCustomPayloadItem(CustomPayloadItemType, messageProp.key, Convert.ToString(messageProp.value), gxcontext);
CustomPayload.Add(CustomPayloadItem);
eventMessageProperty = CreateEventMessageProperty(eventMessPropsItemType, messageProp.key, Convert.ToString(messageProp.value), gxcontext);
eventMessageProperties.Add(eventMessageProperty);
}
}

//Body

CustomPayloadItem = CreateCustomPayloadItem(CustomPayloadItemType, "Body", message.Body, gxcontext);
CustomPayload.Add(CustomPayloadItem);
eventMessageProperty = CreateEventMessageProperty(eventMessPropsItemType, "Body", message.Body, gxcontext);
eventMessageProperties.Add(eventMessageProperty);

//user Properties
if (message.UserProperties.Count > 0)
{
foreach (string key in message.UserProperties.Keys)
{
CustomPayloadItem = CreateCustomPayloadItem(CustomPayloadItemType, key, JSONHelper.Serialize(message.UserProperties[key]), gxcontext);
CustomPayload.Add(CustomPayloadItem);
eventMessageProperty = CreateEventMessageProperty(eventMessPropsItemType, key, JSONHelper.Serialize(message.UserProperties[key]), gxcontext);
eventMessageProperties.Add(eventMessageProperty);
}
}

Expand All @@ -202,28 +201,28 @@ private void ProcessMessage(FunctionContext context, ILogger log, Message messag
foreach (var prop in sysProps)
if (prop.GetIndexParameters().Length == 0)
{
CustomPayloadItem = CreateCustomPayloadItem(CustomPayloadItemType, prop.Name, Convert.ToString(prop.GetValue(message.SystemProperties)), gxcontext);
CustomPayload.Add(CustomPayloadItem);
eventMessageProperty = CreateEventMessageProperty(eventMessPropsItemType, prop.Name, Convert.ToString(prop.GetValue(message.SystemProperties)), gxcontext);
eventMessageProperties.Add(eventMessageProperty);
}
}

//Event

ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessageid", message.MessageId);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessageid", message.MessageId);
Message.MessageProperty enqueuedTimeUtcProp = message.MessageProperties.Find(x => x.key == "EnqueuedTimeUtc");
if (enqueuedTimeUtcProp != null)
{
DateTime enqueuedTimeUtc;
if (DateTime.TryParse(enqueuedTimeUtcProp.value, out enqueuedTimeUtc))
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessagedate", enqueuedTimeUtc);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessagedate", enqueuedTimeUtc);
}
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessagesourcetype", EventSourceType.ServiceBusMessage);
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessageversion", string.Empty);
ClassLoader.SetPropValue(EventMessageItem, "gxTpr_Eventmessagecustompayload", CustomPayload);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessagesourcetype", EventSourceType.ServiceBusMessage);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessageversion", string.Empty);
ClassLoader.SetPropValue(eventMessageItem, "gxTpr_Eventmessageproperties", eventMessageProperties);

//List of Events
EventMessage.Add(EventMessageItem);
parametersdata = new object[] { EventMessages, null };
eventMessage.Add(eventMessageItem);
parametersdata = new object[] { eventMessages, null };
}
try
{
Expand All @@ -232,7 +231,7 @@ private void ProcessMessage(FunctionContext context, ILogger log, Message messag

//Error handling

if ((bool)ClassLoader.GetPropValue(EventMessageResponse, "gxTpr_Handled") == false) //Must retry
if ((bool)ClassLoader.GetPropValue(EventMessageResponse, "gxTpr_Handlefailure") == true) //Must retry
{
exMessage = string.Format("{0} {1}", FunctionExceptionType.AppError, ClassLoader.GetPropValue(EventMessageResponse, "gxTpr_Errormessage"));
throw new Exception(exMessage);
Expand Down
Loading

0 comments on commit 217cb41

Please sign in to comment.