Hello ,
I'm trying to implement multithreaded HTTP communication in a QAction by following the instructions in the (HTTP | DataMiner Docs) but I'm running into a couple of issues and would appreciate some guidance.
I have configured a multithreaded timer with:
-
QActionBefore (QAction 4) → creates the HTTP request
-
Group trigger (QAction 3) → processes the HTTP response
-
QActionAfter (QAction 5) → handles timeouts
Issue
- In QAction 3, when I try to retrieve the response using:
var result = protocol.OldRow();
it always returns null. In Stream Viewer, I see the following message:" Get for Multi threaded Timer - Periodic Monitoring {device-id} [0 ms] returned VT_EMPTY " for every row in the table (The table stores device URLs, and for each row we retrieve the URL and make an HTTP call to the device to check its status.)
But when I test the same HTTP GET call in Postman, it works correctly, so the endpoint itself seems fine.
- Another question is about performance. In
QAction_3I'm retrieving the table row like this:
var row = (DarwindevicesQActionRow)protocol.darwindevices.GetRow(rowKey);
This call is happening in a loop because of row processing by multithreaded timer, so I'm wondering if this might affect performance when many rows are processed.
So my questions are:
-
Why would
protocol.OldRow()return null instead of http response and how the http call is being made by Dataminer? -
Is there a more efficient way to retrieve rows instead of calling
GetRow()in the loop?
Any suggestions would be really helpful. Thanks!!
Timer:
<Timer id="4" options="ip:2000,1;each:120000;threadPool:10;qactionBefore:4;qactionAfter:5;">
<Name>HTTP Timer</Name>
<Time>10000</Time>
<Interval>0</Interval>
<Content>
<Group>3</Group>
</Content>
</Timer>
Group:
<Group id="3">
<Name>Multi threaded Timer - Periodic Monitoring</Name>
<Description>Multi threaded Timer - Periodic Monitoring</Description>
<Type>poll</Type>
<Content/>
</Group>
QAction:
<QAction id="4" name="Get Darwin Status - Create HTTP Request" encoding="csharp" row="true">
</QAction>
<QAction id="3" name="Periodic Darwin Software Monitoring" encoding="csharp" triggers="3" row="true" options="group" >
</QAction>
<QAction id="5" name="Get Darwin Status - After Timeout" encoding="csharp" row="true">
</QAction>
QActionBefore (QAction_4) - Create Http Request
using System;
using System.Text;
using OMS.Library.DataMiner.Generics;
using Skyline.DataMiner.Scripting;
public class QAction
{
/// <summary>
/// Create HTTP Request
/// </summary>
/// <param name="protocol">Link with SLProtocol process.</param>
/// <returns>HTTP Request</returns>
public static object[] Run(SLProtocol protocol)
{
try
{
var rowKey = protocol.RowKey();
protocol.Log("Row Key: " + rowKey);
var row = (DarwindevicesQActionRow)(object[])protocol.GetRow(Parameter.Darwindevices.tablePid, rowKey);
string baseUrl = row.Darwindeviceurl?.ToString();
string path = "api/system";
string method = "GET";
const int timeout = 10000;
string userName = row.Darwindeviceusername?.ToString();
string password = row.Darwindevicepassword?.ToString();
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{userName}:{password}"));
string authHeader = $"Authorization|Basic {credentials}";
string requestHeaders = $"Accept|*/*;Accept-Encoding|gzip, deflate, br;Cache-Control|no-cache;{authHeader}";
string[] requestSettings = { method, baseUrl.TrimEnd('/') + "/", userName, password, requestHeaders ,timeout.ToString(),"true" };
string[] resourcePaths = { path };
protocol.SetParameterIndexByKey(Parameter.Darwindevices.tablePid, rowKey, Parameter.Darwindevices.Idx.darwinapistatus_2005 + 1, "running");
protocol.Log("resourcePaths: " + string.Join(", ", resourcePaths));
protocol.Log("requestSettings: " + string.Join(", ", requestSettings));
return new object[] { "http", requestSettings, resourcePaths };
}
catch (Exception ex)
{
Generic.Log(protocol, "Create HTTP Request", ex.Message);
}
return null;
}
}
QAction_3 (Triggered by Group of multithreaded timer - process the response)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common.Methods;
using Newtonsoft.Json.Linq;
using OMS.Library.DataMiner.Dms;
using OMS.Library.DataMiner.Extensions;
using Skyline.DataMiner.Scripting;
using static Skyline.DataMiner.Scripting.Parameter;
/// <summary>
/// DataMiner QAction Class.
/// </summary>
public class QAction
{
//private static readonly int _batchSize = 10;
/// <summary>
/// The QAction entry point.
/// </summary>
/// <param name="protocol">Link with SLProtocol process.</param>
public void Run(SLProtocolExt protocol)
{
try
{
var rowKey = protocol.RowKey();
var result = protocol.OldRow();
protocol.Log( "key: "+rowKey);
if (result == null)
{
protocol.Log("Result is null.", LogType.Error, LogLevel.NoLogging);
return;
}
string communicationState = Convert.ToString(result);
protocol.Log("Communication state: " + communicationState);
if (communicationState != "TIMEOUT" && communicationState != "NO POLLING OCCURRED" && communicationState != "NO POLLING OCCURED")
{
var responses = (object[])result;
protocol.Log("Received responses: " + responses.ToJson());
if (responses.Length <= 1) return;
var row = (DarwindevicesQActionRow)protocol.darwindevices.GetRow(rowKey); // BETTER WAY OF DOING IT?
var statusLines = (object[])responses[0];
var messageBodies = (object[])responses[1];
for (int i = 0; i < statusLines.Length; i++)
{
string statusLine = Convert.ToString(statusLines[i]);
string response = Convert.ToString(messageBodies[i]);
protocol.Log("Status Line: " + statusLine);
if (statusLine == "HTTP/1.1 200 OK")
{
row.Darwinapistatus_2005 = "200 OK";
}
else
{
row.Darwinapistatus_2005 = "ERROR";
}
}
protocol.darwindevices.SetRow(row);
}
}
catch (Exception e)
{
protocol.Log("QA" + protocol.QActionID + "|Run|An exception occurred: " + e.ToString(), LogType.Error, LogLevel.NoLogging);
}
}
}