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():
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():
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():
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
------------------------------