Skip to main content

Overview

Motia is an event-driven workflow orchestration engine that coordinates complex geospatial processing pipelines. It manages the execution of multi-step workflows, handles dependencies between processing steps, and provides monitoring and error recovery capabilities.

Technology Stack

  • Framework: Motia workflow framework
  • Runtime: Node.js with event-driven architecture
  • Communication: Event streams and message queues
  • State Management: In-memory state with persistence options
  • Monitoring: Built-in metrics and logging

Key Features

Workflow Orchestration

  • Event-Driven: React to events from various sources
  • Step Dependencies: Define complex dependency chains
  • Parallel Execution: Run independent steps concurrently
  • Error Recovery: Automatic retry and compensation logic

Pipeline Management

  • Dynamic Workflows: Modify workflows at runtime
  • Step Reusability: Share processing steps across workflows
  • Parameter Passing: Pass data between workflow steps
  • Conditional Logic: Branch workflows based on conditions

Monitoring & Observability

  • Execution Tracking: Real-time workflow progress monitoring
  • Performance Metrics: Step execution times and resource usage
  • Error Reporting: Detailed error information and stack traces
  • Audit Logging: Complete execution history and logs

Architecture

Event-Driven Design

Motia uses an event-driven architecture where:
  1. Triggers initiate workflow execution
  2. Steps process events and emit new events
  3. Flows coordinate step execution and data flow
  4. States track execution progress and results

Project Structure

apps/motia/
├── steps/
│   ├── lib/                    # Shared utilities
│   ├── nodes/                  # Processing node steps
│   │   ├── api-node.step.ts    # API interaction steps
│   │   ├── aura-node.step.ts   # AI processing steps
│   │   └── ...                 # Other processing steps
│   ├── node-completion-handler.step.ts
│   ├── single-node-execution-start.step.ts
│   ├── workflow-completion-handler.step.ts
│   ├── workflow-execute-node.step.ts
│   └── workflow-execution-start.step.ts
├── utils/                      # Utility functions
├── config.yml                  # Motia configuration
└── package.json

Workflow Components

Steps

Individual processing units that:
  • Receive input events
  • Perform specific operations
  • Emit output events
  • Handle errors gracefully

Flows

Workflow definitions that:
  • Define step sequences
  • Specify dependencies
  • Configure error handling
  • Set execution parameters

Triggers

Event sources that:
  • Start workflow execution
  • Provide initial data
  • Schedule recurring workflows
  • Respond to external events

Configuration

Motia Configuration

# config.yml
name: geoflow-motia
port: 4010

# Environment variables
env:
  NODE_ENV: development
  GEOFLOW_DATA_PATH: /tmp/geoflow-data

# Steps configuration
steps:
  - name: workflow-execution-start
    file: steps/workflow-execution-start.step.ts
  - name: workflow-execute-node
    file: steps/workflow-execute-node.step.ts

Environment Variables

# Service configuration
NODE_ENV=development
GEOFLOW_DATA_PATH=/tmp/geoflow-data

# External services
WORKER_URL=http://geoflow-worker:3002
CONVEX_SELF_HOSTED_URL=http://geoflow-convex-backend:3210

# Processing settings
MAX_CONCURRENT_WORKFLOWS=10
STEP_TIMEOUT_SECONDS=300

Workflow Types

Data Processing Workflows

  • File Ingestion: Upload and validate input files
  • Format Conversion: Convert between geospatial formats
  • Data Processing: Apply algorithms and transformations
  • Quality Assurance: Validate processing results

Analysis Workflows

  • Spatial Analysis: Proximity analysis, overlay operations
  • Statistical Analysis: Generate statistics and reports
  • Visualization: Create maps and charts
  • Export: Generate final deliverables

Integration Workflows

  • API Integration: Connect with external services
  • Data Synchronization: Sync with external databases
  • Notification: Send alerts and reports
  • Archiving: Long-term data storage

Development

Creating Workflow Steps

// steps/custom-processing.step.ts
import { Step } from '@motiadev/core'

export default Step({
  name: 'custom-processing',
  description: 'Custom geospatial processing step',
  handler: async (input) => {
    // Process input data
    const result = await processData(input.data)

    // Emit result event
    return {
      event: 'processing-complete',
      data: result
    }
  }
})

Defining Workflows

// Define a processing workflow
const processingWorkflow = {
  name: 'data-processing',
  steps: [
    {
      name: 'validate-input',
      handler: 'validate-input-step'
    },
    {
      name: 'process-data',
      handler: 'process-data-step',
      dependsOn: ['validate-input']
    },
    {
      name: 'generate-report',
      handler: 'generate-report-step',
      dependsOn: ['process-data']
    }
  ]
}

Local Development

# Install dependencies
bun install

# Start development server
bunx motia dev --port 4010

# Run tests
bun test

# Build for production
bun run build

API Integration

Workflow Execution

// Start workflow execution
POST /workflows/execute
{
  "workflowId": "data-processing-workflow",
  "input": {
    "fileId": "input-file-123",
    "parameters": {
      "format": "GeoTIFF",
      "projection": "EPSG:4326"
    }
  }
}

// Get execution status
GET /executions/:executionId

// Cancel execution
POST /executions/:executionId/cancel

Step Communication

Steps communicate through events:
// Emit event from step
return {
  event: 'step-complete',
  data: {
    result: processedData,
    metadata: {
      processingTime: 1500,
      fileSize: '45MB'
    }
  }
}

// Listen for events in next step
Step({
  name: 'next-step',
  triggers: ['step-complete'],
  handler: async (event) => {
    const { result, metadata } = event.data
    // Process result...
  }
})

Monitoring & Observability

Execution Monitoring

  • Real-time Dashboard: View active workflow executions
  • Step Progress: Track individual step completion
  • Performance Metrics: Execution times and resource usage
  • Error Tracking: Failed steps and error details

Logging

{
  "timestamp": "2024-01-15T10:30:00Z",
  "level": "info",
  "workflowId": "wf-123",
  "executionId": "exec-456",
  "step": "data-processing",
  "message": "Processing completed successfully",
  "duration": 2500,
  "metadata": {
    "inputSize": "100MB",
    "outputSize": "50MB"
  }
}

Metrics

  • Workflow Success Rate: Percentage of successful executions
  • Average Execution Time: Mean time for workflow completion
  • Step Failure Rate: Error rates by step type
  • Resource Utilization: CPU and memory usage trends

Error Handling

Retry Logic

Step({
  name: 'unreliable-step',
  retry: {
    attempts: 3,
    delay: 1000,
    backoff: 'exponential'
  },
  handler: async (input) => {
    // Step implementation with potential failures
  }
})

Compensation

Step({
  name: 'compensating-step',
  compensate: 'cleanup-step',
  handler: async (input) => {
    // Main processing
    return { success: true }
  }
})

// Cleanup step runs if main step fails
Step({
  name: 'cleanup-step',
  handler: async (input) => {
    // Cleanup resources
  }
})

Error Propagation

  • Step-Level Errors: Handled within individual steps
  • Workflow-Level Errors: Can stop entire workflow execution
  • System Errors: Logged and alerted for investigation

Scaling & Performance

Horizontal Scaling

  • Multiple Instances: Run multiple Motia instances
  • Load Balancing: Distribute workflows across instances
  • Shared State: Coordinate through shared database
  • Event Distribution: Broadcast events to all instances

Performance Optimization

  • Step Parallelization: Run independent steps concurrently
  • Resource Pooling: Reuse expensive resources (database connections)
  • Caching: Cache frequently used data and results
  • Async Processing: Non-blocking I/O operations

Resource Management

  • Memory Limits: Configure heap size and garbage collection
  • CPU Limits: Control concurrent step execution
  • Timeout Settings: Prevent runaway processes
  • Queue Management: Control workflow queue depth

Security

Access Control

  • Workflow Permissions: Control who can execute workflows
  • Step Isolation: Sandbox step execution
  • Data Validation: Validate inputs and outputs
  • Audit Logging: Track all workflow activities

Network Security

  • Service Communication: Secure inter-service communication
  • API Authentication: Authenticate external API calls
  • Data Encryption: Encrypt sensitive workflow data
  • Firewall Rules: Restrict network access

Troubleshooting

Common Issues

Workflow Not Starting: Check trigger configuration and event routing Step Timeouts: Increase timeout settings or optimize step performance Event Loss: Verify event broker configuration and connectivity Memory Issues: Monitor heap usage and adjust resource limits

Debug Tools

# View workflow status
curl http://localhost:4010/workflows/status

# Get execution logs
curl http://localhost:4010/executions/:id/logs

# List active workflows
curl http://localhost:4010/workflows/active

# Check service health
curl http://localhost:4010/health

Logging Levels

  • DEBUG: Detailed step execution information
  • INFO: General workflow progress and completion
  • WARN: Non-critical issues and warnings
  • ERROR: Failed executions and system errors