* Enables Kubernetes Master-Worker mode. * * This method configures the application for distributed task processing: * - If --master flag is passed: Acts as the master node (task distribution) * - If --worker flag is passed: Acts as a worker node (task execution) * - If nei
({
masterEndpoint,
taskTimeout = DEFAULT_TASK_TIMEOUT
}: {
masterEndpoint: string,
taskTimeout?: number | Record<string, number>
})
| 802 | * }); |
| 803 | */ |
| 804 | static async enableKubernetes({ |
| 805 | masterEndpoint, |
| 806 | taskTimeout = DEFAULT_TASK_TIMEOUT |
| 807 | }: { |
| 808 | masterEndpoint: string, |
| 809 | taskTimeout?: number | Record<string, number> |
| 810 | }): Promise<void> { |
| 811 | ApiConfig.enableApi() |
| 812 | if (masterEndpoint) { |
| 813 | masterEndpoint = removeAfterFirstSlash(masterEndpoint); |
| 814 | } |
| 815 | // Validate masterEndpoint |
| 816 | if (!masterEndpoint || !isValidUrl(masterEndpoint)) { |
| 817 | throw new Error(`Invalid masterEndpoint: "${masterEndpoint}". Must be a valid URL (e.g., "https://my-app.my-tunnel.app", "https://my-app.cloudflare-tunnel.app", "https://my-app.ngrok-free.app")`); |
| 818 | } |
| 819 | |
| 820 | // Validate taskTimeout |
| 821 | if (typeof taskTimeout === 'number') { |
| 822 | if (taskTimeout < 60) { |
| 823 | console.warn(`[K8s] Warning: taskTimeout (${taskTimeout}s) is less than 60 seconds. This may cause premature task reassignment.`); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | |
| 828 | // Parse command line flags |
| 829 | |
| 830 | if (isMaster) { |
| 831 | // Validate visibility timeout against scrapers if it's an object |
| 832 | if (typeof taskTimeout === 'object') { |
| 833 | Server.validateAgainstLimit(taskTimeout, 'task timeout'); |
| 834 | } |
| 835 | |
| 836 | // Set up master executor |
| 837 | // @ts-ignore |
| 838 | global.executor = new MasterExecutor(taskTimeout); |
| 839 | this.nodeRole = 'master'; |
| 840 | console.log("[K8s] Starting as Kubernetes MASTER node 👑"); |
| 841 | |
| 842 | // @ts-ignore |
| 843 | global.master = true |
| 844 | // Test self-connectivity (master calls itself) |
| 845 | // @ts-ignore |
| 846 | global.checkMasterHealth = () =>{ |
| 847 | setTimeout(async () => { |
| 848 | const result = await checkMasterHealth(masterEndpoint); |
| 849 | if (result) { |
| 850 | console.log("[K8s] Master health check passed ✓"); |
| 851 | } else { |
| 852 | console.log("[K8s] Master health check failed ✗"); |
| 853 | } |
| 854 | }, 2000); // Delay to allow server to start |
| 855 | } |
| 856 | } else if (isWorker) { |
| 857 | // Set up worker executor |
| 858 | // @ts-ignore |
| 859 | global.executor = new WorkerExecutor(masterEndpoint); |
| 860 | this.nodeRole = 'worker'; |
| 861 | console.log("[K8s] Starting as Kubernetes WORKER node 🔧"); |
nothing calls this directly
no test coverage detected