Connecting

See Connecting to a store for an overview.

Connecting to Acquirium: local, Postgres-backed, or remote.

The library connects to exactly one Acquirium. connect() picks the mode from its arguments:

  1. local (no db, no acquirium_url): a managed Acquirium with DuckDB in data_dir. No credentials.

  2. postgres (db=): a managed Acquirium whose timeseries and experiment ledger live in Postgres (TimescaleDB); the graph and artifact files stay in data_dir, which must persist.

  3. remote (acquirium_url=): a running Acquirium, which owns its own database secret.

FLEXORCH_ACQUIRIUM_URL selects mode 3 for a whole process, so a container or host application points the library at its Acquirium without code. While it’s set, the store isn’t chosen in code: passing data_dir, db or a different acquirium_url raises.

A managed server lives as long as the first process that opened its data_dir. If a long-lived one is running there (flex-orch acquirium start, flexorchestrator.service), modes 1 and 2 attach to it instead and never stop it. FLEXORCH_START_ACQUIRIUM=0 makes them fail rather than start a managed one.

Database credentials never come from script code. In mode 2 they’re taken from FLEXORCH_DB_PASSWORD_FILE, then FLEXORCH_DB_USER / FLEXORCH_DB_PASSWORD, then libpq’s own PGUSER / PGPASSWORD / ~/.pgpass. The password is never written to disk or logged: the server gets the DSN (without the password) as PG_DSN and the password as PGPASSWORD, both only in its process environment, and run metadata only ever sees the redacted user@host:port/db.

acquirium and psycopg are imported only when a connection is made.

flexorchestrator.connection.DEFAULT_DATA_DIR = '.flexorch'

The data directory of a local (mode 1) Acquirium.

Type:

str

flexorchestrator.connection.CONFIG_NAME = 'flexorch-acquirium.toml'

The Acquirium config written into a mode 2 data_dir.

Type:

str

flexorchestrator.connection.BINDING_TABLE = 'flexorch_server'

The table that binds a Postgres database to one managed data_dir.

Type:

str

class flexorchestrator.connection.DBCredentials(user=None, password=None)[source]

Bases: object

A Postgres login, for developers and tests; scripts use the environment.

Parameters:
user

The role; None leaves it to the URL or libpq (PGUSER).

Type:

str | None

password

The password; None leaves it to libpq (PGPASSWORD, ~/.pgpass).

Type:

pydantic.types.SecretStr | None

class flexorchestrator.connection.Connection(mode, target, acquirium, owned=False)[source]

Bases: object

A connected Acquirium and how it was reached.

Parameters:
  • mode (Literal['local', 'postgres', 'remote'])

  • target (str)

  • acquirium (Any)

  • owned (bool)

mode

"local", "postgres" or "remote".

Type:

Literal[‘local’, ‘postgres’, ‘remote’]

target

Where it is, safe to log: the data directory, the redacted user@host:port/db, or the URL.

Type:

str

acquirium

The Acquirium client.

Type:

Any

owned

Whether this process started the server. Informational: close() calls aq.shutdown, which stops only a server this process started.

Type:

bool

backend()[source]

Return the AcquiriumBackend on it.

Return type:

Any

metadata()[source]

Return what run metadata records about the connection.

Return type:

dict[str, str]

close()[source]

Disconnect; a server this process started is stopped, its data kept.

Return type:

None

flexorchestrator.connection.connect(*, data_dir=None, db=None, acquirium_url=None, db_credentials=None, exact_only=True, timeout=600.0, environ=None)[source]

Connect to Acquirium in the mode the arguments select.

Parameters:
  • data_dir (str | Path | None) – The managed server’s directory (graph, artifacts and, in mode 1, DuckDB). Defaults to DEFAULT_DATA_DIR in mode 1; mode 2 needs it.

  • db (str | None) – A Postgres URL or conninfo string without a password; selects mode 2.

  • acquirium_url (str | None) – A running Acquirium, e.g. "http://acquirium:8000"; selects mode 3. FLEXORCH_ACQUIRIUM_URL sets it too.

  • db_credentials (DBCredentials | None) – An explicit login for mode 2, for developers and tests.

  • exact_only (bool) – Start the managed server without text-matching indexes.

  • timeout (float) – Seconds to wait for the server.

  • environ (Mapping[str, str] | None) – Where FLEXORCH_ACQUIRIUM_URL, the FLEXORCH_DB_* credentials and FLEXORCH_START_ACQUIRIUM are read from; os.environ by default. libpq’s own PG* variables and ~/.pgpass are always read from the process.

Raises:
  • ValueError – For arguments that mix modes, a password in db, or a store argument while FLEXORCH_ACQUIRIUM_URL names another.

  • DBAuthError – If Postgres refuses the login.

  • StorageError – If a check before starting fails (see preflight()), or FLEXORCH_START_ACQUIRIUM=0 and no long-lived server is running for data_dir.

Return type:

Connection

Example

>>> connect(acquirium_url="http://acquirium:8000")
flexorchestrator.connection.credentials_from_env(environ=None)[source]

Read the Postgres login from the environment.

FLEXORCH_DB_PASSWORD_FILE (a secret file) wins over FLEXORCH_DB_PASSWORD. FLEXORCH_DB_USER names the role. Whatever is left unset falls to libpq: the URL, PGUSER, PGPASSWORD and ~/.pgpass.

Raises:

StorageError – If the secret file can’t be read.

Parameters:

environ (Mapping[str, str] | None)

Return type:

DBCredentials

flexorchestrator.connection.postgres_dsn(db, user=None)[source]

Return the conninfo DSN for db, with user if given.

The DSN never holds a password; libpq gets that from PGPASSWORD.

Raises:

ValueError – If db holds a password or isn’t a valid DSN (the message doesn’t repeat db).

Parameters:
Return type:

str

flexorchestrator.connection.redact(dsn)[source]

Return user@host:port/db for a DSN, without any secret.

A DSN without a user shows libpq’s: PGUSER from the process environment.

Parameters:

dsn (str)

Return type:

str

flexorchestrator.connection.preflight(dsn, password, data_dir)[source]

Check Postgres before starting a managed server on it.

Checks the login, that TimescaleDB is available, that the role can create tables, and binds the database to data_dir on this host: a second managed server with another data_dir is refused, since the graph and files that match the database live in the first one’s.

Raises:
Parameters:
Return type:

None

flexorchestrator.connection.write_config(data_dir, exact_only)[source]

Write the mode 2 Acquirium config into data_dir and return its path.

It selects TimescaleDB and never sets recreate; it holds no pg_dsn (the DSN reaches the server only through its environment).

Parameters:
Return type:

Path

A long-lived Acquirium

A long-lived local Acquirium: flex-orch acquirium start | status | stop.

A managed Acquirium (modes 1 and 2) lives only as long as the first process that opened its data_dir. One that runs its own drivers or data processing has to outlive every script, so it’s started on its own:

flex-orch --data-dir .flexorch acquirium start      # prints its URL
flex-orch --data-dir .flexorch acquirium status
flex-orch --data-dir .flexorch acquirium stop

start() runs acquirium server detached, on loopback only (Acquirium has no login of its own), and records it in the data_dir (flexorch-service.json). With config=, it serves your own acquirium.toml, drivers and all; otherwise it writes one with the managed server’s settings (DuckDB, exact matching), so the same data_dir holds the same stores either way.

While it runs, Orchestrator(data_dir=...) and connect(data_dir=...) attach to it (find()) instead of starting their own, and never stop it. Set FLEXORCH_START_ACQUIRIUM=0 to make them fail rather than start a short-lived one when it isn’t running.

Only Acquirium’s public interfaces are used: its server command, its config file and GET /health.

flexorchestrator.service.STATE_NAME = 'flexorch-service.json'

The record of a running service, in its data_dir.

Type:

str

flexorchestrator.service.CONFIG_NAME = 'flexorch-service.toml'

The config start() writes when it isn’t given one.

Type:

str

flexorchestrator.service.LOG_NAME = 'flexorch-service.log'

The service’s output, in its data_dir.

Type:

str

class flexorchestrator.service.Service(url, pid, data_dir, config, log)[source]

Bases: object

A running long-lived Acquirium.

Parameters:
url

Where it listens (loopback).

Type:

str

pid

Its process.

Type:

int

data_dir

The folder it serves.

Type:

str

config

The acquirium.toml it was started with.

Type:

str

log

Its output.

Type:

str

flexorchestrator.service.start(data_dir=None, *, config=None, port=None, timeout=120.0)[source]

Start a long-lived Acquirium on data_dir, or the one config names.

Parameters:
  • data_dir (str | Path | None) – The folder to serve; .flexorch by default. Not with config, whose [server] data_dir is the folder.

  • config (str | Path | None) – Your own acquirium.toml (drivers, apps, storage).

  • port (int | None) – The port; a free one by default.

  • timeout (float) – Seconds to wait for it to answer.

Raises:
  • ValueError – For both data_dir and config, or a config with no data_dir.

  • StorageError – If one is already running there, or it didn’t start (the message ends with its log).

Return type:

Service

flexorchestrator.service.find(data_dir=None)[source]

Return the long-lived Acquirium running for data_dir, if one answers.

Parameters:

data_dir (str | Path | None)

Return type:

Service | None

flexorchestrator.service.stop(data_dir=None, timeout=60.0)[source]

Stop the long-lived Acquirium for data_dir; its data is kept.

On Linux and macOS it’s asked to stop (SIGTERM); Windows has no such signal for a detached process, so it’s ended, with any process it started, as Acquirium’s own managed server is.

Returns:

What was stopped, or None if nothing was running.

Raises:

StorageError – If it’s still answering after timeout seconds.

Parameters:
Return type:

Service | None

flexorchestrator.service.status(data_dir=None)[source]

Return {"running": bool, ...the service} for data_dir.

Parameters:

data_dir (str | Path | None)

Return type:

dict[str, Any]