Function Blocks for Agents in Real-Time and Batch Workflows
The function blocks described in this section can be used in real-time and batch workflows. The available function blocks are:
initialize
execute
consume
stop
deinitialize
initialize
This function block initializes resources and state.
def initialize()The initialize function block is executed once for each invocation of the workflow and enables you to assign, for example, a global variable with an initial value.
Note!
Avoid reading MIM parameters from within initialize. The order by which agents are initialized is undefined and MIM parameters are therefore not necessarily set during the initialize phase.
The udrRoute function cannot be used in the initialize block.
Example - initialize function block
def initialize():
debug('initialize called')
execute
Note!
This function block only applies for the Python collection agents.
This function block is the main entry point for collection.
def execute()Example - execute function block in a real-time workflow
import random
from time import sleep
def execute():
counter = 0
while not isStopped():
counter += 1
udr = RandomUDR()
udr.sequence = counter
udr.random = random.randint(0, 100)
udrRoute(udr)
sleep(1)Example - execute function block in a batch workflow
import random
def execute():
counter = 0
while counter < 10 and not isStopped():
counter += 1
beginBatch()
udr = RandomUDR()
udr.sequence = counter
udr.random = random.randint(0, 100)
udrRoute(udr)
endBatch()consume
Note!
This function block does not apply for the Python collection agent for batch workflows.
This function block consumes and processes UDRs that are routed to the agent.
def consume(input)The consume function block is executed for each UDR or bytearray passing the agent. Within a consume block, validation, modification and routing can be performed. Each UDR or bytearray is referred to by the input variable.
Example - consume function block
def consume(input):
input.sequence = input.sequence + 1
udrRoute(input)
stop
This function block is called when the workflow is about to stop.
def stop()Note!
If you implement this function block, ensure to implement the stop behavior of your code properly by breaking any long running loops etc, that may run after receiving the stop signal. This is because the agent does not automatically break any loops inserted in your code. Your code may also run faster for some workloads when this function block is implemented as your code runs unaltered.
Example - stop function block
def stop():
debug('stop called')
deinitialize
This function block is executed right before the workflow stops.
def deinitialize()Use the deinitialize block to clean and close resources, for instance external connections.
Example - deinitialize function block
def deinitialize():
debug('deinitialize called')