Overview
In applications where users process multiple documents or tasks sequentially, keeping the session active in the background helps maintain a seamless workflow. The Task Queue enables pre-loading the next document while the current one is in use, minimizing wait times. This guide provides a step-by-step overview of the setup, configuration, and event handler sequence needed to implement this functionality.
How It Works
Task Queue is designed to improve user experience and application performance by ensuring seamless task transitions. While a user is working on one document, the application pre-loads the next document in the background. Once the current document is completed, the pre-loaded document becomes active, and the next one starts loading. A keep-alive timer runs in the background to prevent session termination.
The process follows these key steps:
User Works on Current (Foreground) Task: The user interacts with an active task while the system tracks session activity.
Preloading the Next (Background) Task: While the user is engaged, the next document/task is fetched from an external API and stored in a background queue.
Keep-Alive Timer Activation: A timer continuously refreshes session activity and preloaded task data to prevent expiration.
Task Completion Event: When the user completes a task, the preloaded document/task is seamlessly transitioned into the active state.
Updating Task Queue: The queue updates dynamically, fetching and preloading the next document/task in the sequence.
Maintaining Session Activity: The system periodically refreshes session-related data sets, preventing session expiration and ensuring uninterrupted workflow.
Configuration Steps
The session activity management process consists of:
Adding external data sources.
Creating necessary data sets.
Setting up UI components.
Creating timers.
Configuring event handlers.
Managing timers.
Step 1: Add external data sources
In this step, we'll add two external data sources that will serve as the foundation for retrieving tasks in our application. These sources will allow us to create Data sets of the External API type in Step 2.
- Foreground Task Source represents tasks that are actively being processed.
- Background Task Source represents tasks that are queued and waiting to be processed next.
- Open the External data sources page (
) and click Add new data source.
- In our example, we'll add two external data sources with the following configurations:
Foreground Task Source
Setting Value Name Foreground Task Source Request { "url": "https://api-generator.retool.com/H4kq29/data", "method": "GET", "urlParameters": [ { "name": "_page" }, { "name": "_limit" } ] }
Response For Response type Success, click Extract Schema > Send Request > Accept. - Click Add.
- Repeat the same configuration for the second external data source, named Background Task Source.
Step 2: Create Data sets for the App
In this step, we'll define data sets that will be evaluated and updated as users transition between documents.
To add a Data set:
- Under Explorer and Components > Explorer, click App.
- Under Appearance and Configuration > Configuration, locate the Data sets section. Click Add data set.
- Enter the suggested values for each Data set:
1. Active Record Data set
This data set stores the currently active task that the user is working on.
Setting | Value |
---|---|
Name | Active Record |
Type | Variable |
Evaluate automatically | Enabled |
2. Active Record Object Data set
This data set transforms the Active Record data set into a structured JavaScript object for easier data manipulation.
Setting | Value |
---|---|
Name | Active Record Object |
Type | Javascript |
Evaluate automatically | Enabled |
Inputs | Data set: Active Record Required: Enabled |
Output | Output data type: Object Array: Enabled Add fields: Column 1 (Text type), id (Number type) |
Code | return inputs['Active Record'] |
3. Task Queue Object Data set
This data set generates a predefined array of task IDs in JavaScript.
Setting | Value |
---|---|
Name | Task Queue Object |
Type | Javascript |
Evaluate automatically | Enabled |
Output | Output data type: Text Array: Enabled |
Code | return ["1", "2", "3", "4", "5", "6"]; |
4. Task Queue Data set
This data set stores a list of all task IDs that are currently in the queue.
Setting | Value |
---|---|
Name | Task Queue |
Type | Variable |
Expression | {{{Task Queue Object}}} |
Evaluate automatically | Enabled |
5. Foreground Task ID Data set
This data set stores the ID of the current active task, which is the first item in the task queue. It helps track the task that is currently being processed.
Setting | Value |
---|---|
Name | Foreground Task ID |
Type | Variable |
Expression | {{Task Queue.0}} |
Evaluate automatically | Enabled |
6. Background Task ID Data set
This data set holds the ID of the next task in the queue.
Setting | Value |
---|---|
Name | Background Task ID |
Type | Variable |
Expression | {{Task Queue.1}} |
Evaluate automatically | Enabled |
7. Get Foreground Task ID Data set
This data set retrieves detailed information about the currently active task from the Foreground Task Source external source. It takes the task ID as an input and returns the corresponding task data.
Setting | Value |
---|---|
Name | Get Foreground Task ID |
Type | External API |
Source | Foreground Task Source |
Inputs | Input: _page, Location: Foreground Task ID, Requirement: Not null Input: _limit, Requirement: Any value |
8. Get Background Task ID Data Set
This data set retrieves information about the next task in the queue from the Background Task Source external source. It uses the next background (queued) task ID as input to retrieve the relevant data.
Setting | Value |
---|---|
Name | Get Background Task ID |
Type | External API |
Source | Background Task Source |
Inputs | Input: _page, Location: Background Task ID, Requirement: Not null Input: _limit, Requirement: Any value |
9. Task Queue Limit Data Set
This data set defines the default limit (20) for fetching records to ensure consistency in pagination.
Setting | Value |
---|---|
Name | Task Queue Limit |
Type | Variable |
Expression | 20 |
Evaluate automatically | Enabled |
10. Updated Task Queue Data Set
This data set generates a new version of the task queue by removing the first task from the Task Queue data set. It helps update the queue dynamically as tasks are processed.
Setting | Value |
---|---|
Name | Updated Task Queue |
Type | Javascript |
Evaluate automatically | Disabled |
Inputs | Data Set: Task Queue Required: Enabled |
Output | Output data type: Text Array: Disabled |
Code | const arrayOfTasks = inputs['Task Queue']; return arrayOfTasks.slice(1); |
11. Keep-Alive Timer Active Data Set
This data set serves as a switch to indicate whether a timer should be active.
Setting | Value |
---|---|
Name | Keep-Alive Timer Active |
Type | Variable |
Expression | false |
Evaluate automatically | Enabled |
12. Show Completion Timer Data Set
This data set controls the visibility of the job completion timer.
Setting | Value |
---|---|
Name | Show Completion Timer |
Type | Variable |
Expression | false |
Evaluate automatically | Enabled |
Step 3: Add Components
Add the following UI components to the App:
Component | Configuration |
---|---|
Button 1 / Current Task | Name: Current Task Text: Current Task |
Button 2 / Next Task | Name: Next Task Text: Next Task |
Text 1 (Displays active record information) | Name: Current Task Text: {{{Foreground Task ID}}} |
Text 2 (Displays next record information) | Name: Next Task Text: {{{Background Task ID}}} |
Table (Displays list of records) | Read only: enabled Configuration > Data set: Select the Active Record Object data set |
Side panel Component | Visibility: {{{Show Completion Timer}}} |
Added inside the Side panel Component: Container Component Added inside the Container Component: Text Component | Text: Task complete |
Step 4: Configure Timers
In this step, we'll configure two timers, required to keep task data up to date and manage the display of job completion notifications.
- Session Refresh Timer ensures that the queued task data remains updated by periodically refreshing the relevant data set while Keep-Alive Timer Active is active.
- Job Complete Timer manages the visibility of the job completion notification, automatically hiding it after a set duration.
To configure a timer:
- Go to the Timer management page (
) and click the plus icon to create a new Interval timer.
- Provide the following details for the first timer:
Session Refresh Timer
Setting Value Name Session Refresh Timer Delay (seconds) 10 Condition {{{Get Background Task ID}}} && {{{Keep-Alive Timer Active}}} Event Handlers Click the plus icon and add the event handler: - Handler name: Refresh endpoint
- Event type: Delay time elapsed
- Action: Evaluate a data set
- Data set to evaluate: Get Background Task ID
- Click the plus icon to add another Interval timer with the following configuration:
Job Completion Timer
Setting Value Name Job Completion Timer Delay (seconds) 10 Condition {{{Show Completion Timer}}} Event Handlers Click the plus icon and add the event handler: - Handler name: Stop timer
- Event type: Delay time elapsed
- Action: Set a variable
- Variable to set: Show Completion Timer
- Value to set: false
Step 5: Configure Event Handlers
Active Task button's configuration consists of the following event handlers, which should be configured in the same order as listed below:
1. Load Foreground Task Data
This event handler retrieves data for the active foreground task by evaluating the Get Active Task data set. It ensures that the latest task details are fetched when triggered.
Setting | Value |
---|---|
Handler Name | Load Foreground Task Data |
Event Type | Click |
Action | Evaluate a data set |
Data Set | Get Foreground Task ID |
Wait for Response | Enabled |
Continue on Error | Disabled |
2. Load Background Task Data
This event handler retrieves data for the next queued background task by evaluating the Get Background Task ID data set. It ensures that the queued task information is updated when needed.
Setting | Value |
---|---|
Handler Name | Load Background Task Data |
Event Type | Click |
Action | Evaluate a data set |
Data Set | Get Background Task ID |
Wait for Response | Enabled |
Continue on Error | Disabled |
3. Start Session Refresh Timer
This event handler activates the Session Refresh Timer by setting the Keep-Alive Timer Active variable to true. This allows periodic updates of the queued task while processing is active.
Setting | Value |
---|---|
Handler Name | Start Session Refresh Timer |
Event Type | Click |
Action | Set a variable |
Variable to Set | Keep-Alive Timer Active |
Value to Set | true |
4. Set Active Record
This event handler updates the Active Record variable with the most recent data retrieved from the Get Active Task data set. It ensures that the App reflects the latest foreground task details.
Setting | Value |
---|---|
Handler Name | Set Active Record |
Event Type | Click |
Action | Set a variable |
Variable to Set | Active Record |
Value to Set | {{{Get Foreground Task ID}}} |
Next Task button's configuration consists of the following event handlers, which should be configured in the same order as listed below:
1. Stop Notification Timer
This event handler disables the Show Completion Timer by setting its value to false. It ensures that the job completion message is hidden when triggered.
Setting | Value |
---|---|
Handler Name | Stop Notification Timer |
Event Type | Click |
Action | Set a variable |
Variable to Set | Show Completion Timer |
Value to Set | false |
2. Start Notification Timer
This event handler enables the Show Completion Timer by setting its value to true. It ensures that the job completion message is displayed when triggered.
Setting | Value |
---|---|
Handler Name | Start Notification Timer |
Event Type | Click |
Action | Set a variable |
Variable to Set | Show Completion Timer |
Value to Set | true |
3. Set Active Record
This event handler updates the Active Record variable with the most recent data retrieved from the Get Active Task data set. It ensures that the foreground task is set as the active record.
Setting | Value |
---|---|
Handler Name | Set Active Record |
Event Type | Click |
Action | Set a variable |
Variable to Set | Active Record |
Value to Set | {{{Get Background Task ID}}} |
4. Update Active Record
This event handler evaluates the Active Record Object data set to ensure that the active record data is properly processed and updated.
Setting | Value |
---|---|
Handler Name | Set Active Record JS |
Event Type | Click |
Action | Evaluate a data set |
Data Set to Evaluate | Active Record Object |
Wait for Response | Enabled |
Continue on Error | Disabled |
5. Update Task Queue
This event handler processes the Updated Task Queue data set, which removes the first task from the queue and keeps the task queue updated
Setting | Value |
---|---|
Handler Name | Update Task Queue |
Event Type | Click |
Action | Evaluate a data set |
Data Set to Evaluate | Updated Task Queue |
Wait for Response | Enabled |
Continue on Error | Disabled |
6. Update Background Task ID
This event handler updates the Next Task variable with the new first task from Updated Task Queue.
Setting | Value |
---|---|
Handler Name | Update Background Task ID |
Event Type | Click |
Action | Set a variable |
Variable to Set | Background Task ID |
Value to Set | {{{Updated Task Queue.1}}} |
7. Stop Session Refresh Timer
This event handler stops the Session Refresh Timer by setting Keep-Alive Timer Active to false.
Setting | Value |
---|---|
Handler Name | Stop Session Refresh Timer |
Event Type | Click |
Action | Set a variable |
Variable to Set | Keep-Alive Timer Active |
Value to Set | false |
8. Refresh Background Task Data
This event handler evaluates the Get Background Task ID data set to fetch the most recent background task data and keeps the App updated with the latest queued task information.
Setting | Value |
---|---|
Handler Name | Refresh Background Task Data |
Event Type | Click |
Action | Evaluate a data set |
Data Set to Evaluate | Get Background Task ID |
Wait for Response | Enabled |
Continue on Error | Disabled |
9. Start Session Refresh Timer
This event handler restarts the Session Refresh Timer by setting Keep-Alive Timer Active to true.
Setting | Value |
---|---|
Handler Name | Start Session Refresh Timer |
Event Type | Click |
Action | Set a variable |
Variable to Set | Keep-Alive Timer Active |
Value to Set | true |
10. Update Task Queue
This event handler updates the Task Queue variable with the new task queue from Updated Task Queue and keeps the queue accurate after a task is removed.
Setting | Value |
---|---|
Handler Name | Update Task Queue |
Event Type | Click |
Action | Set a variable |
Variable to Set | Update Task Queue |
Value to Set | {{{Updated Task Queue}}} |
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article