In this quickstart, you'll create a microservice to demonstrate Dapr's bindings API to work with external systems as inputs and outputs. The service listens to input binding events from a system CRON and then outputs the contents of local data to a PostreSql output binding.
Visit this link for more information about Dapr and Bindings.
- Open a new terminal, change directories to
../../db
, and run the container with Docker Compose:
cd db/
docker compose up -d
- Open a new terminal window, change directories to
./batch
in the quickstart directory and run:
cd ../batch
pip install -r requirements.txt
- Run the Python service app with Dapr:
dapr run --app-id batch-sdk --app-port 5001 --dapr-http-port 3500 --components-path ../components -- python3 app.py
- Expected output: A batch script runs every 10 seconds using an input Cron binding. The script processes a JSON file and outputs data to a SQL database using the PostgreSQL Dapr binding:
== APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
== APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
== APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
== APP == Finished processing batch
== APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
== APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
== APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
== APP == Finished processing batch
== APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
== APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
== APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
== APP == Finished processing batch
- Stop postgres container
cd ../db
docker compose stop
Deploy to Azure for dev-test
NOTE: make sure you have Azure Dev CLI pre-reqs here and are on version 0.9.0-beta.3 or greater.
- Run the following command to initialize the project.
azd init --template https://github.com/Azure-Samples/bindings-dapr-python-cron-postgres
This command will clone the code to your current folder and prompt you for the following information:
Environment Name
: This will be used as a prefix for the resource group that will be created to hold all Azure resources. This name should be unique within your Azure subscription.
- Run the following command to package a deployable copy of your application, provision the template's infrastructure to Azure and also deploy the application code to those newly provisioned resources.
azd up
This command will prompt you for the following information:
Azure Location
: The Azure location where your resources will be deployed.Azure Subscription
: The Azure Subscription where your resources will be deployed.
NOTE: This may take a while to complete as it executes three commands:
azd package
(packages a deployable copy of your application),azd provision
(provisions Azure resources), andazd deploy
(deploys application code). You will see a progress indicator as it packages, provisions and deploys your application.
- Confirm the deployment is susccessful:
Navigate to the Container App resource for the Batch service. Locate the Log stream
and confirm the batch container is logging each insert successfully every 10s.