View [code example](#code example)
Generally speaking, we take the logic that is time-consuming, consumes a lot of resources, or is prone to error, which is very suitable to be separated from the main request process and executed asynchronously. For example, when a new user registers, the system usually sends a welcome email after the registration is successful. The action of sending the welcome email can be separated from the registration process and executed as an asynchronous task. In this way, the application server can avoid being overwhelmed by computationally intensive tasks such as image processing, and users can also get a faster response. Yao supports adding asynchronous tasks to reduce server request pressure.
process | Description | Documentation |
---|---|---|
tasks.XXX.Add | Add a task | - |
tasks.XXX.progress | Query the progress of a task process | - |
tasks.XXX.get | Get information about task processing | - |
Parameters | Meaning | Description |
---|---|---|
name | Task name | |
worker_nums | Specifies the number of processes | |
attempts | Number of failed retries | |
attempt_after | retry interval | |
timeout | timeout period | |
process | The process bound to this task | |
next | Generate task unique id | |
add | Method triggered when a task is added | |
success | Trigger method after successful task processing | |
error | Trigger method after task failure | |
progress | Called during task processing |
/tasks/task.js
{"name": "Test task","worker_nums": 10,"attempts": 3,"attempt_after": 200,"timeout": 2,"size": 1000,"process": "scripts.task.Send","event": {"next": "scripts.task.NextID","add": "scripts.task.OnAdd","success": "scripts.task.OnSuccess","error": "scripts.task.OnError","progress": "scripts.task.OnProgress"}}
scripts/task.js
apis/task.http.json
{"name": "Task","version": "1.0.0","description": "Task","guard": "","group": "task","paths": [{"path": "/task","method": "GET","process": "scripts.test.task","in": [],"out": {"status": 200,"type": "application/json"}}]}
scripts/test.js
function task() {for (i = 1; i < 100; i++) {Process("tasks.test.Add", "Enter task" + i);}}
Run the project yao start
and visit the url 127.0.0.1:5099/api/task/task
to see the print information
tasks.handlername.Add
View [code example](#code example)
Generally speaking, we take the logic that is time-consuming, consumes a lot of resources, or is prone to error, which is very suitable to be separated from the main request process and executed asynchronously. For example, when a new user registers, the system usually sends a welcome email after the registration is successful. The action of sending the welcome email can be separated from the registration process and executed as an asynchronous task. In this way, the application server can avoid being overwhelmed by computationally intensive tasks such as image processing, and users can also get a faster response. Yao supports adding asynchronous tasks to reduce server request pressure.
process | Description | Documentation |
---|---|---|
tasks.XXX.Add | Add a task | - |
tasks.XXX.progress | Query the progress of a task process | - |
tasks.XXX.get | Get information about task processing | - |
Parameters | Meaning | Description |
---|---|---|
name | Task name | |
worker_nums | Specifies the number of processes | |
attempts | Number of failed retries | |
attempt_after | retry interval | |
timeout | timeout period | |
process | The process bound to this task | |
next | Generate task unique id | |
add | Method triggered when a task is added | |
success | Trigger method after successful task processing | |
error | Trigger method after task failure | |
progress | Called during task processing |
/tasks/task.js
{"name": "Test task","worker_nums": 10,"attempts": 3,"attempt_after": 200,"timeout": 2,"size": 1000,"process": "scripts.task.Send","event": {"next": "scripts.task.NextID","add": "scripts.task.OnAdd","success": "scripts.task.OnSuccess","error": "scripts.task.OnError","progress": "scripts.task.OnProgress"}}
scripts/task.js
apis/task.http.json
{"name": "Task","version": "1.0.0","description": "Task","guard": "","group": "task","paths": [{"path": "/task","method": "GET","process": "scripts.test.task","in": [],"out": {"status": 200,"type": "application/json"}}]}
scripts/test.js
function task() {for (i = 1; i < 100; i++) {Process("tasks.test.Add", "Enter task" + i);}}
Run the project yao start
and visit the url 127.0.0.1:5099/api/task/task
to see the print information
tasks.handlername.Add