IBM QRadar SOAR

IBM QRadar

Join this online user group to communicate across Security product users and IBM experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only

Using Circuits Timers to create components which are delayed or repeating in nature.

  • 1.  Using Circuits Timers to create components which are delayed or repeating in nature.

    Posted Tue October 16, 2018 11:31 AM
    Edited by Ryan Gordon Wed October 17, 2018 04:35 AM
    The integration framework for Resilient Circuits is based on a technology called Circuits. circuits is a Lightweight Event driven and Asynchronous Application Framework for the Python Programming Language with a strong Component Architecture[Source].

    Depending on how you are setting up your component, you may need to create a class which inherits from the Component class in the circuits package. 

    Here is an example component which inherits both the ResilientComponent and the circuits Component class:
    from circuits import Component
    from resilient_circuits import ResilientComponent
    
    class MyComponent(ResilientComponent,Component):
        channel = "my-component"
    
        def __init__(self):
    
    
        def make_api_call():
            # Implementation logic
    The above example can make use of Timers AND has access to the Resilient REST API Client.


    Creating a component which repeats some function or executes a function once after X seconds.
    Disclaimer: The below samples will need some modification to run as-is and are intended as a guide. The most important concepts are the notion of creating an Event which targets a function and registering this Event with a Timer

    Delayed Components:
    You may have a use-case where some component you created must wait a certain amount of seconds before executing. Maybe you wish for some other component / function to complete or you depend on some service which takes time to initialise. In this case you can use a Circuits Timer which delays execution.

    Example: 
    from circuits import Component, Event, Timer
    from resilient_circuits import ResilientComponent
    
    class MyComponent(ResilientComponent,Component):
        channel = "delayed-component"
    
        def __init__(self):
            self.started()
    
    
        def make_api_call():
            # Implementation logic
            print("Impl")
    
        def started(self):
            event = Event.create("make_api_call")
            Timer(30, event).register(self)

    In the above example, we create an event based on the make_api_call function and we then setup a Timer which will execute this Event after 30 seconds.

    Repeating Components:
    Alternatively you may have a use-case where some component you created could benefit from executing every X seconds. An example of this would be a component which polls some external resource periodically for information.  In this case you can use a Circuits Timer which repeats execution.

    Example:
    from circuits import Component, Event, Timer
    from resilient_circuits import ResilientComponent
    
    class MyComponent(ResilientComponent,Component):
        channel = "repeating-component"
    
        def __init__(self):
            self.started()
    
    
        def make_api_call():
            # Implementation logic
            print("Impl")
    
        def started(self):
            event = Event.create("make_api_call")
            Timer(30, event, persist=True).register(self)


    In this example, we create logic which is almost identical to the Delayed Component however the difference is that we make use of the 'persist' attribute present on the Timer class. This means rather than executing once after 30 seconds, this component will execute every 30 seconds.

    For more information on the Circuits framework and its different features such as Events and Timers, review the Circuits Documentation



    ------------------------------
    Ryan Gordon
    ------------------------------