Distributed execution node for the Trading Automation platform. Each worker boots, registers with the main control plane, exposes a gRPC task surface, and drives brokerage automation through the AutoRSA CLI.
- Entry point (
app/main.py) – loads configuration, wires logging, and kicks off the worker bootstrap sequence. - Bootstrap Orchestrator (
app/bootstrap.py) – initializes the gRPC client/server, task facade, and AutoRSA services, then blocks on task consumption. - gRPC Task Server (
app/data/strategy/grpc/DefaultServicer.py) – receives activation/deactivation/transaction RPCs from Trading-Automation-Main and normalises payloads. - Task Dispatch Layer (
app/taskFacade/TaskFacade.py) – maps protobuf tasks to use cases, handles retries, and raises structured errors back over gRPC. - AutoRSA Integration (
app/services/autoRsaService/AutoRSAService.py) – wraps the AutoRSA CLI to perform brokerage logins, account activation, and trade execution. - Structured Logging (
app/logger.py) – streams JSON logs to stdout and rotates error output intoerror.logfor local debugging.
┌─────────────────────────────┐ Ping (startup) ┌──────────────────────────┐
│ Trading-Automation-Worker │────────────────────────────▶│ Trading-Automation-Main │
├─────────────────────────────┤ ├──────────────────────────┤
│ GrpcTaskFetcher server │◀── Activation / Task RPC ───│ Worker stub registry │
│ DefaultServicer handlers │ │ Redis-backed routing │
│ TaskFacade + UseCases │─── Task responses ─────────▶│ REST / gRPC orchestrator │
│ AutoRSA runner │ │ │
└─────────────────────────────┘ └──────────────────────────┘
▲
│ AutoRSA CLI (commands)
▼
Brokerage / Exchange APIs
Worker Runtime Walkthrough (ASCII)
┌────────────────────────┐
│ Trading Automation Main│
│────────────────────────│
│ • FastAPI REST router │
│ • Redis group registry │
│ • Worker stub manager │
└──────────┬─────────────┘
│ secure gRPC
▼
┌────────────────────────────────────────────────────────────┐
│ Trading Automation Worker │
│────────────────────────────────────────────────────────────│
│ Bootstrap (app/bootstrap.py) │
│ • loads configs/app.json │
│ • spins up GrpcTaskFetcher server │
│ • issues startup ping to MainTradingService │
│ │
│ DefaultServicer(app/data/strategy/grpc/DefaultServicer.py) │
│ • exposes Activate/Deactivate/Transaction RPCs │
│ • emits protobuf Payload → Task models │
│ │
│ TaskFacade (app/taskFacade/TaskFacade.py) │
│ • selects Activation/Deactivation/Transaction use case │
│ • coordinates AutoRSAService execution │
│ │
│ AutoRSAService (app/services/autoRsaService/...) │
│ • shells out to lib/auto-rsa/ scripts │
│ • parses CLI responses → gRPC replies │
└────────────────────────────────────────────────────────────┘
- Registers itself with the main control plane via periodic ping/keepalive and replays queued tasks on reconnect.
- Exposes a gRPC task API for account activation, deactivation, and transaction execution.
- Transforms strongly typed protobuf requests into AutoRSA commands with credential validation and error surfacing.
- Ships with Dockerfiles for standalone worker containers or Compose-based multi-service stacks.
- Provides configurable logging and retry hooks suitable for headless deployment.
- Python 3.9+
- uv (recommended for dependency management)
- AutoRSA CLI dependencies (run
bash scripts/build_auto_rsa.sh) - Access to a running Trading-Automation-Main instance over gRPC
- Docker (optional) for containerized workloads
-
Clone
git clone <repository-url> cd Trading-Automation-Worker
-
Install uv (if needed)
curl -Ls https://astral.sh/uv/install.sh | sh -
Install dependencies
uv sync
-
Provision AutoRSA
bash scripts/build_auto_rsa.sh
The script clones/updates
lib/auto-rsa, builds its virtual environment, installs Playwright, and ensures the runner scripts are executable. Populatelib/auto-rsa/credsand.envaccording to brokerage requirements once it finishes. -
Generate protobuf stubs
uv run bash scripts/proto_build.sh
-
Configure worker endpoints – update
configs/app.jsonwith the main server host/port and the worker's listening address. -
Launch the worker
uv run python app/main.py
The worker pings the main service, binds its gRPC server (default
0.0.0.0:50051), and begins consuming tasks.
-
Build image
docker build -t ta-worker . -
Run container
docker run -d --name trading-worker \ -p 50051:50051 \ -v $(pwd)/configs:/app/configs \ -v $(pwd)/lib/auto-rsa:/app/lib/auto-rsa \ trading-worker
Ensure the container can reach the main service gRPC endpoint and that AutoRSA credentials are mounted securely.
-
Compose stack – when using the main project’s
docker-compose.yml, tag the image asworker:latestso the orchestrator can schedule it.
configs/app.json– declaresMAIN_SERVER_HOST,MAIN_SERVER_PORT, and the worker’sTASK_FETCHER_*bind settings.lib/auto-rsa/.env– broker-specific secrets such as usernames, passwords, and MFA seeds.lib/auto-rsa/creds/– encrypted credential bundles consumed by AutoRSA.- Environment variables can override defaults by extending the bootstrap logic in
app/bootstrap.py.
- Worker boots, loads config, and sends a
PingtoMainTradingServicewith its callback host/port. - Trading-Automation-Main dials the worker and establishes a bidirectional gRPC channel.
- Incoming protobuf tasks hit
DefaultServicer, which converts them into internal task models. TaskFacaderoutes each task to the matching use case and invokesAutoRSAServiceto perform the action.- Results (success/error payloads) are serialized back over gRPC to the main service for client responses.
- Protobuf generation:
uv run bash scripts/proto_build.sh - Unit tests:
uv run pytest(ensure dependencies and any mocks for AutoRSA are available) - Linting/formatting: integrate
ruff,flake8, orblackvia uv-managed environments as desired.
app/
├── main.py # Worker entry point and runtime loop
├── bootstrap.py # Config loading, ping client, and gRPC bootstrap
├── logger.py # Structured logging configuration
├── data/
│ ├── strategy/grpc/ # GrpcTaskFetcher, DefaultServicer, generated stubs
│ └── model/task/ # Internal task DTOs and converters
├── taskFacade/TaskFacade.py # Central task dispatcher
├── useCase/ # Activation/Deactivation/Transaction flows
├── services/autoRsaService/ # AutoRSA command execution wrapper
├── router/Router.py # Factory wiring for task handlers
configs/app.json # Worker runtime configuration
scripts/proto_build.sh # Regenerate protobuf definitions
lib/auto-rsa/ # Embedded AutoRSA CLI toolkit
Dockerfile # Container image definition
app/logger.pyemits JSON logs to stdout and mirrors exceptions intoerror.log—ship the stream to your log aggregator in production.- gRPC keepalive is managed in the bootstrap; tune intervals/timeouts before deployment to noisy networks.
- AutoRSA commands may surface sensitive output—mask logs and secure the
lib/auto-rsadirectory.
- Ping failures: verify
MAIN_SERVER_HOST/PORTand that the worker can reach the orchestrator over the network. - AutoRSA errors: ensure Python dependencies are installed and credentials in
lib/auto-rsaare valid. - gRPC port conflicts: adjust
TASK_FETCHER_PORTinconfigs/app.jsonor the Docker port mapping. - Permission issues: AutoRSA scripts require execution permission (
chmod +x) when mounted into containers.
- Trading-Automation-Main – FastAPI control plane and worker orchestrator.