I guess as a start you can check existing services in different addons such as kitsu, ftrack, and etc.
Mostly, we have 2 folders in repos:
- “services”: where services implementation live. (basically, they are all python scripts + docker file for builder docker image where the code will run eventually)
- “service tools”: which includes helper tools for development like asking it to run a particular service.
I didn’t dig much in creating services before, but let me share some findings. Hope it helps wrapping your head around it.
Personally, as a start, I believe for a far very minimal service, one can start by writing their python implementation.
Here’s a very rough example which should outline the code flow.
Rough example
import ayon_api
def main_loop():
# while service is not stopped
...
def main():
try:
ayon_api.init_service()
connected = True
except Exception:
connected = False
if not connected:
...
# Register interrupt signal (it includes a clean up process)
...
main_loop()
if __name__ == "__main__":
main()
once this is setup, one can start exploring working with events:
from ayon_api import (
get_event,
get_events,
update_event,
dispatch_event,
create_event,
delete_event,
enroll_event_job,
)
I don’t think there’s a way to subscribe to events to act upon them in Realtime. Alternatively, I think it’s the other where you implement a listener and just check the server events periodically for the event of interest.
Tip: In addon server side you can implement a web hook to be run when a specific event is created. (which sounds like subscribing to events)
Register Event Hook
class MyCooolAddon(BaseServerAddon):
async def setup(self):
register_event_hook("entity.folder.created", self.on_folder_created)
async def on_folder_created(self, event):
"""put a comment on every new folder"""
await create_activity(
project_name=event.project,
activity_type="comment",
body=f"Hoooo, i have been created by f{event.user}!",
entity_references=[
{"type": "origin", "entity_type": "folder", "entity_id": event.summary["folderId"}
]
user_references=[{"type": "author", "name": event.user}]
)
You can also stop listening via:
# Stop listen
event_callback.deregister()