Persistent Variables

Note!

This section is only relevant for Python agents in batch workflows.

Persistent variables are variables that are persisted after the endBatch function block has been executed for the processing agent, or after the endBatch() function has been called for the collection agent. They are read from the persistence store just before the initialize block is executed.

Use persistent variables if you have any transaction state that must be available in the commit or rollback function blocks to finalize a transaction.

A persistent variable must be defined as a global variable. You can specify a default value in the constructor, otherwise the default value is None. A persistent variable has the property value, which you use to get or set the persistent variable value.

The value of a persistent variable must be compatible with drany. For further information, see APL and Ultra Field Types for Python.

Note!

A persistent variable is not the same as a normal Python global variable. A normal global variable is not persisted between executions of a workflow. Do not rely on normal global variables for any state necessary to finalize a transaction.

Example - Persistent variables for Python collection agent

counter = persistent(0) # Defines a persistent variable with default value 0
start = persistent(drdate.now()) # Defines another persistent variable with the start date

def initialize():
    if counter.value == 0:
        debug('This is the first time this workflow has processed batches')
    else:
        debug('This workflow has processed batches before')

def execute():
    debug('execute')
    beginBatch() # Starts a new batch
    counter.value += 1
    endBatch() # Ends the batch, all persistent variables have been persisted when this call returns
 
def commit():
    debug('commit')
    # The persistent variables are available in commit during both normal and recovery commits
    debug(counter)
    debug(start)

Example - Persistent variables for Python processing agent

counter = persistent(0) # Defines a persistent variable with default value 0

def endBatch():
    debug('endBatch')
    counter.value += 1
    # The persistent variable is persisted after this function block has been executed

def commit():
    debug('commit')
    # The persistent variable is available in commit during both normal and recovery commits
    debug(counter)