@codmir/cloudflare-workers
v1.0.1
Published
Distributed task execution using Cloudflare Workers as minions
Maintainers
Readme
Cloudflare Workers - Distributed Task Execution
A distributed task execution system using Cloudflare Workers as "minions" for scalable, edge-based processing of coding tasks.
Architecture
Main Agent → Task Divider → Worker Orchestrator → Cloudflare Workers (Minions)
↓ ↓ ↓ ↓
Complex Task → Micro-Tasks → Load Balancing → Edge Execution
↓ ↓ ↓ ↓
Integration ← Results Aggregation ← Task Results ← Worker ResultsKey Components
1. Task Divider
Breaks down complex tasks into worker-sized chunks:
- Code Analysis: Parallel file processing, import extraction, syntax validation
- File Processing: Distributed file analysis and transformation
- API Integration: Data fetching, transformation, and code generation
- Content Generation: Parallel section generation with aggregation
2. Worker Orchestrator
Manages distributed Cloudflare Workers:
- Load Balancing: Round-robin, least-loaded, geographic, capability-based
- Health Monitoring: Worker heartbeats and performance tracking
- Task Distribution: Dependency-aware execution planning
- Retry Logic: Exponential backoff with configurable limits
3. Worker Minions
Lightweight Cloudflare Workers that execute micro-tasks:
- Capabilities: Text processing, code analysis, API calls, data transformation
- Edge Execution: Low-latency processing across global regions
- Stateless Design: Perfect for parallel, independent operations
4. Integration Layer
Connects with existing agent orchestration system:
- Hybrid Execution: Workers for suitable tasks, agents for complex operations
- Fallback Strategy: Automatic fallback to agents when workers fail
- Result Aggregation: Seamless integration of worker and agent results
Usage
Basic Setup
import { createCloudflareIntegration } from '@codmir/cloudflare-workers';
const integration = createCloudflareIntegration({
accountId: 'your-cloudflare-account-id',
apiToken: 'your-api-token',
scriptName: 'codmir-worker-minion',
maxConcurrentTasks: 100,
loadBalancing: { type: 'least_loaded' }
});
// Execute a task with worker distribution
const result = await integration.orchestrateWithWorkers(
"Analyze all TypeScript files in the project",
{
projectId: 'proj_123',
userId: 'user_456',
files: ['src/app.ts', 'src/utils.ts', 'src/types.ts']
},
{
useWorkers: true,
workerTaskTypes: ['code_analysis', 'file_processing'],
fallbackToAgents: true
}
);Worker Registration
import { WorkerOrchestrator } from '@codmir/cloudflare-workers';
const orchestrator = new WorkerOrchestrator(config);
// Register workers
await orchestrator.registerWorker({
id: 'worker-us-east-1',
name: 'US East Worker',
capabilities: ['text_processing', 'code_analysis'],
region: 'us-east-1',
status: 'idle'
});
// Execute distributed task
const result = await orchestrator.executeTask(
'task_123',
'code_analysis',
'Analyze project dependencies',
{ files: ['package.json', 'src/**/*.ts'] }
);Task Types
Supported Worker Tasks
- analyze_file: File content analysis and metrics
- validate_syntax: Code syntax validation
- extract_imports: Import/dependency extraction
- generate_docs: Documentation generation
- format_code: Code formatting and combining
- run_linter: Code linting and issue detection
- fetch_api_data: API data fetching
- transform_data: Data transformation and processing
- generate_test: Test code generation
- check_dependencies: Dependency analysis and security scanning
Task Division Examples
Code Analysis Task
Original: "Analyze all project files"
↓
Micro-tasks:
├── extract_imports (parallel per file)
├── validate_syntax (parallel per file)
├── check_dependencies (depends on imports)
└── generate_docs (depends on all above)File Processing Task
Original: "Process configuration files"
↓
Micro-tasks:
├── analyze_file (parallel per file)
└── transform_data (aggregate results)Configuration
Environment Variables
# Cloudflare credentials
CLOUDFLARE_ACCOUNT_ID=your-account-id
CLOUDFLARE_API_TOKEN=your-api-token
CLOUDFLARE_SCRIPT_NAME=codmir-worker-minion
# Worker configuration
MAX_CONCURRENT_TASKS=100
TASK_TIMEOUT_MS=30000
LOAD_BALANCING_STRATEGY=least_loaded
# Integration settings
USE_WORKERS=true
FALLBACK_TO_AGENTS=true
WORKER_TASK_TYPES=code_analysis,file_processing,content_generationLoad Balancing Strategies
Least Loaded
{
type: 'least_loaded'
}Geographic
{
type: 'geographic',
config: { preferredRegion: 'us-east-1' }
}Capability Based
{
type: 'capability_based',
config: { prioritizeSpecialized: true }
}Deployment
Deploy Workers
# Install Wrangler CLI
npm install -g wrangler
# Login to Cloudflare
wrangler login
# Deploy worker minions
npm run deploy-worker
# Deploy to specific environment
wrangler deploy --env productionWorker Scaling
Workers automatically scale based on demand:
- Cold Start: ~5ms for new worker instances
- Concurrent Requests: Up to 1000 per worker
- Global Distribution: Automatic edge deployment
- Cost: Pay-per-request pricing model
Monitoring
Worker Health
const status = orchestrator.getStatus();
console.log({
totalWorkers: status.totalWorkers,
activeWorkers: status.activeWorkers,
activeTasks: status.activeTasks,
completedTasks: status.completedTasks
});Performance Metrics
orchestrator.on('task_completed', (event) => {
console.log(`Task ${event.taskId} completed in ${event.totalTimeMs}ms`);
});
orchestrator.on('worker_registered', (worker) => {
console.log(`New worker: ${worker.id} in ${worker.region}`);
});Benefits
Scalability
- Edge Distribution: Workers run in 300+ locations worldwide
- Automatic Scaling: No infrastructure management required
- Parallel Execution: Massive parallelization of suitable tasks
Performance
- Low Latency: Edge execution reduces network overhead
- Fast Cold Starts: Workers start in milliseconds
- Efficient Resource Usage: Pay only for actual execution time
Reliability
- Fault Tolerance: Automatic failover between workers
- Retry Logic: Configurable retry with exponential backoff
- Graceful Degradation: Fallback to agent execution when needed
Cost Efficiency
- Pay-per-Request: No idle server costs
- Resource Optimization: Right-sized execution environments
- Global Distribution: Reduced bandwidth costs
Integration with Existing System
The Cloudflare Workers integration seamlessly extends the existing agent orchestration system:
- Task Analysis: Existing PM Agent analyzes and enriches tasks
- Task Division: New Task Divider breaks suitable tasks into micro-tasks
- Hybrid Execution: Workers handle parallelizable tasks, agents handle complex operations
- Result Aggregation: Results from both systems are combined seamlessly
- Fallback Strategy: Failed worker tasks automatically fall back to agents
This creates a powerful hybrid system that leverages the best of both worlds: the intelligence and flexibility of agents with the scalability and performance of edge computing.
