1. Home
  2. Workflow & Automation
  3. Windows Services
  4. Processing Queued Commands Using a Windows Service

Processing Queued Commands Using a Windows Service

This article will provide a code overview as well as how to configure your windows service to process asynchronous commands queued by users in your trellispark environment. Everything in this article depends on a service using the trellispark generic windows service template. To learn how to create your own generic service, please refer to this article.

Process Queued Commands

The first step is to pull the list of command queues from the service definition.

 ExecuteSQLInformation mySQL = new()
 {
     SessionGUID = userData.SessionGUID,
     UserGUID = userData.UserGUID,
     CurrentInstanceGUID = ServiceGUID,
     CommandName = "Get_ZSO_CommandQueue"
 };
 MyDB.ExecuteTSQLDataSet(mySQL);

Then for each command queue, we pull a list of all current commands on the queue.

foreach (XElement queue in commandQueues.Elements("Table"))
{
    // Read all commands currently available for this command queue.
    // Marks all commands as Executing to prevent duplicate executions.
    string queueName = queue.GetElement("Name");
    mySQL = new()
    {
        SessionGUID = userData.SessionGUID,
        UserGUID = userData.UserGUID,
        CurrentInstanceGUID = ServiceGUID,
        AdditionalParameters = queueName,
        CommandName = "GI_QueuedCommand_Read"
    };
    MyDB.ExecuteTSQLDataSet(mySQL);

Then for each command in the command queue, it attempts to run the command.

foreach (XElement command in queuedCommands.Elements("Table"))
{
    // Execute the command.
    await ExecuteCommand(userData, ServiceGUID, queueName, command);
}

The command for Execute Command follows the path below.

Execute Command

First the Execute Command method parses the command into an ExecuteTargetInformation object. Then uses the ExecuteTarget infrastructure to execute the command.

ExecuteTargetInformation queuedCommand = new()
{
    CurrentInstanceGUID = command.GetElementGUID("CurrentInstanceGUID"),
    TargetInstanceGUID = command.GetElementGUID("TargetInstanceGUID"),
    UserGUID = command.GetElementGUID("UserGUID"),
    SessionGUID = command.GetElementGUID("RequestGUID"),
    AdditionalParameters = parameters.ToString(),
    RequestGUID = command.GetElementGUID("RequestGUID")
};
ExecuteTarget executeTarget = new(MyDB);
// Execute the command.
await executeTarget.CallTarget(queuedCommand, true);

Once the command has been called, the result of the command is logged both on success and failure. If an error was thrown, that error is logged as well.

 // Log the execution result.
 ExecuteSQLInformation LogResultCommand = new()
 {
     SessionGUID = userData.SessionGUID,
     UserGUID = queuedCommand.UserGUID,
     CurrentInstanceGUID = queuedCommand.RequestGUID,
     AdditionalParameters = queuedCommand.ErrorMessage,
     CommandName = "GI_QueuedCommand_Result"
 };
 await MyDB.ExecuteTSQLValues(LogResultCommand);

 // If there was an error, log the error.
 if (queuedCommand.ErrorMessage != "")
 {
     await AppInsights.Log(1, "ServiceLogic.ProcessCommandQueues - Failed to process command", $"ServiceGUID: {ServiceGUID} CommandQueue: {queueName} Error: {queuedCommand.ErrorMessage}", MyDB);
 }

Configuration

To configure a trellispark message queue for processing, first open the Workspace application in the workspace and navigate to the Service tab. Then open the service to configure.

Service Definition

When you are looking at the page above, click the “Add new item” button in the Command Queues childlist. You will be presented with the following page.

Command Queue Overview

Provide a human readable name in the Name textbox. When creating asynchronous commands, Paste the command queue GUID into the Command Queue textbox.

Updated on March 6, 2024

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support

Leave a Comment