Workflow Lite: A lightweight workflow engine
Project description
State Machine Framework
A flexible state machine implementation with visualization and REST API support.
Quick Start
- Install dependencies:
pip install -e .
Inititialize the database and load some templates
rm workflows.db
PYTHONPATH=${PWD}/src streamlit run app/template_manager.py
Start the API Server
- Start the API server:
uvicorn runtime.workflow_api:app --reload --port 8000
The API documentation will be available at: http://localhost:8000/docs
Start the Workflow simulator
PYTHONPATH=${PWD}/src streamlit run app/simulator.py
The API documentation will be available at: http://localhost:8000/docs
Using the Customer Workflow API
- Assign a workflow to a customer:
curl -X POST http://localhost:8000/workflow/customer/assign \
-H "Content-Type: application/json" \
-d '{
"customer_id": "CUST-001",
"template_name": "Simple"
}'
Response:
{
"customer_id": "CUST-001",
"template_name": "Simple",
"instance_id": "Simple-CUST-001-1678901234567-a1b2c3d4"
}
- Trigger an event for a customer:
curl -X POST http://localhost:8000/workflow/customer/CUST-001/event \
-H "Content-Type: application/json" \
-d '{
"event_name": "case_created",
"event_details": {
"data": {
"amount": 900
}
}
}'
Response:
{
"success": true,
"customer_id": "CUST-001",
"instance_id": "order_process-CUST-001-1678901234567-a1b2c3d4",
"current_state": "submitted",
"state_name": "Order Submitted",
"context": {
"order_id": "12345",
"amount": 100
},
"actions": []
}
- Check customer workflow status:
curl http://localhost:8000/workflow/customer/CUST-001
Common API Operations
- List available templates:
curl http://localhost:8000/workflow/templates
- Get available events for current state:
curl http://localhost:8000/workflow/customer/CUST-001/events
Example Workflow
- Assign order process workflow:
curl -X POST http://localhost:8000/workflow/customer/assign \
-d '{"customer_id": "CUST-001", "template_name": "order_process"}'
- Submit order:
curl -X POST http://localhost:8000/workflow/customer/CUST-001/event \
-d '{
"event_name": "submit",
"event_details": {
"order_id": "ORD-123",
"data": {
"amount": 1500,
"items": ["item1", "item2"]
}
}
}'
- Approve order:
curl -X POST http://localhost:8000/workflow/customer/CUST-001/event \
-d '{
"event_name": "approve",
"event_details": {
"approved_by": "manager1",
"data": {
"notes": "Order approved"
}
}
}'
### Package
```bash
source venv/bin/activate
pip install twine
python setup.py sdist bdist_wheel
pip install dist/wflite-0.0.33-py3-none-any.whl --force-reinstall
twine upload dist/*
Command Line Interface
Workflow Lite includes a command-line interface (CLI) for interacting with workflows:
Installation
When you install Workflow Lite, the CLI tool is automatically installed:
pip install -e .
Usage
The CLI supports several commands:
Trigger an event
# Trigger an event with default customer ID and event name
wflite event
# Trigger an event with specific customer ID and event name
wflite event --customer-id CUST-002 --event-name approve
# Trigger an event with event data
wflite event -c CUST-001 -e submit -d '{"order_id": "ORD-123", "amount": 500}'
# Trigger an event with data from a JSON file
wflite event -c CUST-001 -e submit -d path/to/data.json
Get workflow state
# Get state for default customer ID
wflite state
# Get state for a specific customer
wflite state --customer-id CUST-002
Assign a workflow template
# Assign a workflow with default customer ID and template
wflite assign
# Assign a specific template to a customer
wflite assign --customer-id CUST-002 --template OrderProcess
List available templates
# List all available workflow templates
wflite templates
Environment Configuration
The CLI will use environment variables from your .env file. You can also specify the API URL:
# Set API URL via environment variable
export WFLITE_API_URL=http://api.example.com
# Or specify it directly in the command
wflite event --api-url http://api.example.com
Environment Variable Configuration
Workflow Lite supports configuration through environment variables. This is especially useful for deployment scenarios where you don't want to include credentials in config files.
Using .env Files for Local Development
For local development, you can use a .env file to set environment variables without modifying your system's environment.
-
Create a copy of the template file:
cp .env.template .env
-
Edit the
.envfile and set your configuration values:# Example .env file PERSISTENCE_PROVIDER=mongodb MONGODB_CONNECTION_STRING=mongodb://localhost:27017 MONGODB_DATABASE=workflow_db
-
The application will automatically load these variables when it starts.
Common Environment Variables
# General
export PERSISTENCE_PROVIDER=mongodb # sqlite, mongodb, cosmosdb, dynamodb
# MongoDB/CosmosDB MongoDB API
export MONGODB_CONNECTION_STRING="mongodb://user:password@host:port/database?options"
export MONGODB_HOST="your-mongodb-host"
export MONGODB_PORT=27017
export MONGODB_DATABASE="workflow_db"
export MONGODB_COSMOS_MODE=true # When using CosmosDB with MongoDB API
# CosmosDB SQL API
export COSMOSDB_CONNECTION_STRING="AccountEndpoint=https://your-account.documents.azure.com:443/;AccountKey=your-key;"
# AWS DynamoDB
export DYNAMODB_REGION="us-east-1"
export DYNAMODB_ACCESS_KEY_ID="your-access-key-id"
export DYNAMODB_SECRET_ACCESS_KEY="your-secret-access-key"
Configuration Precedence
Workflow Lite uses the following order of precedence for configuration (highest to lowest):
- Directly provided configuration in API calls
- Environment variables
- Azure Key Vault secrets (if available)
- Default values in the config.yml file
Azure Key Vault Integration
Workflow Lite can securely store and retrieve database credentials and other configuration secrets using Azure Key Vault. This integration is particularly useful for serverless deployments like Azure Functions.
Storing MongoDB Connection String in Azure Key Vault
Follow these steps to store your MongoDB connection string in Azure Key Vault:
-
Create an Azure Key Vault (if you don't have one already):
az keyvault create --name "wflite-kv" --resource-group "your-resource-group" --location "eastus"
-
Add your MongoDB connection string as a secret:
az keyvault secret set --vault-name "wflite-kv" --name "MONGODB-CONNECTION-STRING" --value "mongodb://user:password@host:port/database?options"
-
Create a Managed Identity for your Azure Function App:
az functionapp identity assign --name "rmhub" --resource-group "rg-integration-middleware"
This command will return the principal ID of the managed identity. Copy this ID for the next step.
-
Grant your Function App's Managed Identity access to Key Vault:
az keyvault set-policy --name "wflite-kv" --object-id "b2574ac6-3ac3-4728-afb7-a2f6a82d55f1" --secret-permissions get list
-
Set the KEYVAULT_URL in your Function App's settings:
az functionapp config appsettings set --name "update-mongo-db" --resource-group "rg-integration-middleware" --settings KEYVAULT_URL="https://wflite-kv.vault.azure.net/"
-
Set the MONGODB-CONNECTION-STRING in the key vault:
az keyvault secret set --vault-name "wflite-kv" --name "MONGODB-CONNECTION-STRING" --value "mongodb://user:password@host:port/database?options"
Using Key Vault in Your Code
When running in Azure Functions, Workflow Lite will automatically attempt to retrieve secrets from Key Vault. You don't need to explicitly provide credentials in your function code:
import azure.functions as func
from wflite.runtime.serverless import azure_function_handler
import json
def main(req: func.HttpRequest) -> func.HttpResponse:
result = azure_function_handler(req)
return func.HttpResponse(
json.dumps(result),
mimetype="application/json",
status_code=200 if result.get('success') else 400
)
The serverless module will:
- Check if you're running in Azure Functions environment
- Attempt to use the Managed Identity to access Key Vault
- Retrieve the MongoDB connection string and other configured secrets
- Apply these secrets to your workflow configuration
Available Secret Names
The following secret names are currently supported:
MONGODB-CONNECTION-STRING: Connection string for MongoDB/CosmosDBCOSMOSDB-CONNECTION-STRING: Connection string for CosmosDB SQL APIDYNAMODB-CONNECTION-STRING: Connection string for AWS DynamoDB
To add more secrets, modify the secret_names list in wflite/config/azure_keyvault.py.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wflite-0.0.38.tar.gz.
File metadata
- Download URL: wflite-0.0.38.tar.gz
- Upload date:
- Size: 58.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2937f37caf99ac4c0dd032fe55d14ba88bfe47dd2c1cf654e26ffe020e59563b
|
|
| MD5 |
29fdb526af7c01ae88b96241f807249e
|
|
| BLAKE2b-256 |
5f84cbb9e4282522862c007a27561a37d6b62e41873569cfa76e7f0db2e1afa2
|
File details
Details for the file wflite-0.0.38-py3-none-any.whl.
File metadata
- Download URL: wflite-0.0.38-py3-none-any.whl
- Upload date:
- Size: 62.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc7b6947a9eab3bcd447b8e4781a3858874dd7cd048ed63289273158f159f8ad
|
|
| MD5 |
3a303e54ece7c268a985302265206275
|
|
| BLAKE2b-256 |
1d068c8eaf66446dafc4239ce9dcae4bee701f657ae16d0d01cdb1375d24ca0e
|