dispatchrun / dispatch-py

Python package to develop applications with Dispatch.
https://pypi.org/project/dispatch-py/
Apache License 2.0
54 stars 3 forks source link

Mock server #126

Closed chriso closed 6 months ago

chriso commented 6 months ago

It's difficult to try out Dispatch today. Users either have to use ngrok to expose their test functions to the production Dispatch instance, or must manually fit the pieces together from the dispatch.test package (#86) to setup a local server that they can connect to.

This PR introduces a front-end to the components in the dispatch.test package, via a python -m dispatch.test command. Users can use this to quickly spin up a mock Dispatch server locally.

$ python -m dispatch.test --help
Mock Dispatch server for use in test environments.

Usage:
  dispatch.test <endpoint> [--api-key=<key>] [--hostname=<name>] [--port=<port>] [-v | --verbose]
  dispatch.test -h | --help

Options:
     --api-key=<key>      API key to require when clients connect to the server [default: test].

     --hostname=<name>    Hostname to listen on [default: 127.0.0.1].
     --port=<port>        Port to listen on [default: 4450].

  -v --verbose            Show verbose details in the log.
  -h --help               Show this help information.

The mock server is a very basic in-memory implementation of the Dispatch interface. It will dispatch function calls from a FIFO queue, and supports both polling and tail calls, but it doesn't have any scheduling capabilities implemented beyond that (e.g. adaptive concurrency control).

Example

When you start the server, it prints configuration for a local endpoint or stand-alone client:

$ python -m dispatch.test http://127.0.0.1:8000
Spawned a mock Dispatch server on 127.0.0.1:4450 to dispatch
function calls to the endpoint at http://127.0.0.1:8000.

The Dispatch SDK can be configured with:

  export DISPATCH_API_URL="http://127.0.0.1:4450"
  export DISPATCH_API_KEY="test"
  export DISPATCH_ENDPOINT_URL="http://127.0.0.1:8000"
  export DISPATCH_VERIFICATION_KEY="Z+nTe2VRcw8t8Ihx++D+nXtbO28nwjWIOTLRgzrelYs="

By default, a static API key and verification key is used, so that users don't have to reconfigure their endpoints each time.

Here's an example endpoint with one function:

test.py

from fastapi import FastAPI
from dispatch.fastapi import Dispatch

app = FastAPI()
dispatch = Dispatch(app)

@dispatch.function
def myfunction():
    print("Running my function")

print("Dispatching call to my function")
myfunction.dispatch()

If I start the endpoint on the URL expected by the mock Dispatch server, I see that the function was called correctly:

$ uvicorn test:app --port 8000
Dispatching call to my function
...
Running my function