Route tasks to specific worker pools with TypeScript
import { HyrexRegistry } from '@hyrex/hyrex'; const hy = new HyrexRegistry(); // Simple string queue const emailTask = hy.task({ name: 'sendEmail', config: { queue: 'emails' }, func: async (data) => { // Task will be processed by workers listening to 'emails' queue return { sent: true }; } }); // Default queue (if not specified) const defaultTask = hy.task({ name: 'defaultTask', func: async (data) => { // Goes to 'default' queue return { processed: true }; } });
import { HyrexQueue } from '@hyrex/hyrex'; // Define queue with concurrency limit const cpuIntensiveTask = hy.task({ name: 'cpuIntensive', config: { queue: { name: 'cpu-intensive', concurrencyLimit: 2 // Max 2 concurrent tasks } }, func: async (data) => { // Heavy computation return { result: 'computed' }; } }); // Or create HyrexQueue instance const gpuQueue = new HyrexQueue({ name: 'gpu-processing', concurrencyLimit: 1 // Single GPU task at a time }); const mlTask = hy.task({ name: 'trainModel', config: { queue: gpuQueue }, func: async (data) => { // GPU-intensive ML training return { model: 'trained' }; } });
# Listen to single queue hyrex run-worker hyrex-app.ts --queue emails # Listen to all queues (default) hyrex run-worker hyrex-app.ts --queue "*" # Use glob patterns hyrex run-worker hyrex-app.ts --queue "email-*" hyrex run-worker hyrex-app.ts --queue "priority-?" hyrex run-worker hyrex-app.ts --queue "{emails,sms,push}"
*
?
[abc]
{a,b,c}
const flexibleTask = hy.task({ name: 'flexibleTask', config: { queue: 'default' }, func: async (data) => { return { processed: true }; } }); // Send to default queue const taskId1 = await flexibleTask.send({ data: 'normal' }); // Override to high-priority queue const taskId2 = await flexibleTask.withConfig({ queue: 'high-priority' }).send({ data: 'urgent' });
// High priority queue const urgentTask = hy.task({ name: 'urgentTask', config: { queue: 'high-priority', priority: 9 // Higher task priority within queue }, func: async (data) => { return { handled: 'immediately' }; } }); // Low priority queue const batchTask = hy.task({ name: 'batchTask', config: { queue: 'low-priority', priority: 2 }, func: async (data) => { return { processed: 'eventually' }; } });
// CPU-intensive tasks const analyzeData = hy.task({ name: 'analyzeData', config: { queue: 'cpu-intensive', timeoutSeconds: 600 }, func: async (data) => { // Complex calculations return { analyzed: true }; } }); // I/O-bound tasks const fetchExternalData = hy.task({ name: 'fetchExternalData', config: { queue: 'io-bound', maxRetries: 5 }, func: async (data) => { // External API calls return { fetched: true }; } }); // Memory-intensive tasks const processLargeFile = hy.task({ name: 'processLargeFile', config: { queue: { name: 'memory-intensive', concurrencyLimit: 1 // Prevent OOM } }, func: async (data) => { // Large file processing return { processed: true }; } });
// Email service queue const emailQueue = 'email-service'; const sendWelcomeEmail = hy.task({ name: 'sendWelcomeEmail', config: { queue: emailQueue }, func: async (data) => { /* ... */ } }); const sendPasswordReset = hy.task({ name: 'sendPasswordReset', config: { queue: emailQueue }, func: async (data) => { /* ... */ } }); // Payment processing queue const paymentQueue = 'payment-processing'; const chargeCard = hy.task({ name: 'chargeCard', config: { queue: paymentQueue, idempotencyKey: 'charge-123' // Prevent duplicate charges }, func: async (data) => { /* ... */ } }); const refundPayment = hy.task({ name: 'refundPayment', config: { queue: paymentQueue }, func: async (data) => { /* ... */ } });
# Email worker (high concurrency) hyrex run-worker hyrex-app.ts 10 --queue emails # Payment worker (limited concurrency for safety) hyrex run-worker hyrex-app.ts 2 --queue payment-processing # Background jobs worker hyrex run-worker hyrex-app.ts 5 --queue "batch-*"
# CPU-optimized instance hyrex run-worker hyrex-app.ts 8 --queue cpu-intensive # Memory-optimized instance hyrex run-worker hyrex-app.ts 2 --queue memory-intensive # GPU instance hyrex run-worker hyrex-app.ts 1 --queue gpu-processing
// Good config: { queue: 'email-notifications' } config: { queue: 'payment-processing' } // Avoid config: { queue: 'q1' } config: { queue: 'misc' }
// Limit concurrent database writes config: { queue: { name: 'database-writes', concurrencyLimit: 5 // Prevent connection exhaustion } }
# Separate workers for different concerns hyrex run-worker app.ts 10 --queue "user-facing-*" hyrex run-worker app.ts 5 --queue "background-*" hyrex run-worker app.ts 1 --queue "scheduled-reports"
// Tasks can go to region-specific queues const regionalTask = hy.task({ name: 'processRegional', config: { queue: `process-${region}` }, func: async (data) => { /* ... */ } }); // Workers can handle multiple regions // hyrex run-worker app.ts --queue "process-*"
Was this page helpful?