An open API service indexing awesome lists of open source software.

https://github.com/64j0/poc--postgres-trigger-program

POC for using Postgres to trigger a program execution leveraging F#'s native MailboxProcessor
https://github.com/64j0/poc--postgres-trigger-program

api distributed-systems fsharp giraffe mailbox-processor postgres

Last synced: about 1 month ago
JSON representation

POC for using Postgres to trigger a program execution leveraging F#'s native MailboxProcessor

Awesome Lists containing this project

README

          

#+TITLE: [POC] Postgres trigger program
#+DATE: [2024-12-26 qui]

The idea of this POC project is to create a multi-service application composed
of:

- 1. An HTTP Rest API to interact with the user to set scripts that are going to
be run in the background,
- 2. A Postgres database to store the data (application's source of truth) that
is also responsible for communicating the background application about new
script execution requests,
- 3. A background application leveraging a MailboxProcessor that is notified by
the database whenever a new ~program_execution~ entity is created and it's
used to run the specified program, collect its output and update the database
accordingly.

Other than this, when using the API the user will be able to:

- [X] Register new scripts;
- [X] List scripts that are already registered;
- [ ] Get the script execution results.

** How to run with containers

#+BEGIN_SRC bash :tangle no
# start the containerized project
make compose-up

# start playing with the API and check the logs from the manager process
curl \
-d '{"programName":"placeholder_001"}' \
-H "Content-Type: application/json" \
-X POST \
http://localhost:5000/api/program

curl http://localhost:5000/api/programs

# upload the script (change the necessary details at the following command):
curl --request PATCH \
--url http://localhost:5000/api/program/PROGRAM_ID_GUID \
--header 'Content-Type: multipart/form-data' \
--form script=@/YOUR_LOCAL_PATH/samples/wait.fsx

curl \
-d '{"programInput":"100"}' \
-H "Content-Type: application/json" \
-X POST \
http://localhost:5000/api/execution/PROGRAM_ID_GUID

# check the database and the container logs
#+END_SRC

** How to run with dotnet CLI

#+BEGIN_SRC bash :tangle no
# 1st shell
#
# start the database container with
docker compose up -d postgres

# if you need to restart it use
#
# docker compose down
# docker compose up -d postgres
#
# check the logs with
#
# docker container logs -f postgres_container

# Start the API with
dotnet run --define:DEBUG --project api/

# ===============================================
# 2nd shell
#
# start the manager process
dotnet run --define:DEBUG --project manager/

# ===============================================
# 3rd shell
#
# start playing with the API and check the logs from the manager process
curl \
-d '{"programName":"placeholder_001"}' \
-H "Content-Type: application/json" \
-X POST \
http://localhost:5000/api/program

curl http://localhost:5000/api/programs

# upload the script (change the necessary details at the following command):
curl --request PATCH \
--url http://localhost:5000/api/program/PROGRAM_ID_GUID \
--header 'Content-Type: multipart/form-data' \
--form script=@/YOUR_LOCAL_PATH/samples/get_date.fsx

curl \
-d '{"programInput":"1 2 3"}' \
-H "Content-Type: application/json" \
-X POST \
http://localhost:5000/api/execution/PROGRAM_ID_GUID

curl http://localhost:5000/api/executions
#+END_SRC

** Useful links

- *POSTGRES:*
- https://www.postgresql.org/docs/current/sql-notify.html
- https://www.npgsql.org/doc/wait.html
- https://tedeh.net/publishing-rabbitmq-messages-from-postgresql/
- *F#:*
- https://thesharperdev.com/fsharps-mailboxprocessor/
- https://en.wikibooks.org/wiki/F_Sharp_Programming/MailboxProcessor
- https://fsharpforfunandprofit.com/posts/concurrency-actor-model/