greenie-msft / bindings-dapr-nodejs-cron-postgres

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dapr Bindings (Dapr SDK)

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.

Run and develop locally

Run and initialize PostgreSQL container

  1. Open a new terminal, change directories to ../../db, and run the container with Docker Compose:
cd db/
docker compose up -d

Run Javascript service with Dapr

  1. Open a new terminal window, change directories to ./batch in the quickstart directory and run:
cd ../batch
npm install
  1. Run the Javascript service app with Dapr:
dapr run --app-id batch-sdk --app-port 5002 --dapr-http-port 3500 --components-path ../components -- node index.js
  1. 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
  1. Stop postgres container
cd ../db
docker compose stop

Deploy to Azure (Azure Container Apps and Azure Postgres)

Deploy to Azure for dev-test

NOTE: make sure you have Azure Dev CLI pre-reqs here

  1. Provision infra and deploy application:
azd up
  1. 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.

About


Languages

Language:Bicep 97.2%Language:JavaScript 2.5%Language:Dockerfile 0.2%Language:Makefile 0.1%