Skip to content

Commit

Permalink
More readme stuff. We will need a real ServiceBus Client
Browse files Browse the repository at this point in the history
  • Loading branch information
erewok committed Dec 10, 2024
1 parent 78f4cc3 commit b11438c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The `Boilermaker` application object requires some state (which will get sent to

A task handler for `Boilermaker` is any async function which takes as its first argument application state and which has been registered:

```sh
```python
# This is a background task that we'll register
async def background_task1(state, somearg, somekwarg=True):
"""`state` must be first argument."""
Expand All @@ -26,21 +26,25 @@ async def background_task1(state, somearg, somekwarg=True):
print(somearg)
print(somekwarg)

# Our task must be registered to be invocable.
boilermaker_app.register_async(background_task1, policy=...A retry policy goes here...)
```
**Note**: `Boilermaker` does not currently have a way to store or use the results of tasks, and all arguments must be JSON-serializable.

### Complete Example

For a fuller example, in our application, we may have some code like the following:

```python
from azure.identity.aio import DefaultAzureCredential
from azure.servicebus.aio import ServiceBusSender

from boilermaker.app import Boilermaker
from boilermaker.config import Config
# The boilermaker ServiceBus wrapper is for convenience/example
from boilermaker.service_bus import AzureServiceBus
from boilermaker import retries


# This represents our "App" object
# This represents our "App" object which will be passed as `state` to our handlers
class App:
def __init__(self, data):
self.data = data
Expand All @@ -55,16 +59,15 @@ async def background_task1(state, somearg, somekwarg=True):
print(somekwarg)


azure_identity_async_credential = DefaultAzureCredential()

service_bus_namespace_url = os.environ["SERVICE_BUS_NAMESPACE_URL"]
service_bus_queue_name = os.environ["SERVICE_BUS_QUEUE_NAME"]

# We create a service bus Sender client
sbus_client = AzureServiceBus(
service_bus_namespace_url,
service_bus_queue_name,
azure_identity_async_credential, # type: DefaultAzureCredential
conf = Config(
service_bus_namespace_url=service_bus_namespace_url,
service_bus_queue_name=service_bus_queue_name,
)
# We create a service bus Sender client
service_bus_client = AzureServiceBus(conf)

# Next we'll create a worker and register our task
worker = Boilermaker(App({"key": "value"}), service_bus_client=service_bus_client)
Expand Down
1 change: 0 additions & 1 deletion boilermaker/service_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def _validate_access_settings(self):
raise ValueError("Invalid configuration for AzureServiceBus")
return None


@property
def client(self):
if self._client is None:
Expand Down
22 changes: 9 additions & 13 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import asyncio
import os

from azure.identity.aio import DefaultAzureCredential
from azure.servicebus.aio import ServiceBusSender

from boilermaker.app import Boilermaker
from boilermaker import retries
from boilermaker.app import Boilermaker
from boilermaker.config import Config
from boilermaker.service_bus import AzureServiceBus


azure_identity_async_credential = DefaultAzureCredential()
service_bus_namespace_url = os.environ["SERVICE_BUS_NAMESPACE_URL"]
service_bus_queue_name = os.environ["SERVICE_BUS_QUEUE_NAME"]
conf = Config(
service_bus_namespace_url=service_bus_namespace_url,
service_bus_queue_name=service_bus_queue_name,
)
# We create a service bus Sender client
service_bus_client = AzureServiceBus(conf)

# This represents our "App" object
class App:
Expand All @@ -27,13 +30,6 @@ async def background_task1(state, somearg, somekwarg=True):
print(somekwarg)


# We create a service bus Sender client
service_bus_client = ServiceBusSender(
service_bus_namespace_url,
azure_identity_async_credential,
queue_name=service_bus_queue_name,
)

# Next we'll create a worker and register our task
worker = Boilermaker(App({"key": "value"}), service_bus_client=service_bus_client)
worker.register_async(background_task1, policy=retries.RetryPolicy.default())
Expand Down
18 changes: 8 additions & 10 deletions examples/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import asyncio
import os

from azure.identity.aio import DefaultAzureCredential
from azure.servicebus.aio import ServiceBusSender
from boilermaker import retries
from boilermaker.app import Boilermaker
from boilermaker.config import Config
from boilermaker.failure import TaskFailureResult
from boilermaker.service_bus import AzureServiceBus

azure_identity_async_credential = DefaultAzureCredential()
service_bus_namespace_url = os.environ["SERVICE_BUS_NAMESPACE_URL"]
service_bus_queue_name = os.environ["SERVICE_BUS_QUEUE_NAME"]
conf = Config(
service_bus_namespace_url=service_bus_namespace_url,
service_bus_queue_name=service_bus_queue_name,
)
# We create a service bus Sender client
service_bus_client = AzureServiceBus(conf)


# This represents our "App" object
Expand All @@ -35,13 +40,6 @@ async def sad_path(state):
print("Everything is sad")


# We create a service bus Sender client
service_bus_client = ServiceBusSender(
service_bus_namespace_url,
azure_identity_async_credential,
queue_name=service_bus_queue_name,
)

# Next we'll create a worker and register our task
worker = Boilermaker(App({"key": "value"}), service_bus_client=service_bus_client)

Expand Down

0 comments on commit b11438c

Please sign in to comment.