WaitForKey

A simple loop to inspect the context for a specific key. If the key is not found after "iterations" loops, the script exits with a message.

python · xMatters

Source

import traceback
from typing import Any

import demistomock as demisto
from CommonServerPython import *


def wait_for_key(args: dict[str, Any]):
    context_key = args.get("context_key", "None")

    max_iterations = 10
    try:
        max_iterations = int(args.get("iterations", 10))
    except (ValueError, TypeError):
        return_error('Please provide an integer value for "iterations"')

    demisto_context = demisto.context()
    itr = 0
    done = False
    while not done and itr < max_iterations:
        if demisto_context.get(context_key) is not None:
            done = True
        demisto.executeCommand("Sleep", {"seconds": "1"})
        itr = itr + 1

    if done is False:
        readable_output = f'Could not find "{context_key}" after "{itr!s}" iterations'
    else:
        readable_output = f'Found "{context_key}" after "{itr!s}" iterations'

    return CommandResults(readable_output=readable_output)


def main():
    try:
        args = demisto.args()
        return_results(wait_for_key(args))

    except Exception as ex:
        demisto.error(traceback.format_exc())  # print the traceback
        return_error(f"Failed to execute wait_for_key. Error: {ex!s}")


""" ENTRY POINT """

if __name__ in ("__main__", "__builtin__", "builtins"):
    main()

README

Waits for a context value to appear in the incident or 10 loops and exits.

Script Data


Name Description
Script Type python
Tags utility

Dependencies


This script uses the following commands and scripts.

  • sleep

Inputs


Argument Name Description
context_key The incident context key to check for value
iterations The maximum number of loops (and seconds) to wait before exiting if the context isn’t found.

Outputs


There are no outputs for this script.