Skip to main content

Virtual Environment Setup

setup.sh

Environment Setup

The .env file is created when you download your agent code:
This downloads your agent code from the platform with the .env file pre-configured with your keys.
lifecycle_management.py

How to Run

lifecycle_management.py

What This Example Demonstrates

Initialization Phase (@on_boot)

  1. Environment Validation - Checks required environment variables before starting
  2. Database Setup - Establishes database connections asynchronously
  3. Cache Initialization - Sets up in-memory caches for performance
  4. Metrics Collection - Starts background monitoring systems

Task Processing (@on_task)

  • Uses resources initialized during boot phase
  • Demonstrates database queries and cache usage
  • Shows how lifecycle management enables shared resources

Cleanup Phase (@on_shutdown)

  1. Data Persistence - Saves cache data before shutdown
  2. Connection Cleanup - Gracefully closes database connections
  3. Metrics Finalization - Stops monitoring and saves reports
  4. Memory Cleanup - Clears sensitive data from memory

Key Features

Sequential Execution

Handlers execute in registration order, allowing proper dependency management

Mixed Async/Sync

Seamlessly combines synchronous and asynchronous initialization/cleanup tasks

Error Handling

Boot failures prevent startup, shutdown errors are logged but don’t block exit

Resource Management

Proper initialization and cleanup of shared resources like databases and caches

Best Practices Shown

  1. Environment Validation - Check requirements early in boot process
  2. Resource Dependencies - Initialize resources in correct order
  3. Graceful Cleanup - Properly close connections and save data on shutdown
  4. Error Resilience - Handle cleanup gracefully even if resources are unavailable
  5. Memory Management - Clear sensitive data during shutdown

Production Deployment

For creating and deploying agents to production, see the Setup and Deployment Guide.