You can create a Job task template in Serverless Application Engine (SAE) to automatically run tasks on a schedule or trigger tasks by sending HTTP/HTTPS requests. After a task is complete, the compute resources are quickly released.
Scenarios
SAE Job is suitable for asynchronous offline scenarios that are not sensitive to latency.
Time-driven processing scenarios, such as sending coupons on the hour, updating daily earnings, or refreshing daily tag counts and crowd data.
Batch data processing, such as generating monthly statistical reports or performing batch updates of text message statuses, where real-time performance is not critical.
Asynchronous execution for decoupling, such as refreshing activity statuses, running offline queries asynchronously, and decoupling from internal logic.
CI/CD: After a continuous integration (CI)/continuous delivery (CD) task is complete, compute resources can be released immediately.
For a comparison between SAE Job and self-built open source solutions in different scenarios, see Scenarios.
Preparations
If you use an Alibaba Cloud account, you must activate SAE and EventBridge, and grant the required permissions.
To use a Resource Access Management (RAM) user, you must complete the following preparations:
The Alibaba Cloud account holder must activate EventBridge.
The Alibaba Cloud account holder must activate SAE Job and create the
AliyunServiceRoleForEventBridgeSendToSAEservice-linked role as prompted in the console.The Alibaba Cloud account holder must grant the
AliyunEventBridgeReadOnlyAccesspermission to the RAM user. For more information, see Grant permissions to a RAM user.
Usage flow
Before you can run a task, you must create a task template.
Tasks are triggered by a scheduling rule or by sending an HTTP/HTTPS request. Each trigger generates a new task based on the task template.
SAE creates a new instance to run each task.
Create a task template
On the SAE Task Templates page, select a region and a namespace in the top navigation bar, and then click Create Task Template.
On the Basic Information page of the wizard, configure the parameters and click Next.
Configuration Item
Description
Task Template Name
Enter a custom name for the task template.
Namespace Type
Select a System-created namespace or Select Existing Namespace.
Task Deployment Method
Deploy by Image: Click Set Image. Select Demo Image, My Alibaba Cloud Images (images in an Alibaba Cloud Container Registry (ACR) instance under the same account), Private Images from Other Alibaba Cloud Accounts (images in an ACR instance under a different account), or Custom Image (images not from an ACR instance) to deploy the application. You can also directly Reference Image from Other Task Template. The process of creating, pushing, and pulling images is the same as for deploying an application. For more information, see Deploy an application using an image.
Deploy by Code Package: Click Set Code Package Deployment. Select a Technology Stack Language and Code Package Type. Then, upload the code package and specify its runtime environment. The process of creating and uploading a code package is the same as for deploying an application. For more information, see Deploy an application using a code package.
Shell Script: Click Configure Shell Script and configure the task execution script by referring to the example script on the page.
Instance Type
Default: Provides stable performance. Instances are not preempted or evicted. This type is suitable for scenarios that require high real-time performance and stability.
BestEffort: Performance may fluctuate significantly. Instances can be preempted and evicted. This type is suitable for scenarios with no special requirements for real-time performance or stability.
BestEffort Preferred: The system prioritizes scheduling BestEffort instances. If a BestEffort instance is preempted, the system automatically falls back to a Default instance to prevent task interruption.
Single Instance Specification
Specify the CPU and memory specifications for a single instance when the task runs.
Concurrent Instances
Specify the number of instances that should run concurrently at any given time.
On the Task Settings page of the wizard, configure the parameters and click Next.
Configuration Item
Description
Task Type
Periodic Task
Cron Expression: Defines the schedule for task execution. You can manually enter a cron expression or click Use Generation Tool to generate one interactively.
One-time Task (Triggered by HTTP/HTTPS)
Request Type: Select the request protocol to trigger task execution as needed: HTTP, HTTPS, or HTTP&HTTPS.
Request Method: Select the request methods to trigger task execution as needed. You can select multiple methods: GET, POST, PUT, PATCH, DELETE, HEAD.
Security Configuration: Select a security configuration method from the drop-down list. The options are:
No Configuration Required: No security configuration is needed. All received URL requests are converted into events and pushed to EventBridge.
Secure Domain Name: Enter the secure domain name. Only URL requests from this domain name are converted into events and pushed to EventBridge. All other requests are filtered.
IP CIDR Block: Enter a valid IP address or IP CIDR block. Only URL requests from this IP address or an IP address within this CIDR block are converted into events and pushed to EventBridge. All other requests are filtered.
Concurrency Policy
Expand More Settings and select a concurrency policy.
Allow: Allows tasks to run concurrently. Scheduled tasks continuously create new tasks.
Forbid: Prevents tasks from running concurrently. A new task is not created if the previous one has not finished.
Replace: If the previous task has not finished when it is time to create a new task, the new task replaces the previous one.
Timeout
Specify the maximum duration a task can run. If the task does not finish within the timeout period, the system attempts to stop it. The value must be in the range of [0, 604800]. The unit is seconds. The default value is 3600 seconds.
Retries
Specify the number of times to retry a task after it fails. The value must be in the range of [0, 10]. The default value is 3.
Enable Task Sharding
Turn on the Enable Task Sharding switch, click Edit Task Shard Name, and configure the settings in the Edit Task Shard Name panel. This feature is often used for big data processing, where shards are evenly distributed among multiple clients for execution.
(Optional) On the Advanced Settings page of the wizard, configure the required features. The following example shows how to configure deployment using an image.
Click Create and wait for the task template to be created.
To view the created task template, on the SAE Task Templates page, select a region and a namespace in the top navigation bar. Then, click the target task template to go to its details page.
Execute, start, and stop tasks
Execute a periodic task
A periodic task runs automatically on a schedule based on the cron expression that is specified in the task template.
Execute a one-time task
A one-time task can be triggered by sending an HTTP or HTTPS request. The request protocol and method are determined by the task template configuration.
On the SAE Task Templates page, select a region and a namespace in the top navigation bar. Then, click the target task template to go to its details page.
In the Job Settings section, obtain the Public Request URL or Private Request URL. If you want to send the request from the public network, use the Public Request URL.
Trigger the task by sending a request. You can configure the POST request method to pass specific parameters in JSON format in the request body. For more information about the parameters, see ExecJob.
Example 1: Specify environment variables in the call.
curl -d '{"Envs":[{"name":"aaa","value":"bbb"},{"name":"ccc","value":"ddd"}]}' -H 'Content-Type: application/json' <url> # Note: Replace <url> with the actual value.Example 2: Specify the number of concurrent instances in the call.
curl -d '{"Replicas":"3"}' -H 'Content-Type: application/json' <url> # Note: Replace <url> with the actual value.
Manually trigger a task
On the SAE Task Templates page, select a region and a namespace in the top navigation bar. Then, click the target task template to go to its details page.
On the Task Template Details page, click Manually Execute Task in the upper-right corner.
Stop a task
On the SAE Task Templates page, select a region and a namespace in the top navigation bar. Then, click the target task template to go to its details page.
On the Task Template Details page, click Stop Task in the upper-right corner and follow the on-screen instructions.
Manage task records
On the SAE Task Templates page, select a region and a namespace in the top navigation bar. Then, click the target task template to go to its details page.
In the navigation pane on the left, click Task Records. On the Task Records page, you can view the task execution status and perform operations on the tasks.
To delete a task, click Delete in the Actions column and follow the on-screen instructions.
To view details, click a task ID to go to the Task Record Details page and view the running status of the instance.
View task events
On the SAE Task Templates page, select a region and a namespace in the top navigation bar. Then, click the target task template to go to its details page.
In the navigation pane on the left, click Event Information. In the search box at the top, you can search for events by criteria such as Source Name, Event Reason, Source Type, and Event Level.
Edit and delete a task template
On the SAE Task Templates page, select a region and a namespace in the top navigation bar. Then, click the target task template to go to its details page.
In the upper-right corner, click Edit Task Template or Delete Task Template and follow the on-screen instructions.
After you delete a task template, the system deletes all task instances that are associated with the template and the template's creation information. The SLS resources that were purchased on your behalf are also released, and the feature that collects and sends logs to SLS is disabled.
Roll back to a previous version
If you encounter issues with a new version after you modify a task template, you can roll it back to a previous version.
On the SAE Task Templates page, select a region and a namespace in the top navigation bar. Then, click the target task template to go to its details page.
In the upper-right corner, click Roll Back to Previous Version.
On the Roll Back to Previous Version page, select the version to which you want to roll back and click OK.