Use enable
In this guide, we’ll explore the enable
function, which allows you to control whether a container should start based on specific conditions. By configuring enable
, you can ensure that certain containers only initialize when needed, providing greater flexibility and control over application resources and dependencies.
We’ll explore:
- Container Statuses: An overview of possible statuses and what each one represents.
- How
enable
interacts with container statuses. - Practical examples showing how to set up
enable
with different conditions.
Using enable
effectively allows you to create more dynamic container workflows, optimizing the execution and resource usage of your application.
Container Statuses
Containers can have the following statuses based on their execution state and the state of their dependencies:
- idle: The container is inactive and has not yet started.
- pending: The container is ready to start and is waiting for its turn. This status indicates that all dependencies (both strict and optional) have reached a final state of either
done
,fail
, oroff
. - done: The container has started successfully and is fully operational.
- fail: The container attempted to start but encountered an error, either directly or due to a failed strict dependency.
- off: The container is disabled and will not attempt to start.
Understanding these statuses helps in diagnosing and managing the execution flow of containers, especially in complex configurations with multiple dependencies.
How enable
Interacts with Container Statuses
The enable
function provides conditional control over whether a container should start. Here’s how enable
interacts with container statuses, based on dependency resolutions and the value returned by enable
.
-
When
enable
is evaluated: Theenable
function is only called once all strict dependencies are in thedone
status, and optional dependencies (if any) are in one of the final statuses (done
,fail
, oroff
). If a strict dependency isoff
, the container itself will also be set tooff
. If a strict dependency has afail
status, the container will inherit thisfail
status as well. -
Return value of
enable
:true
orPromise<true>
: Ifenable
returnstrue
(or a promise that resolves to true), the container will proceed to call itsstart
function. This means the container will transition topending
and then todone
upon successful execution.false
orPromise<false>
: Ifenable
returnsfalse
(or a promise that resolves to false), the container status will be set tooff
, andstart
will not be called.
-
Default behavior:
enable
is optional. Ifenable
is not defined for a container, it defaults to returningtrue
, allowing the container to proceed tostart
as soon as its dependencies are resolved.
Using enable
provides fine-grained control over the execution of containers based on specific conditions. This mechanism helps manage complex workflows, ensuring containers start only when all necessary dependencies are available and conditions are met.
Examples
Example 1: Basic enable Condition
In this example, we’ll create a container that only starts if a certain condition is met. The enable
function will check this condition and return true
or false
.
Expected Result
If featureToggle
is done
and isEnabled
is true
, then newFeature
will start
, printing:
If isEnabled
is false
, then newFeature will remain off
and not print anything about starting.
Example 2: enable
with Dependencies and Async Conditions
This example demonstrates using an asynchronous enable
to control execution based on a network request. The container will only start if the user has permissions loaded from an API.
Expected Result
If the user has permission (feature_access)
, restrictedFeature will start
If the user lacks the permission, restrictedFeature
will be set to off
and won’t start:
Example 3: Optional enable
Condition
If no enable
function is provided, it defaults to returning true
. This allows a container to start
as soon as all dependencies are resolved.
Expected Result
In this case, since enable
is not defined, the container defaults to true
, allowing dashboard to start as soon as settings
is done
:
Example 4: Using enable with optionalDependsOn
In this example, we’ll create a container that has an optional dependency and uses enable
to check if the optional dependency is available before starting. The container will only start if the optional dependency provides the necessary data.
Expected Result
If optionalFeature
is available and isAvailable
is true
, then mainComponent
will start with access to the optional feature:
If optionalFeature
is unavailable or isAvailable
is false
, then mainComponent will be set to off and won’t start:
Summary
In this guide, we explored how to use the enable
function to control the conditional execution of containers. By configuring enable
, you can ensure that containers only start when necessary conditions are met, allowing for more efficient resource usage and flexible workflows. Key takeaways include:
- Conditional Execution: The
enable
function controls whether a container should start, based on custom conditions. - Interaction with Dependencies:
enable
is only evaluated when all strict dependencies aredone
, and optional dependencies (if any) have resolved todone
,fail
, oroff
. If a strict dependency isoff
orfail
, the container inherits that status. - Return Values:
enable
should return aboolean
or aPromise<boolean>
. Returningtrue
allows the container to proceed tostart
, whilefalse
sets it tooff
. - Default Behavior:
enable
is optional; if not specified, it defaults totrue
, allowing the container tostart
when dependencies are met.
With enable
, you can create dynamic and responsive container setups, optimizing the execution flow based on application requirements and dependency availability.