Create or Register namespace with python-sdk and code

I was struggling to register namespace from the worker, but thanks to @Chad_Retz for the solution. I decided to post it here, so other people can search for the solution.

import asyncio
import logging

from google.protobuf.duration_pb2 import Duration
from temporalio.api.workflowservice.v1 import (ListNamespacesRequest,
                                               RegisterNamespaceRequest)
from temporalio.service import ConnectConfig, ServiceClient


async def main():
    logging.basicConfig(level=logging.INFO)

    client = await ServiceClient.connect(ConnectConfig(target_host="127.0.0.1:7233"))

    # List namespaces
    list_resp = await client.workflow_service.list_namespaces(ListNamespacesRequest())
    print(f"First page of {len(list_resp.namespaces)} namespaces:")
    for namespace in list_resp.namespaces:
        print(f"  Namespace: {namespace.namespace_info.name}")

    print("Attempting to add namespace 'my-namespace'")
    await client.workflow_service.register_namespace(
        RegisterNamespaceRequest(
            namespace="my-namespace",
            # 3 day retention period
            workflow_execution_retention_period=Duration(seconds=3 * 24 * 60 * 60),
        )
    )
    print("Registration complete (may take a few seconds to be usable)")


if __name__ == "__main__":
    asyncio.run(main())
2 Likes