# About Workers

These instructions apply to the current Admin Console.

{% hint style="info" %}
Contextual Workers are in early access and may not be available in your account. [Contact Contextual support](mailto:help@contextual.io) to request that Workers be enabled for your tenant.
{% endhint %}

A Worker configuration selects the application and runtime settings for a deployment in your tenant. Configure the Worker in the admin console under **Workers**. For CLI procedures, see [Workers CLI Reference](https://docs.contextual.io/build/contextual-workers/contextual-cli.md).

## Application Code and Configuration

Write and test the application in your own development environment. Keep the application source, Dockerfile, and any Compose file in a Git repository. Build a container image from the application source, then select the built image in the Worker configuration.

Application code and runtime configuration are maintained separately. For example, you can change an environment variable or replica count without rebuilding the application.

## Source

**Source** selects how you describe the application:

| Choice | What You Supply | When to Choose |
| --- | --- | --- |
| **Single image** | One container image. The CLI can build and push the image from a Dockerfile. | A self-contained application, such as the [HTTP quickstart](https://docs.contextual.io/build/contextual-workers/quickstart.md). |
| **Compose** | A [Compose definition](https://docs.contextual.io/build/contextual-workers/connect-to-contextual.md#configure-a-custom-compose-worker) with service images and `x-contextual` settings. The console accepts YAML or JSON; the CLI accepts a Compose file and can build service images. | Several services, or a custom application that needs specific platform features. |
| **Template** | A template name and version published by your organization, and the options it declares. | Several Workers that share one Compose design, such as one Worker per customer or region. |
| **Managed worker** | A template name, version, and the options declared by the selected template. | An application that uses a reusable execution pattern, such as event processing. |

### Managed Workers

A managed Worker runs your application using a platform-provided template. The template selects the trigger, the platform access, and the configuration options. Write your code to the selected template's application contract.

In the console, choose **Managed worker**, then select a template and **Version**. The form shows the selected template version's options, including application image fields. Options and required fields vary by template. Some templates also control scaling through template options.

For a template with an application image option, **Configuration** offers **Single image** or **Compose**. **Compose** replaces the image fields with a Compose editor. Define the application service there, and add any services it needs, such as a cache, to the same Pod. See [Add Services With a Compose File](https://docs.contextual.io/build/contextual-workers/managed-workers/templates-and-options.md#add-services-with-a-compose-file).

The read-only **Managed configuration** preview shows the selected template, version, and options as JSON. Change the template and option fields above the preview to update the managed configuration.

The `managed-event-worker`, `managed-http-worker`, and `managed-scheduled-worker` templates run apps built on a platform runtime adapter for TypeScript, Rust, Go, or Python, and the app's `worker.json` defines its handlers. The template's host holds the platform credentials and gives your handler typed access to records, attachments, AI, messaging, and logging. See [Managed Workers](https://docs.contextual.io/build/contextual-workers/managed-workers.md) to build one.

The `managed-site-worker` template runs a plain HTTP application beside a public authentication gateway. It uses the Site application's Dockerfile and server rather than handler adapter packaging. See [Site Workers](https://docs.contextual.io/build/sites/site-workers.md).

### Templates

A template is a Compose file that your organization publishes under a name and version. It declares options under `x-contextual.options`, and its services use those options. A Worker selects a template version and supplies option values, the same way it uses a managed worker. Use a template when several Workers share one design but differ in images or settings.

Publish templates with `ctxl workers templates push`. See [Use a Template](https://docs.contextual.io/build/contextual-workers/contextual-cli.md#use-a-template). A template version cannot be pushed twice, and a template or version that a Worker uses cannot be deleted.

In the console, choose **Template**, then select a template and **Version**. As with a managed worker, **Configuration → Compose** adds services to the template. The read-only **Template configuration** preview shows the selection as JSON.

### Select Native Object Records

Managed Site Workers use a `native_object` option to select the Site record whose authentication profile signs visitors in. For example, a Worker serving a customer portal can reference the `customer-portal` Site record and optionally pin a particular revision.

Other managed and organization templates can use the same mechanism to select records their applications need, such as a document-processing configuration profile. When saving a Worker, the platform checks that the selected record—and any pinned revision—exists in the Worker's tenant. The application receives the record reference and reads its contents.

In the console, the field for a `native_object` option shows a list of that type's records, then a revision list that defaults to **Latest**. **Latest** saves only the record's ID, so the Worker doesn't restart when the record changes. Choosing a revision saves `<id>#<version>`, such as `customer-portal#3`. See [Options](https://docs.contextual.io/build/contextual-workers/x-contextual-specification.md#options).

## Worker Settings

The Worker form shows settings that apply to the selected configuration. Scheduled Workers and templates with scaling options hide settings that do not apply.

| Setting | Meaning |
| --- | --- |
| **ID** | The identifier used in commands and the default HTTP hostname. Use lowercase letters, numbers, and dashes. The Worker ID cannot be changed after creation. |
| **Name** and **Description** | Labels that help you and your team identify the Worker. |
| **Image** | The container build to run. Select a pushed image or enter its reference on your organization's image host, such as `images.<org>.my.contextual.io/<name>:<tag>`. Saving the Worker pins the image to a digest. The digest identifies the exact container build. See [Image References](#image-references). |
| **Size** | The CPU and memory available to each replica. Select a preset or [configure a custom Worker size](https://docs.contextual.io/build/contextual-workers/configure-worker-size.md). See [Choose a Worker Size](https://docs.contextual.io/build/contextual-workers/choose-worker-size.md). |
| **Min replicas** and **Max replicas** | The lower and upper bounds on running copies for CPU-based scaling. Set the minimum and maximum replica counts to the same value for a fixed count. |
| **Target CPU %** | The CPU utilization target for CPU-based scaling within the replica range. |
| **Environment variables** | Named values supplied at runtime, such as the quickstart's `GREETING`. Names are converted to upper case, so two names that differ only in case conflict. Values are stored encrypted. Every service in the Worker receives them, except variables used by Compose secrets. Keep configuration that varies between tenants out of the image. Put secret values here, not in the image. To deliver a value as a file instead, see [Secrets and Configuration Files](https://docs.contextual.io/build/contextual-workers/connect-to-contextual.md#secrets-and-configuration-files). |
| **Schedule** | An optional cron expression in UTC. A scheduled Worker runs once per tick and must finish and exit. Scheduled Workers have no public HTTP endpoint. |
| **Custom domain** | An optional hostname for an HTTP Worker. The Custom domain field is hidden for scheduled Workers and managed templates without a public service. See [Custom Domains](https://docs.contextual.io/build/contextual-workers/custom-domains.md) for setup and DNS verification. |

CPU-based replica limits range from 1 to 100. The maximum replica count must be at least the minimum replica count. Scheduled Workers keep the minimum and maximum replica counts at 1. Backlog scaling replaces CPU-based scaling for event processing and can scale the Worker to zero while its backlog is empty. Configure it in an event template's options or a Compose service's `x-contextual.scaling`. See [Events and Schedules](https://docs.contextual.io/build/contextual-workers/connect-to-contextual.md#events-and-schedules).

## Image References

A Worker runs images from two image hosts:

* **Your organization's image host**, such as `images.<org>.my.contextual.io`. Images you push with `ctxl workers images push`, or that `--build` pushes, are stored here as `images.<org>.my.contextual.io/<name>:<tag>`.
* **The platform's image host**, `images.contextual.io`. Platform images are under `images.contextual.io/worker/platform/`. The runtime adapter images under `images.contextual.io/worker/base/` are for your image's `FROM` line only. A Worker can't run one directly.

Name images by these hosts in the console, in CLI flags, in Compose files, and in templates. For example, a Compose service can run an image that you pushed earlier:

```yaml
services:
  web:
    image: images.<org>.my.contextual.io/orders-api:1.4.0
```

Saving the Worker pins each image to its digest. The saved Worker record stores the pinned image as a registry path. The CLI and the console show it under your image host again, for example `images.<org>.my.contextual.io/orders-api:1.4.0@sha256:…`. Images from other registries, such as Docker Hub, are refused.

## Runtime Environment

Each replica runs as an isolated pod in its own virtual machine. The platform applies the same restrictions to every container, whatever the image declares:

* **User:** Processes run as user and group ID 10001, not the image's `USER`. All Linux capabilities are dropped.
* **Read-only filesystem:** The root filesystem is read-only. Only `/tmp` and Compose named volumes are writable. Point caches, home-directory files, and temporary output at `/tmp`.
* **Storage:** Each replica has 1 GiB of scratch space, divided evenly between each service's `/tmp` and each named volume. The replica's total storage, including scratch space and container logs, is limited to 2 GiB. Every new replica starts with empty scratch space, so a deployment or restart clears it.
* **Network:** Services in one Worker share a network. A service reaches another at its service name and port.
* **Shutdown:** The platform sends `SIGTERM` and allows at least 30 seconds for the process to exit.

Keep durable state in Native Objects or an external data store.

### HTTP Workers

A single-image Worker's container is the public service, unless the Worker is scheduled. A [scheduled Worker](#scheduled-workers) never has a public service. In Compose, the public service is the one service that sets `x-contextual.public` to `true` or a port; publishing `ports` alone doesn't make a service public. The public service listens on `PORT`. The platform sets `PORT` from `x-contextual.public` or the first `ports` entry in Compose, otherwise from the image's `EXPOSE`, otherwise to 8080. An image that exposes more than one port must choose one with `x-contextual.public`. Use a port from 1024 to 65535.

{% hint style="warning" %}
When moving an existing Compose Worker to explicit public-service selection, set `x-contextual.public: true` (or the intended port) on its HTTP service before redeploying. An unmarked service that was exposed implicitly can lose its HTTP endpoint on its next deployment.
{% endhint %}

A public service needs a readiness check: a `HEALTHCHECK` in the image, or a `healthcheck` or `x-contextual.http_healthcheck` in Compose. The platform sends traffic to a replica only while its check passes.

The Worker URL is public. For generic HTTP Workers, the platform does not authenticate requests, so the application must authenticate its callers. A [Site Worker](https://docs.contextual.io/build/sites/site-workers.md#authentication-and-browser-origins) uses its gateway to validate sessions when the Site has an Auth Profile; application code still controls access to routes and operations.

Request bodies are limited to 1 MiB, and requests time out after 30 seconds. To change these limits, set the Worker environment variables `BODY_SIZE_LIMIT` (for example, `10mb`) and `HTTP_TIMEOUT_MS` (in milliseconds).

### Scheduled Workers

A scheduled Worker starts one run per tick of its UTC cron schedule. Each run must finish and exit.

* A run is stopped after 30 minutes.
* A failed run is not retried. The next tick starts a new run.
* A tick is skipped while the previous run is still in progress.

The schedule is available to the application as `CONTEXTUAL_SCHEDULE`.

## Deploy and Iterate

1. Edit and test the application locally in the source repository.
2. Build and push an image with the CLI. The `workers create` command with `--build` combines image building, image pushing, and Worker creation.
3. Create or update the Worker configuration to select the image and runtime settings. Saving starts deployment.
4. Open **Deployment** to inspect instances and readiness. For an HTTP Worker, copy the **Public Worker URL** and test the application. Read the containers' output with `ctxl workers logs`. See [Read Logs](https://docs.contextual.io/build/contextual-workers/contextual-cli.md#read-logs).
5. For application code changes, build a new image and update the Worker. For configuration changes, update the Worker settings.

Pushing an image alone does not update a running Worker. Update the Worker to select the pushed image. A saved configuration version, an image digest, and a managed template version identify different parts of the deployment.

For repository access and automated deployments, see [Deploy from GitHub](https://docs.contextual.io/build/contextual-workers/contextual-cli.md#deploy-from-github).

The Worker's status and **Deployment** view describe runtime state. A **Running** label alone does not confirm that the application is ready. Use the application checks in the [quickstart](https://docs.contextual.io/build/contextual-workers/quickstart.md#5-verify-the-deployment).

## Services and Releases

Workers deploy independently of Services. When promoting a solution with Service-managed components, install the required Service releases in the target tenant before deploying the Worker. See [Coordinate Worker Deployments and Service Releases](https://docs.contextual.io/build/contextual-workers/releases.md) for dependency ownership, deployment order, verification, and recovery.
