shipdeck
Version:
Ship MVPs in 48 hours. Fix bugs in 30 seconds. The command deck for developers who ship.
364 lines (313 loc) • 10.3 kB
JavaScript
/**
* Parallel Runner Agent Usage Examples
* Demonstrates how to use the ParallelRunnerAgent for orchestrating concurrent execution
*/
// Example usage patterns for the ParallelRunnerAgent
/**
* Basic Parallel Execution Example
*/
async function basicParallelExecution() {
const { ParallelRunnerAgent } = require('./parallel-runner');
const parallelRunner = new ParallelRunnerAgent({
maxWorkers: 4,
workerTimeout: 30000,
taskTimeout: 60000
});
// Define tasks to execute in parallel
const tasks = [
{
id: 'auth-setup',
type: 'backend',
description: 'Setup user authentication system',
agentPath: './backend-agent.js',
taskDefinition: {
type: 'auth',
description: 'Create JWT-based authentication with user registration and login'
}
},
{
id: 'ui-components',
type: 'frontend',
description: 'Create UI components',
agentPath: './frontend-agent.js',
taskDefinition: {
type: 'ui',
description: 'Build login form, dashboard, and navigation components'
}
},
{
id: 'api-docs',
type: 'documentation',
description: 'Generate API documentation',
agentPath: './documentation-agent.js',
taskDefinition: {
type: 'docs',
description: 'Auto-generate OpenAPI specifications for all endpoints'
}
}
];
try {
const result = await parallelRunner.execute({
type: 'parallel-batch',
tasks: tasks
});
console.log('✅ Parallel execution completed');
console.log(`📊 Results: ${result.results.length} tasks`);
console.log(`📈 Success rate: ${calculateSuccessRate(result.results)}%`);
console.log(`⚡ Peak concurrency: ${result.stats.peakConcurrency}`);
return result;
} catch (error) {
console.error('❌ Parallel execution failed:', error.message);
throw error;
} finally {
await parallelRunner.cleanup();
}
}
/**
* Resource Conflict Aware Execution
*/
async function conflictAwareExecution() {
const { ParallelRunnerAgent } = require('./parallel-runner');
const parallelRunner = new ParallelRunnerAgent();
// Tasks with potential file conflicts
const tasks = [
{
id: 'database-migration',
type: 'backend',
description: 'Run database migrations',
files: [{ path: './database/schema.sql', mode: 'write' }],
database: { connection: 'main', schema: 'public', mode: 'write' },
agentPath: './database-agent.js',
taskDefinition: {
type: 'migration',
description: 'Apply pending database migrations'
}
},
{
id: 'seed-data',
type: 'backend',
description: 'Seed database with initial data',
database: { connection: 'main', schema: 'public', mode: 'write' },
agentPath: './database-agent.js',
taskDefinition: {
type: 'seed',
description: 'Insert initial application data'
}
},
{
id: 'generate-types',
type: 'codegen',
description: 'Generate TypeScript types',
files: [{ path: './src/types/generated.ts', mode: 'write' }],
database: { connection: 'main', schema: 'public', mode: 'read' },
agentPath: './codegen-agent.js',
taskDefinition: {
type: 'types',
description: 'Generate TypeScript interfaces from database schema'
}
}
];
try {
const result = await parallelRunner.execute({
type: 'parallel-batch',
tasks: tasks
});
// The runner automatically detected conflicts and scheduled tasks appropriately
console.log(`🔒 Conflicts detected: ${result.conflicts.length}`);
result.conflicts.forEach(conflict => {
console.log(` • ${conflict.resource} (${conflict.type}): ${conflict.tasks.join(', ')}`);
});
return result;
} finally {
await parallelRunner.cleanup();
}
}
/**
* Batch Processing with Concurrency Control
*/
async function batchProcessingExample() {
const { ParallelRunnerAgent } = require('./parallel-runner');
const parallelRunner = new ParallelRunnerAgent({
maxWorkers: 6
});
// Large batch of similar tasks
const userIds = Array.from({ length: 100 }, (_, i) => i + 1);
const tasks = userIds.map(userId => ({
id: `process-user-${userId}`,
type: 'processing',
description: `Process user data for user ${userId}`,
agentPath: './data-processing-agent.js',
taskDefinition: {
type: 'user-processing',
userId: userId,
description: `Analyze and process user ${userId} activity data`
}
}));
try {
const result = await parallelRunner.execute({
type: 'parallel-batch',
tasks: tasks,
maxConcurrency: 10 // Limit concurrent processing
});
console.log(`📦 Processed ${tasks.length} users`);
console.log(`⚡ Peak concurrency: ${result.stats.peakConcurrency}`);
console.log(`⏱️ Average task time: ${result.stats.averageExecutionTime}ms`);
return result;
} finally {
await parallelRunner.cleanup();
}
}
/**
* Multi-Environment Deployment
*/
async function multiEnvironmentDeployment() {
const { ParallelRunnerAgent } = require('./parallel-runner');
const parallelRunner = new ParallelRunnerAgent();
const environments = ['staging', 'production', 'demo'];
const tasks = environments.map(env => ({
id: `deploy-${env}`,
type: 'deployment',
description: `Deploy to ${env} environment`,
ports: env === 'production' ? [443, 80] : [3000 + environments.indexOf(env)],
agentPath: './deployment-agent.js',
taskDefinition: {
type: 'deploy',
environment: env,
description: `Build and deploy application to ${env} environment`
}
}));
try {
const result = await parallelRunner.execute({
type: 'parallel-batch',
tasks: tasks,
timeout: 300000 // 5 minute timeout for deployments
});
const successful = result.results.filter(r => r.success);
const failed = result.results.filter(r => !r.success);
console.log(`✅ Successful deployments: ${successful.map(r => r.taskId).join(', ')}`);
if (failed.length > 0) {
console.log(`❌ Failed deployments: ${failed.map(r => r.taskId).join(', ')}`);
}
return result;
} finally {
await parallelRunner.cleanup();
}
}
/**
* Performance Testing with Real-time Monitoring
*/
async function performanceTestingExample() {
const { ParallelRunnerAgent } = require('./parallel-runner');
const parallelRunner = new ParallelRunnerAgent({
maxWorkers: 8,
enableResourceMonitoring: true
});
// Setup performance testing tasks
const testSuites = [
{ name: 'unit-tests', estimated: 30000 },
{ name: 'integration-tests', estimated: 60000 },
{ name: 'load-tests', estimated: 120000 },
{ name: 'security-tests', estimated: 90000 }
];
const tasks = testSuites.map(suite => ({
id: `test-${suite.name}`,
type: 'testing',
description: `Run ${suite.name.replace('-', ' ')}`,
estimatedDuration: suite.estimated,
agentPath: './testing-agent.js',
taskDefinition: {
type: 'test-suite',
suite: suite.name,
description: `Execute ${suite.name} and generate reports`
}
}));
// Monitor progress
const progressInterval = setInterval(() => {
const stats = parallelRunner.getExecutionStats();
console.log(`📊 Progress: ${stats.tasksCompleted}/${tasks.length} tasks, ${stats.activeWorkers} workers active`);
}, 5000);
try {
const startTime = Date.now();
const result = await parallelRunner.execute({
type: 'parallel-batch',
tasks: tasks
});
const totalTime = Date.now() - startTime;
clearInterval(progressInterval);
console.log(`🏁 All tests completed in ${totalTime}ms`);
console.log(`📈 Performance Stats:`);
console.log(` • Average task time: ${result.stats.averageExecutionTime}ms`);
console.log(` • Peak concurrency: ${result.stats.peakConcurrency}`);
console.log(` • Total tasks: ${result.stats.totalTasks}`);
console.log(` • Success rate: ${Math.round((result.stats.tasksCompleted / result.stats.totalTasks) * 100)}%`);
return result;
} finally {
clearInterval(progressInterval);
await parallelRunner.cleanup();
}
}
/**
* Event-Driven Execution with Progress Tracking
*/
async function eventDrivenExecution() {
const { ParallelRunnerAgent } = require('./parallel-runner');
const parallelRunner = new ParallelRunnerAgent();
// Setup event listeners for detailed tracking
parallelRunner.eventEmitter.on('task:completed', ({ taskId, duration }) => {
console.log(`✅ Task ${taskId} completed in ${duration}ms`);
});
parallelRunner.eventEmitter.on('task:failed', ({ taskId, error }) => {
console.error(`❌ Task ${taskId} failed: ${error.message}`);
});
parallelRunner.eventEmitter.on('task:cancelled', ({ taskId }) => {
console.log(`🚫 Task ${taskId} was cancelled`);
});
const tasks = [
{
id: 'build-frontend',
type: 'build',
description: 'Build React frontend',
agentPath: './build-agent.js',
taskDefinition: {
type: 'frontend-build',
description: 'Compile TypeScript and bundle React application'
}
},
{
id: 'build-backend',
type: 'build',
description: 'Build Node.js backend',
agentPath: './build-agent.js',
taskDefinition: {
type: 'backend-build',
description: 'Compile TypeScript backend services'
}
}
];
try {
const result = await parallelRunner.execute({
type: 'parallel-batch',
tasks: tasks
});
return result;
} finally {
await parallelRunner.cleanup();
}
}
/**
* Utility function to calculate success rate
*/
function calculateSuccessRate(results) {
if (!Array.isArray(results) || results.length === 0) return 0;
const successful = results.filter(r => r.success).length;
return Math.round((successful / results.length) * 100);
}
module.exports = {
basicParallelExecution,
conflictAwareExecution,
batchProcessingExample,
multiEnvironmentDeployment,
performanceTestingExample,
eventDrivenExecution,
calculateSuccessRate
};