schedr.connections

Connections (archive/PROJECT.md, Connections; decisions 53 and 174): what a task connects to.

A connection is a generator function, as with contextlib.contextmanager(), whose parameters are its shape; @connection(...) gives their values, which may be secrets, so each environment supplies its own:

@connection(uri=t"{secrets['mongo/default']['uri']}", database="orders")
def mongodb_default(uri: str, database: str) -> Iterator[Database]:
    client = MongoClient(uri)
    try:
        yield client[database]
    finally:
        client.close()

@task(connections=[mongodb_default])
def count() -> int:
    with mongodb_default() as db:
        return db.orders.count_documents({})

A task opens only the connections it declares (decision 179). The worker’s agent renders each before the task starts (fetching its secrets, masked in the task’s log); entering it, the function runs up to yield, and leaving runs the rest. The parser records every connection of a DAG folder (its name and values, never a secret’s value): the agent renders only what was recorded. Its values are secrets and plain values (decision 180).

class schedr.connections.HTTP(name, /, *, base_url, token=None, user=None, password=None, headers=None, ca_cert=None, client_cert=None, client_key=None, timeout=30.0, check_path=None)

Bases: TwoWayConnection[object, object]

An HTTP API: an httpx.Client with its base URL, authentication and TLS settings.

with billing_api() as client: client.get("/invoices"); async with gives an httpx.AsyncClient.

Parameters:
  • name (str)

  • base_url (str)

  • token (str | None)

  • user (str | None)

  • password (str | None)

  • headers (dict[str, str] | None)

  • ca_cert (str | None)

  • client_cert (str | None)

  • client_key (str | None)

  • timeout (float)

  • check_path (str | None)

class schedr.connections.SQL(name, /, *, url, user=None, password=None, ca_cert=None, timeout=30.0)

Bases: TwoWayConnection[DbApiConnection, object]

A Postgres or MySQL database, by URL: a DB-API connection (psycopg 3, PyMySQL).

with warehouse() as db: cursor = db.cursor(); the block’s work is committed when it ends, rolled back on an error, as psycopg’s own with does.

TLS is libpq’s sslmode in the URL, for both: verify-full (the default) needs TLS and checks the server’s certificate and name, against ca_cert or the system’s CAs; verify-ca the certificate only; require TLS, unchecked; prefer and allow TLS when offered; disable none (?sslmode=disable, for a server without TLS).

Parameters:
  • name (str)

  • url (str)

  • user (str | None)

  • password (str | None)

  • ca_cert (str | None)

  • timeout (float)

class schedr.connections.SSH(name, /, *, host, user, port=22, private_key=None, passphrase=None, password=None, known_hosts=None, insecure_skip_host_check=False, timeout=30.0)

Bases: Connection[object]

An SSH host (network devices too): a connected paramiko SSHClient.

with router() as client: client.exec_command("show version"). The host’s key must be in known_hosts; insecure_skip_host_check=True accepts any, loudly.

Parameters:
  • name (str)

  • host (str)

  • user (str)

  • port (int)

  • private_key (str | None)

  • passphrase (str | None)

  • password (str | None)

  • known_hosts (str | None)

  • insecure_skip_host_check (bool)

  • timeout (float)

class schedr.connections.AsyncConnection(function, values)

Bases: ConnectionBase[T_co]

An async def connection: async with name() as client: in an async def task.

Parameters:
  • function (Callable[[...], AsyncIterator[T_co]])

  • values (Mapping[str, Value])

fn

The function as an async context manager, for calling it yourself (in a test) with values of your own: async with mongodb_default.fn(uri=...) as db:.

run_test()

Opens the connection and runs its check, or opens and closes it without one.

What schedr connections test NAME runs on a worker (decision 198); the check may be async def. As a task’s coroutine: a timeout’s stop signal lets its finally run.

Return type:

None

class schedr.connections.Connection(function, values, *, name=None, kind=None, checker=None)

Bases: ConnectionBase[T_co]

A connection: with name() as client: in a task (see the module’s description).

Parameters:
  • function (Callable[[...], Iterator[T_co]])

  • values (Mapping[str, Value])

  • name (str | None)

  • kind (str | None)

  • checker (Checker[T_co] | None)

fn

The function as a context manager, for calling it yourself (in a test) with values of your own: with mongodb_default.fn(uri=..., database=...) as db:.

run_check()

Opens the connection and runs its check, in a running task.

Return type:

None

run_test()

Opens the connection and runs its check, or opens and closes it without one.

What schedr connections test NAME runs on a worker (decision 198).

Return type:

None

class schedr.connections.ConnectionBase(function, values, *, name=None, kind=None, checker=None)

Bases: Generic[T_co]

What Connection and AsyncConnection share.

Parameters:
  • function (Callable[[...], object])

  • values (Mapping[str, Value])

  • name (str | None)

  • kind (str | None)

  • checker (Checker[T_co] | None)

name

Its name: the function’s, or a typed connection’s first argument.

kind

A typed connection’s type ("http", …); None for a @connection.

check(function)

Gives the connection a check (@name.check), replacing a typed one’s own.

schedr connections test NAME runs it on a worker: it gets the client and raises when the connection doesn’t work (a Mongo ping, a SELECT 1).

Parameters:
  • function (Callable[[U], R]) – The check, given what the connection yields: a type checker refuses one taking something else.

  • self (ConnectionBase)

Returns:

function, unchanged.

Return type:

Callable[[U], R]

run_test()

Opens the connection and runs its check, or opens and closes it without one.

What schedr connections test NAME runs on a worker (decision 198).

Return type:

None

exception schedr.connections.ConnectionDeniedError

Bases: LookupError

A connection the task opens without declaring it in connections= (decision 179).

exception schedr.connections.ConnectionNotFoundError

Bases: LookupError

A connection that can’t be opened: not recorded by the parser, or no double for it.

class schedr.connections.DbApiConnection(*args, **kwargs)

Bases: Protocol

A DB-API connection, as psycopg’s and PyMySQL’s are.

cursor()

A new cursor.

Return type:

DbApiCursor

commit()

Commits the transaction.

Return type:

None

rollback()

Rolls it back.

Return type:

None

close()

Closes the connection.

Return type:

None

class schedr.connections.Generic(name, /, **values)

Bases: TwoWayConnection[dict[str, object], dict[str, object]]

Keys of one’s own: the task gets them as a dict.

with feature_flags() as flags: flags["region"], or async with.

Parameters:
  • name (str)

  • values (object)

class schedr.connections.Mongo(name, /, *, uri, database, user=None, password=None, ca_cert=None, timeout=30.0)

Bases: TwoWayConnection[object, object]

A MongoDB database: a pymongo Database, its client closed when the block ends.

with orders_db() as db: db.orders.count_documents({}). TLS is used and verified unless the URI says tls=false (decision 189): against ca_cert, else the system’s CAs; the URI’s own TLS options (tlsAllowInvalidHostnames=true, …) as written.

Parameters:
  • name (str)

  • uri (str)

  • database (str)

  • user (str | None)

  • password (str | None)

  • ca_cert (str | None)

  • timeout (float)

class schedr.connections.SchedrApi(name, /, *, server, token=None, user=None, password=None)

Bases: Connection[object]

A schedr deployment’s REST API, over HTTP: a schedr.api.Client.

For another deployment, or this one as a user of its own: with other_schedr() as api: api.post_event(...). A task reaches its own deployment through schedr.api with no connection at all.

Parameters:
  • name (str)

  • server (str)

  • token (str | None)

  • user (str | None)

  • password (str | None)

class schedr.connections.TwoWayConnection(function, async_function, values, *, name, kind, checker)

Bases: Connection[T_co], Generic[T_co, A_co]

A typed connection: with gives its sync client, async with its async one.

Parameters:
  • function (Callable[[...], Iterator[T_co]])

  • async_function (Callable[[...], AsyncIterator[A_co]])

  • values (Mapping[str, Value])

  • name (str)

  • kind (str)

  • checker (Checker[T_co])

afn

The async function as an async context manager, as fn is the sync one.

schedr.connections.connection(**values)

Makes a generator function a connection, with its parameters’ values.

An async def generator gives a connection for async with.

Parameters:

**values (object) – A value for each of its parameters (those with defaults may be left out): plain values, secrets (secrets['mongo/default']['uri']), and t-strings of them. A run’s values (dag_ctx) go to the task’s own arguments instead.

Returns:

The decorator.

Return type:

_ConnectionDecorator