Skip to main content

Workflow Lite: A lightweight workflow engine

Project description

State Machine Framework

A flexible state machine implementation with visualization and REST API support.

Quick Start

  1. 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

  1. 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

  1. 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"
}
  1. 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": []
}
  1. 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

  1. Assign order process workflow:
curl -X POST http://localhost:8000/workflow/customer/assign \
-d '{"customer_id": "CUST-001", "template_name": "order_process"}'
  1. 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"]
        }
    }
}'
  1. 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.

  1. Create a copy of the template file:

    cp .env.template .env
    
  2. Edit the .env file and set your configuration values:

    # Example .env file
    PERSISTENCE_PROVIDER=mongodb
    MONGODB_CONNECTION_STRING=mongodb://localhost:27017
    MONGODB_DATABASE=workflow_db
    
  3. 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):

  1. Directly provided configuration in API calls
  2. Environment variables
  3. Azure Key Vault secrets (if available)
  4. 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:

  1. 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"
    
  2. 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"
    
  3. 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.

  4. 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
    
  5. 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/"
    
  6. 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:

  1. Check if you're running in Azure Functions environment
  2. Attempt to use the Managed Identity to access Key Vault
  3. Retrieve the MongoDB connection string and other configured secrets
  4. Apply these secrets to your workflow configuration

Available Secret Names

The following secret names are currently supported:

  • MONGODB-CONNECTION-STRING: Connection string for MongoDB/CosmosDB
  • COSMOSDB-CONNECTION-STRING: Connection string for CosmosDB SQL API
  • DYNAMODB-CONNECTION-STRING: Connection string for AWS DynamoDB

To add more secrets, modify the secret_names list in wflite/config/azure_keyvault.py.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wflite-0.0.41.tar.gz (57.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wflite-0.0.41-py3-none-any.whl (61.3 kB view details)

Uploaded Python 3

File details

Details for the file wflite-0.0.41.tar.gz.

File metadata

  • Download URL: wflite-0.0.41.tar.gz
  • Upload date:
  • Size: 57.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for wflite-0.0.41.tar.gz
Algorithm Hash digest
SHA256 d2eaa9173955eaa7c04ff53f1abc12739ca55d52d897592acac49b46211d7891
MD5 9fd8648f865980bb6e2e9c97bc3fe929
BLAKE2b-256 9e172c17416568fc930a81a474abb3462b69ef65f53ee2051ae0705029085be2

See more details on using hashes here.

File details

Details for the file wflite-0.0.41-py3-none-any.whl.

File metadata

  • Download URL: wflite-0.0.41-py3-none-any.whl
  • Upload date:
  • Size: 61.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for wflite-0.0.41-py3-none-any.whl
Algorithm Hash digest
SHA256 654ca9b1360432d938e0022b5566cf79eda9853a9320534bc25e4c1659c88b16
MD5 d68831f2d35272c5598500940b1d0412
BLAKE2b-256 3d950b1d746fa67323cbdb403cb34f2ebb111f1fba8a4d1e3869090a57eea174

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page