Skip to main content
Version: 1.6.x

Custom Events

Custom events (events.NormalType) are freely defined by developers within service elements according to requirements and are actively triggered within business logic code. They provide a high degree of flexibility for developing event-driven systems.

Custom events are suitable for scenarios such as decoupling business logic, asynchronous task processing, and cross-service communication. For example:

  • Business Decoupling: After a user registers, an event is triggered to send an email and SMS. The registration logic does not need to be concerned with the notification details.
  • Asynchronous Processing: Trigger a time-consuming data analysis task, return a response immediately, and execute the task asynchronously in the background.
  • Extension Mechanism: Allow third-party modules to subscribe to core business events for functional extension.

The hierarchical structure of custom event elements is Meta (events.Meta) → Type (events.NormalType) → Instance. Developers can quickly create custom event instance elements using visual development tools.

Quick Start​

Prerequisites​

Developers must first define the event within a service element before creating a Custom Event instance element to subscribe to that event. See: Event Definition in Service Elements

Creating Instance Elements​

Directory Structure​

Create a new event directory (e.g., MyCustomEvent) under the events/ directory. The standard structure is as follows:

events/
└── MyCustomEvent/ # [Directory] Event element name
├── e.json # [File] Core configuration file
├── inner.py # [File] (Optional) Internal execution logic code
└── __init__.py # [File] Python package identifier

e.json​

events/MyCustomEvent/e.json
{
"type": "events.NormalType",
"title": "Test Custom Event",
"sender": "services.MyService.CustomEvent",
"funcType": "Inner",
"asyncType": false,
"enable": 1,
"backendBundleEntry": "."
}

inner.py​

events/MyCustomEvent/inner.py
def customFunc(*args, **kwargs):
"""
Function name must be customFunc
:param args: Positional arguments
:param kwargs: Keyword arguments
"""
# When `funcType` is `Global`, this function does not need to be implemented.
# Business logic implementation
print(f"Custom event triggered: args={args}, kwargs={kwargs}")
return True

__init__.py​

events/MyCustomEvent/__init__.py
from .inner import customFunc

Element Configuration​

e.json Configuration​

Field NameTypeRequiredDescriptionExample
typeStringYesFixed value"events.NormalType"
titleStringNoEvent display name"Test Custom Event"
senderStringYesEvent sender, format: service element fullName.event name"services.MyService.CustomEvent"
funcTypeStringNoFunction type: "Global" | "Inner""Global" (Default/Recommended)
funcStringConditionalRequired when funcType is "Global". Points to the service function path"services.NotifySvc.send_email"
asyncTypeBooleanNoWhether to execute asynchronouslyfalse (Default)
objModeBooleanNoObject mode. If enabled, parameters are wrapped in objfalse (Default)
enableIntegerNo1: Enabled, 0: Disabled1 (Default)
backendBundleEntryStringYesBackend load entry, fixed as ".""."
returnTypeStringNoReturn value type"None"
pathStringNoRelative path of the element directory"events"
extendTypeStringNoInheritance extension type"self"

Execution Function​

Function Arguments​

Input parameters vary based on the objMode configuration.

Normal Mode (objMode: false)​

Parameter NameTypeDescription
argstuplePositional arguments
kwargsdictKeyword arguments

Object Mode (objMode: true)​

Parameter NameTypeDescription
objdictDictionary object containing all parameters

Function Body​

Suitable for reusing existing Service logic. Set funcType to "Global" and func to point to the Service function.

services/NotifySvc/service.py
from services.NormalType import NormalService

class NotifySvc(NormalService):
def send_email(self, *args, **kwargs):
email = kwargs.get('email')
print(f"Sending email to: {email}")

Event Internal Function​

Suitable when the logic belongs exclusively to this event. Set funcType to "Inner" and implement it in inner.py.

events/MyCustomEvent/inner.py
def customFunc(*args, **kwargs):
"""
Function name must be customFunc
"""
# Business logic
print("Custom event logic execution")
return True