The asynchronous task mode is primarily applied to long-running and compute-intensive tasks. Volcano Engine Function as a Service (veFaaS) provides the asynchronous task mode in both webserver mode and non-webserver mode. The primary difference between the two modes lies in whether the business side needs to be built on a webserver to process requests. However, the two modes do not differ significantly in terms of business scenarios. This topic describes how to enable asynchronous tasks in these modes.
Prerequisites
Limits
- Switching between synchronous and asynchronous modes is not supported. You can select whether to enable the asynchronous task mode only during function creation. After the function is created, this configuration cannot be modified or updated.
- Switching between webserver and non-webserver modes for asynchronous tasks is not supported.
- If the QPS of your task is high, we recommend that you enable the multiple concurrent requests per instance feature, or increase the maximum number of instances for the current function.
warning
In non-webserver mode, a single instance can run only one task at a time (exclusive mode) by default and cannot process multiple concurrent requests.
- Retries are automatically performed only for system errors. User-initiated retries are not supported.
- The currently supported task execution duration ranges from 10 seconds to 3 hours. Specify an appropriate timeout duration based on your business requirements.
- When a task times out or a user terminates a task, the response of veFaaS varies slightly based on the following scenarios:
- If the "multiple concurrent requests per instance" feature is not enabled, veFaaS will terminate requests and restart the webserver process.
- If the multiple concurrent requests per instance feature is enabled, veFaaS will only cancel the corresponding requests.
warning
Make sure that cancellation signals are handled as expected in the business logic. For example, listen to Context cancellation signals and terminate business logic execution. Otherwise, your business logic may continue to be executed after requests are canceled. As a result, resources will be wasted, or the tasks will not be stopped as expected.
- In non-webserver mode, veFaaS will terminate the corresponding task process.
- For completed tasks, the records of execution status can be retained for up to 30 days, or up to 1,000 records of execution status can be retained. The 1,000-record limit and the 30-day limit are in the OR relationship.
- The currently supported event trigger types include the APIG trigger and the timer trigger.
- The payload of an asynchronous task can be up to 128 KB.
Enable the asynchronous task mode
You can perform the following steps to enable the asynchronous task mode:
- Enable asynchronous tasks when you create a function.
- When you create a function, select Create「Task」. As a result, asynchronous tasks will be automatically enabled.
tip
We recommend that you enable the logging feature. Otherwise, you will not be able to query the execution logs of historical tasks.
- After you enable the asynchronous task mode, you can specify the Retries of async task parameter. The value can be set to 0 to 3. The default value is 0, which indicates that an asynchronous task will not be retried after failure.
- Click Release to release the function online.
Asynchronous task result callback
After receiving an asynchronous invocation request, veFaaS persists the request and immediately returns a response without waiting for the request execution to complete. If you want to notify downstream services of function asynchronous execution failures, you can configure the result callback feature. After you configure the target service, when an asynchronous invocation request is executed, veFaaS automatically calls back the corresponding target service based on the execution result.
Scenario introduction
Asynchronous task result callbacks apply to the following scenarios:
- Save events that would otherwise be discarded for subsequent use.
- When an asynchronous task fails and still fails after being retried based on the specified policy, you may need to be promptly notified of the task failure and handle it based on your business requirements. In this case, you can configure an asynchronous task result callback to push the failure event to the target service. You can also specify the target service as another function. veFaaS will automatically push the context of failure events to this function and execute your custom error handling logic.
- Automatically notify downstream services of execution results
- After an asynchronous task is successfully executed, if you need to automatically notify downstream services of the result so that they can take further actions, you can configure the asynchronous task result callback feature. These services will perform subsequent operations accordingly. For example, if you have configured veFaaS to automatically decompress ZIP files that are uploaded to Volcano Engine Torch Object Storage (TOS) and you want to receive notifications after a ZIP file is decompressed, you can configure the asynchronous task result callback feature for the target service.
Limits
- Only HTTP- or HTTPS-type webhooks can be specified as the target services for asynchronous invocation.
- Only one target service can be configured.
- Do not call back the function itself to avoid circular calls.
- The payload returned by an asynchronous task in webserver mode can be up to 64 KiB in size. If the upper limit is exceeded, the payload will be truncated in the callback message.
Callback message format
The following sample code shows the format of a callback message and the description of each parameter:
tip
- The callback request is in JSON format.
- The requestPayload and responsePayload fields take Base64-encoded strings as values to ensure compatibility with binary or non-UTF-8 payloads.
Configure target services for asynchronous callbacks
- In the left navigation bar, click Function. Find the function for which the asynchronous task mode is enabled and click the function name.
- On the Function configuration tab of the function details page, edit the asynchronous task settings.
- In the Edit configuration information panel, configure target services for asynchronous callbacks.
- Click Save to save the configuration.
What to do next
Submit the task and invoke the function in asynchronous mode. For details on submitting and terminating tasks, refer to Task management.