Task

View source code

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.

handler list

processDescriptionDocumentation
tasks.XXX.AddAdd a task-
tasks.XXX.progressQuery the progress of a task process-
tasks.XXX.getGet information about task processing-

Naming conventions

ParametersMeaningDescription
nameTask name
worker_numsSpecifies the number of processes
attemptsNumber of failed retries
attempt_afterretry interval
timeouttimeout period
processThe process bound to this task
nextGenerate task unique id
addMethod triggered when a task is added
successTrigger method after successful task processing
errorTrigger method after task failure
progressCalled during task processing

code example

Step 1: Create a new file directory /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"
}
}

Step 2: Create a new scripts/task.js

View source code

Step 3: Create a new route 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"
}
}
]
}

Step 4: Create a new test function 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

Invoke the Task handler tasks.handlername.Add

Task

View source code

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.

handler list

processDescriptionDocumentation
tasks.XXX.AddAdd a task-
tasks.XXX.progressQuery the progress of a task process-
tasks.XXX.getGet information about task processing-

Naming conventions

ParametersMeaningDescription
nameTask name
worker_numsSpecifies the number of processes
attemptsNumber of failed retries
attempt_afterretry interval
timeouttimeout period
processThe process bound to this task
nextGenerate task unique id
addMethod triggered when a task is added
successTrigger method after successful task processing
errorTrigger method after task failure
progressCalled during task processing

code example

Step 1: Create a new file directory /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"
}
}

Step 2: Create a new scripts/task.js

View source code

Step 3: Create a new route 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"
}
}
]
}

Step 4: Create a new test function 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

Invoke the Task handler tasks.handlername.Add