Skip to content

Commit e85886f

Browse files
committed
Add template for event emitter plugins
1 parent ad268aa commit e85886f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
"""
3+
template.py
4+
5+
An ansible-rulebook event emitter plugin template.
6+
7+
Examples:
8+
sources:
9+
- template:
10+
11+
"""
12+
import asyncio
13+
from typing import Any, Dict
14+
15+
16+
async def main(queue: asyncio.Queue, args: Dict[str, Any]):
17+
# The main entrypoint for the emitter plugin.
18+
# This function is called by the rulebook engine.
19+
# The queue is used to send messages from the rulebook engine to the plugin.
20+
# The args dictionary contains the arguments passed to the plugin in the rulebook.
21+
delay = args.get("delay", 0)
22+
23+
while True:
24+
# Get a message from the queue.
25+
# This is a blocking call, so the plugin will wait here until a message is available.
26+
message = await queue.get()
27+
# Do something with the message.
28+
# This is where you would send the message to a remote service.
29+
# If the message is None, then the rulebook engine is shutting down.
30+
if message is None:
31+
break
32+
# Here we just print the message as an example.
33+
print(message)
34+
35+
36+
if __name__ == "__main__":
37+
# Test your plugin locally by running this file directly.
38+
39+
# We use the MockQueue class to simulate the queue for testing.
40+
# In production, the queue will be provided by the rulebook engine.
41+
class MockQueue:
42+
count = 10
43+
async def get(self):
44+
self.count -= 1
45+
if self.count == 0:
46+
return None
47+
else:
48+
return {'message': 'hello world'}
49+
50+
mock_arguments = dict()
51+
asyncio.run(main(MockQueue(), mock_arguments))
52+

0 commit comments

Comments
 (0)