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:
local (no
db, noacquirium_url): a managed Acquirium with DuckDB indata_dir. No credentials.postgres (
db=): a managed Acquirium whose timeseries and experiment ledger live in Postgres (TimescaleDB); the graph and artifact files stay indata_dir, which must persist.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:
- flexorchestrator.connection.CONFIG_NAME = 'flexorch-acquirium.toml'¶
The Acquirium config written into a mode 2
data_dir.- Type:
- flexorchestrator.connection.BINDING_TABLE = 'flexorch_server'¶
The table that binds a Postgres database to one managed
data_dir.- Type:
- class flexorchestrator.connection.DBCredentials(user=None, password=None)[source]¶
Bases:
objectA Postgres login, for developers and tests; scripts use the environment.
- password¶
The password;
Noneleaves it to libpq (PGPASSWORD,~/.pgpass).- Type:
pydantic.types.SecretStr | None
- class flexorchestrator.connection.Connection(mode, target, acquirium, owned=False)[source]¶
Bases:
objectA connected Acquirium and how it was reached.
- 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:
- acquirium¶
The Acquirium client.
- Type:
Any
- owned¶
Whether this process started the server. Informational:
close()callsaq.shutdown, which stops only a server this process started.- Type:
- backend()[source]¶
Return the
AcquiriumBackendon it.- Return type:
- 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_DIRin 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_URLsets 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, theFLEXORCH_DB_*credentials andFLEXORCH_START_ACQUIRIUMare read from;os.environby default. libpq’s ownPG*variables and~/.pgpassare always read from the process.
- Raises:
ValueError – For arguments that mix modes, a password in
db, or a store argument whileFLEXORCH_ACQUIRIUM_URLnames another.DBAuthError – If Postgres refuses the login.
StorageError – If a check before starting fails (see
preflight()), orFLEXORCH_START_ACQUIRIUM=0and no long-lived server is running fordata_dir.
- Return type:
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 overFLEXORCH_DB_PASSWORD.FLEXORCH_DB_USERnames the role. Whatever is left unset falls to libpq: the URL,PGUSER,PGPASSWORDand~/.pgpass.- Raises:
StorageError – If the secret file can’t be read.
- Parameters:
- Return type:
- flexorchestrator.connection.postgres_dsn(db, user=None)[source]¶
Return the conninfo DSN for
db, withuserif given.The DSN never holds a password; libpq gets that from
PGPASSWORD.- Raises:
ValueError – If
dbholds a password or isn’t a valid DSN (the message doesn’t repeatdb).- Parameters:
- Return type:
- flexorchestrator.connection.redact(dsn)[source]¶
Return
user@host:port/dbfor a DSN, without any secret.A DSN without a user shows libpq’s:
PGUSERfrom the process environment.
- 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_diron this host: a second managed server with anotherdata_diris refused, since the graph and files that match the database live in the first one’s.- Raises:
DBAuthError – If the login is refused.
StorageError – If Postgres can’t be reached or a check fails.
- Parameters:
- Return type:
None
- flexorchestrator.connection.write_config(data_dir, exact_only)[source]¶
Write the mode 2 Acquirium config into
data_dirand return its path.It selects TimescaleDB and never sets
recreate; it holds nopg_dsn(the DSN reaches the server only through its environment).
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:
- flexorchestrator.service.CONFIG_NAME = 'flexorch-service.toml'¶
The config
start()writes when it isn’t given one.- Type:
- flexorchestrator.service.LOG_NAME = 'flexorch-service.log'¶
The service’s output, in its
data_dir.- Type:
- class flexorchestrator.service.Service(url, pid, data_dir, config, log)[source]¶
Bases:
objectA running long-lived Acquirium.
- flexorchestrator.service.start(data_dir=None, *, config=None, port=None, timeout=120.0)[source]¶
Start a long-lived Acquirium on
data_dir, or the oneconfignames.- Parameters:
data_dir (str | Path | None) – The folder to serve;
.flexorchby default. Not withconfig, whose[server] data_diris 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_dirandconfig, or a config with nodata_dir.StorageError – If one is already running there, or it didn’t start (the message ends with its log).
- Return type:
- flexorchestrator.service.find(data_dir=None)[source]¶
Return the long-lived Acquirium running for
data_dir, if one answers.
- 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
Noneif nothing was running.- Raises:
StorageError – If it’s still answering after
timeoutseconds.- Parameters:
- Return type:
Service | None