|
| 1 | +# PLAN: pgflow workers start CLI Command |
| 2 | + |
| 3 | +**Created**: 2025-11-28 |
| 4 | +**Status**: Future Work |
| 5 | +**Related**: PLAN_auth-verification.md |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Goal |
| 10 | + |
| 11 | +Provide a CLI command to start workers with proper authentication, eliminating the need for manual curl commands. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Command Design |
| 16 | + |
| 17 | +```bash |
| 18 | +# Uses SUPABASE_SERVICE_ROLE_KEY from env or .env |
| 19 | +pgflow workers start greet-user-worker |
| 20 | + |
| 21 | +# Or explicit key |
| 22 | +pgflow workers start greet-user-worker --secret-key <key> |
| 23 | + |
| 24 | +# Multiple workers |
| 25 | +pgflow workers start greet-user-worker payment-worker |
| 26 | +``` |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Behavior |
| 31 | + |
| 32 | +### Key Resolution |
| 33 | + |
| 34 | +1. Check `--secret-key` flag |
| 35 | +2. Check `SUPABASE_SERVICE_ROLE_KEY` env var |
| 36 | +3. Check `.env` file in current directory |
| 37 | +4. Check `supabase/.env` file |
| 38 | +5. Error if no key found |
| 39 | + |
| 40 | +### Request Handling |
| 41 | + |
| 42 | +- Call worker endpoint with proper `apikey` header |
| 43 | +- Handle HTTP responses and errors |
| 44 | +- Display worker status/logs |
| 45 | +- Handle reconnection on disconnect |
| 46 | + |
| 47 | +### URL Resolution |
| 48 | + |
| 49 | +- Default: `http://localhost:54321/functions/v1/<worker-name>` |
| 50 | +- Support `--url` flag for custom endpoints |
| 51 | +- Support `SUPABASE_URL` env var for hosted |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Implementation |
| 56 | + |
| 57 | +### Files to Create/Modify |
| 58 | + |
| 59 | +- `pkgs/cli/src/commands/workers/start.ts` - New command |
| 60 | +- `pkgs/cli/src/commands/workers/index.ts` - Command group |
| 61 | + |
| 62 | +### Command Structure |
| 63 | + |
| 64 | +```typescript |
| 65 | +import { Command } from 'commander'; |
| 66 | + |
| 67 | +export const workersStartCommand = new Command('start') |
| 68 | + .description('Start a worker to process tasks') |
| 69 | + .argument('<worker...>', 'Worker function name(s)') |
| 70 | + .option('--secret-key <key>', 'Service role / secret key') |
| 71 | + .option('--url <url>', 'Supabase functions URL') |
| 72 | + .action(async (workers, options) => { |
| 73 | + const secretKey = resolveSecretKey(options); |
| 74 | + |
| 75 | + for (const worker of workers) { |
| 76 | + await startWorker(worker, secretKey, options.url); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | +async function startWorker(name: string, secretKey: string, baseUrl?: string) { |
| 81 | + const url = `${baseUrl || getDefaultUrl()}/functions/v1/${name}`; |
| 82 | + |
| 83 | + const response = await fetch(url, { |
| 84 | + headers: { |
| 85 | + 'apikey': secretKey, |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + // Handle response, reconnection, etc. |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Key Resolution Function |
| 94 | + |
| 95 | +```typescript |
| 96 | +function resolveSecretKey(options: { secretKey?: string }): string { |
| 97 | + // 1. CLI flag |
| 98 | + if (options.secretKey) return options.secretKey; |
| 99 | + |
| 100 | + // 2. Env var |
| 101 | + if (process.env.SUPABASE_SERVICE_ROLE_KEY) { |
| 102 | + return process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 103 | + } |
| 104 | + |
| 105 | + // 3. .env file |
| 106 | + const envPath = findEnvFile(); |
| 107 | + if (envPath) { |
| 108 | + const env = dotenv.parse(fs.readFileSync(envPath)); |
| 109 | + if (env.SUPABASE_SERVICE_ROLE_KEY) { |
| 110 | + return env.SUPABASE_SERVICE_ROLE_KEY; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + throw new Error('No secret key found. Provide --secret-key or set SUPABASE_SERVICE_ROLE_KEY'); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## UX Considerations |
| 121 | + |
| 122 | +### Output Format |
| 123 | + |
| 124 | +``` |
| 125 | +$ pgflow workers start greet-user-worker |
| 126 | +
|
| 127 | +Starting worker: greet-user-worker |
| 128 | + URL: http://localhost:54321/functions/v1/greet-user-worker |
| 129 | + Auth: Using SUPABASE_SERVICE_ROLE_KEY from environment |
| 130 | +
|
| 131 | +Worker started successfully. Press Ctrl+C to stop. |
| 132 | +``` |
| 133 | + |
| 134 | +### Error Messages |
| 135 | + |
| 136 | +``` |
| 137 | +$ pgflow workers start greet-user-worker |
| 138 | +
|
| 139 | +Error: No secret key found. |
| 140 | +
|
| 141 | +To fix this, either: |
| 142 | + 1. Set SUPABASE_SERVICE_ROLE_KEY environment variable |
| 143 | + 2. Add SUPABASE_SERVICE_ROLE_KEY to your .env file |
| 144 | + 3. Use --secret-key flag: pgflow workers start greet-user-worker --secret-key <key> |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## When to Implement |
| 150 | + |
| 151 | +After: |
| 152 | +- Auth verification is implemented (PLAN_auth-verification.md) |
| 153 | +- Basic worker functionality is stable |
| 154 | +- User feedback indicates need for easier worker management |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## Future Enhancements |
| 159 | + |
| 160 | +- `pgflow workers list` - List available workers |
| 161 | +- `pgflow workers status` - Show running workers |
| 162 | +- `pgflow workers stop` - Stop running workers |
| 163 | +- Watch mode for development |
| 164 | +- Multiple worker instances with `--concurrency` flag |
0 commit comments