Batch script

This is a simple example of a batch script (test-batch). It requires the time, logging, ska_ser_logging and ska_sdp_scripting Python modules:

import logging
import time

import ska_sdp_scripting
import ska_ser_logging

We initialise the logging:

ska_ser_logging.configure_logging()
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)

The first step is to claim the processing block and get its parameters.

pb = ska_sdp_scripting.ProcessingBlock()
parameters = pb.get_parameters()

We then need to request the input and output buffer reservations. This is just a placeholder currently to give an example of how resources will be requested.

in_buffer_res = pb.request_buffer(100e6, tags=['sdm'])
out_buffer_res = pb.request_buffer(parameters['length'] * 6e15 / 3600, tags=['visibilities'])

We declare the phases of the script and define which resource requests are needed for each phase. In the current implementation, we can only declare one phase, which in this example we call the ‘work’ phase:

work_phase = pb.create_phase('work', [in_buffer_res, out_buffer_res])

Next, we define the function to be executed by the execution engine. In a real script this would most likely be imported from a library of standard script functions.

def some_processing(duration: float):
    """
    Do some 'processing' for the required duration.
    """
    LOG.info('Starting processing for %f s', duration)
    time.sleep(duration)
    LOG.info('Finished processing')

We start the work phase using a with block. On entry, it waits until the resources are available and in the meantime it monitors the processing block state to check if it has been cancelled. Once the resources are available, it proceeds into the body of the with block and deploys a fake execution engine using the ee_deploy_test method to execute our function. The example we defined sleeps for the specified duration. This happens in a separate thread, so the method returns immediately. It then enters the loop at the end which waits until the execution is finished, and also monitors the processing block state and whether the deployment has been cancelled or not.

with work_phase:

    deploy = work_phase.ee_deploy_test(
        'test', some_processing, (parameters['duration'],)
    )

    for txn in work_phase.wait_loop():
        if deploy.is_finished(txn):
            break

On exiting the with block, it removes the execution engine deployment and updates the processing block state with the status of the script.