# Remote Workers

The Remote Workers Configuration is a parallelism technique in Continuous Integration (CI) testing environment, where testing operations are not performed on the main **orchestrator**, but are delegated across various worker nodes. These worker nodes act as remote workers, enabling the simultaneous execution of multiple tests, thereby reducing feedback time.

In this configuration, the **orchestrator** (main node) is responsible for distributing tests among the **worker nodes**. Each worker node independently executes the assigned tests, ensuring quicker CI test execution.

## Install

Follow the instructions in the [Quick Start ⏱️](/welcome-to-redefine/quick-start.md)guide to install Redefine.

## Configure

### Basic Configurations

Configure the relevant configurations on the orchestrator following the instructions in the quick start guide. Note that configuration changes on worker nodes are not required for this process.

### Configure a shared session ID

To use Redefine with the remote worker architecture, it is important to configure a shared session ID between the orchestrator and the worker nodes. This allows Redefine to connect the prediction and the execution of the tests.

Follow these steps to configure the shared Session ID:

1. It is crucial to generate the session ID once and provide it to both the orchestrator and worker nodes, to avoid misalignment between the orchestrator and the workers. &#x20;
2. You can use the CLI command `redefine get session_id` to generate the session ID, which represents the current session.
3. Export the environment variable REDEFINE\_SESSION\_ID with the same value on both the orchestrator and each worker node.<br>

```bash
export REDEFINE_SESSION_ID=$(redefine get session_id)
```

## Verify

Make sure that the verify command ends with success both for the orchestrator and the worker nodes, that will help ensure that the CI environment is valid for installation.&#x20;

{% tabs %}
{% tab title="Orchestrator" %}

```bash
redefine verify --<testing-framework>
```

{% endtab %}

{% tab title="Worker" %}

```bash
redefine verify --<testing-framework> --worker
```

{% endtab %}
{% endtabs %}

Follow the [Quick Start ⏱️](/welcome-to-redefine/quick-start.md#verify) guide for instructions and troubleshooting.

## Run

### Orchestrator

Execute the redefine predict command to receive an ordered list of tests, which would be saved to a file in the desired path without actually running the tests. Once you have the list, you should distribute the tests in the existing logic, ensuring you follow the specified order.

{% tabs %}
{% tab title="Pytest" %}

```bash
redefine predict --pytest --<selection_mode> --output-path="/file/path" --command="pytest tests"
export OPTIMAL_TESTS_LIST=$(cat /file/path)

# Use OPTIMAL_TESTS_LIST in your existing test distribution logic
# All specs will be ordered by priority in <output_path>,
# separated with a new-line ('\n').
```

{% endtab %}

{% tab title="Cypress" %}

```bash
redefine predict --cypress --<selection_mode> --output-path="/file/path" --command="cypress run"
export OPTIMAL_TESTS_LIST=$(cat /file/path)

# Use OPTIMAL_TESTS_LIST in your existing test distribution logic
# All specs will be ordered by priority in <output_path>,
# separated with a new-line ('\n').
```

{% endtab %}

{% tab title="Mocha" %}

```bash
redefine predict --mocha --<selection_mode> --output-path="/file/path" --command="npx mocha"
export OPTIMAL_TESTS_LIST=$(cat /file/path)

# Use OPTIMAL_TESTS_LIST in your existing test distribution logic
# All specs will be ordered by priority in <output_path>,
# separated with a new-line ('\n').
```

{% endtab %}
{% endtabs %}

### Workers

Run the `redefine install` command to run redefine using `--worker` as the selection mode.\
\
Following this, you can execute the specified tests that have been assigned to the worker.

{% tabs %}
{% tab title="Pytest" %}

```bash
redefine install --pytest --worker

# Run the tests assigned to the current worker 
# based on the existing tests distribution logic
```

{% endtab %}

{% tab title="Cypress" %}

```bash
redefine install --cypress --worker

# Run the tests assigned to the current worker 
# based on the existing tests distribution logic
```

{% endtab %}

{% tab title="Mocha" %}

```bash
redefine install --mocha --worker

# Run the tests assigned to the current worker 
# based on the existing tests distribution logic
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}

#### **Maintain the test execution order**

After the orchestrator completes, an optimized test execution order is provided. This test execution order is a crucial part of our orchestrator's function. The orchestrator creates an optimized list that maps out how the tests should be run for the most efficient workflow. Change this order, and you could see a significant drop in efficiency and potential miss of important tests.\
\
Keep in mind, modifying the test order does more than just reduce efficiency — it could also compromise the accuracy of our test predictions. Therefore, it's essential to adhere to the initial order.
{% endhint %}

### Session check command

In Redefine, tests will only be executed until the [Broken mention](broken://pages/ukeQo52y77NM3cuhDJos) is reached. Any remaining tests will be automatically skipped. However, it's important to be aware that the setup and teardown processes may impose a significant overhead.

To mitigate this overhead, it is highly recommended to periodically check if the test execution exceeds the time limit. If the time limit is exceeded, no more tests should be executed.

To determine if the time limit is exceeded, use the following command:

{% tabs %}
{% tab title="Pytest" %}

```bash
# Make sure REDEFINE_SESSION_ID is available
if redefine get session_check; then
    redefine install --pytest --worker
    # Run the tests assigned to the current worker 
fi
```

{% endtab %}

{% tab title="Cypress" %}

```bash
# Make sure REDEFINE_SESSION_ID is available
if redefine get session_check; then
    redefine install --cypress --worker
    # Run the tests assigned to the current worker 
fi
```

{% endtab %}

{% tab title="Mocha" %}

```bash
# Make sure REDEFINE_SESSION_ID is available
if redefine get session_check; then
    redefine install --mocha --worker
    # Run the tests assigned to the current worker 
fi
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
This is especially important when iterating over a large list of tests, as the overhead may increase dramatically.
{% endhint %}

## Example

The example below shows a complete run with `pytest` running in [Optimize](/configuration/selection-modes/optimize.md) mode.

{% tabs %}
{% tab title="Orchestrator" %}

```bash
export REDEFINE_SESSION_ID=<new_session_id>
redefine config set time_limit=300
redefine predict --pytest --optimize --output-path="/file/path" --command="pytest tests"
export OPTIMAL_TESTS_LIST=$(cat /file/path)

# Use OPTIMAL_TESTS_LIST in your existing tests distribution logic
```

{% endtab %}

{% tab title="Worker" %}

```bash
export REDEFINE_SESSION_ID=<new_session_id>
redefine install --pytest --worker

# Run the tests assigned to the current worker 
# based on the existing tests distribution logic
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.redefine.dev/configuration/parallel-test-execution/remote-workers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
