GitHub

entity_resource

API reference for entity_resource.

Resources that keep their entity alive (Effect's EntityResource): acquired once per entity, held across restarts of its handlers, and released when closed or idle.

Classes

Functions

make

make[A, E](acquire: Effect[A, E, CloseScope], *, idle_time_to_live: DurationInput | None = None, acquire_eagerly: bool = False) -> Effect[EntityResource[A, E], E, Scope | Sharding | CurrentAddress]
make[A, E, R](acquire: Effect[A, E, R | CloseScope], *, idle_time_to_live: DurationInput | None = None, acquire_eagerly: bool = False) -> Effect[EntityResource[A, E], E, Scope | R | Sharding | CurrentAddress]

A resource acquired by acquire (with its CloseScope) that keeps the entity alive while held; it is released after idle_time_to_live unused (never by default).

ParameterTypeDescription
acquire
idle_time_to_live= None
acquire_eagerly= False
@overload
def make[A, E](
    acquire: Effect[A, E, CloseScope],
    *,
    idle_time_to_live: DurationInput | None = None,
    acquire_eagerly: bool = False,
) -> Effect[EntityResource[A, E], E, Scope | Sharding | CurrentAddress]: ...
@overload
def make[A, E, R](
    acquire: Effect[A, E, R | CloseScope],
    *,
    idle_time_to_live: DurationInput | None = None,
    acquire_eagerly: bool = False,
) -> Effect[EntityResource[A, E], E, Scope | R | Sharding | CurrentAddress]: ...
def make(
    acquire: Effect[Any, Any, Any],
    *,
    idle_time_to_live: DurationInput | None = None,
    acquire_eagerly: bool = False,
) -> Effect[EntityResource[Any, Any], Any, Any]:
    """A resource acquired by ``acquire`` (with its ``CloseScope``) that
    keeps the entity alive while held; it is released after
    ``idle_time_to_live`` unused (never by default)."""
    require_effect(acquire, "acquire")

    @gen
    def build():
        # Per run, as each activation of the entity has its own lifetime.
        shutting_down = [False]

        @gen
        def acquire_alive():
            yield from keep_alive(True)
            closeable = CloseableScope()

            def release(exit: Exit[Any, Any]) -> Effect[None, Never, Any]:
                if shutting_down[0]:
                    return _VOID
                return closeable.close(exit).and_then(keep_alive(False))

            yield from add_finalizer(release)
            return (yield from acquire.pipe(provide_service(CloseScope, closeable)))

        ref = yield from RcRef.make(
            acquire_alive,
            idle_time_to_live="Infinity" if idle_time_to_live is None else idle_time_to_live,
        )

        def shut_down(_: Exit[Any, Any]) -> Effect[None]:
            shutting_down[0] = True
            return _VOID

        yield from add_finalizer(shut_down)
        if acquire_eagerly:
            yield from scoped(ref.get())
        return EntityResource(ref.get(), ref.invalidate())

    return build

make_k8s_pod

make_k8s_pod(spec, *, idle_time_to_live=None) -> Effect[EntityResource[PodStatus], Never, Scope | Sharding | CurrentAddress | K8sHttpClient]

A Kubernetes pod created from the manifest spec (a whole Pod, with its metadata and spec) for the entity, and deleted when the resource is released.

ParameterTypeDescription
specK8sPod
idle_time_to_liveDurationInput | None = None

Returns Effect[EntityResource[PodStatus], Never, Scope | Sharding | CurrentAddress | K8sHttpClient]

def make_k8s_pod(
    spec: K8sPod, *, idle_time_to_live: DurationInput | None = None
) -> Effect[EntityResource[PodStatus], Never, Scope | Sharding | CurrentAddress | K8sHttpClient]:
    """A Kubernetes pod created from the manifest ``spec`` (a whole ``Pod``,
    with its ``metadata`` and ``spec``) for the entity, and deleted when the
    resource is released."""

    @gen
    def build():
        create_pod = yield from make_create_pod

        @gen
        def acquire():
            scope = yield from CloseScope()
            return (yield from create_pod(spec).pipe(provide_service(Scope, scope)))

        return (yield from make(acquire, idle_time_to_live=idle_time_to_live))

    return build