|
| 1 | +// This file contains the TypeScript port of the Python workflow for managing cluster updates and signals |
| 2 | + |
| 3 | +import { proxyActivities, defineSignal, defineQuery, setHandler, condition, sleep, defineWorkflow } from '@temporalio/workflow'; |
| 4 | +import type { AllocateNodesToJobInput, DeallocateNodesForJobInput, FindBadNodesInput } from './interfaces'; |
| 5 | + |
| 6 | +// Define signals |
| 7 | +const startClusterSignal = defineSignal('startCluster'); |
| 8 | +const shutdownClusterSignal = defineSignal('shutdownCluster'); |
| 9 | +const allocateNodesToJobSignal = defineSignal<[AllocateNodesToJobInput]>('allocateNodesToJob'); |
| 10 | +const deallocateNodesForJobSignal = defineSignal<[DeallocateNodesForJobInput]>('deallocateNodesForJob'); |
| 11 | + |
| 12 | +// Define queries |
| 13 | +const getClusterStatusQuery = defineQuery<{}>('getClusterStatus'); |
| 14 | + |
| 15 | +// Define activities |
| 16 | +const { allocateNodesToJob, deallocateNodesForJob, findBadNodes } = proxyActivities<{ |
| 17 | + allocateNodesToJob(input: AllocateNodesToJobInput): Promise<void>; |
| 18 | + deallocateNodesForJob(input: DeallocateNodesForJobInput): Promise<void>; |
| 19 | + findBadNodes(input: FindBadNodesInput): Promise<string[]>; |
| 20 | +}>({ |
| 21 | + startToCloseTimeout: '1 minute', |
| 22 | +}); |
| 23 | + |
| 24 | +// Define workflow interface |
| 25 | +export interface ClusterManagerWorkflow { |
| 26 | + run(input: ClusterManagerWorkflowInput): Promise<ClusterManagerWorkflowResult>; |
| 27 | +} |
| 28 | + |
| 29 | +// Define workflow input and result types |
| 30 | +export interface ClusterManagerWorkflowInput { |
| 31 | + testContinueAsNew: boolean; |
| 32 | +} |
| 33 | + |
| 34 | +export interface ClusterManagerWorkflowResult { |
| 35 | + maxAssignedNodes: number; |
| 36 | + numCurrentlyAssignedNodes: number; |
| 37 | + numBadNodes: number; |
| 38 | +} |
| 39 | + |
| 40 | +// Workflow implementation |
| 41 | +export const clusterManagerWorkflow: ClusterManagerWorkflow = defineWorkflow({ |
| 42 | + async run(input: ClusterManagerWorkflowInput) { |
| 43 | + let state = { |
| 44 | + clusterStarted: false, |
| 45 | + clusterShutdown: false, |
| 46 | + nodes: {} as Record<string, string | null>, |
| 47 | + jobsAdded: new Set<string>(), |
| 48 | + maxAssignedNodes: 0, |
| 49 | + }; |
| 50 | + |
| 51 | + // Signal handlers |
| 52 | + setHandler(startClusterSignal, () => { |
| 53 | + state.clusterStarted = true; |
| 54 | + for (let i = 0; i < 25; i++) { |
| 55 | + state.nodes[i.toString()] = null; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + setHandler(shutdownClusterSignal, () => { |
| 60 | + state.clusterShutdown = true; |
| 61 | + }); |
| 62 | + |
| 63 | + setHandler(allocateNodesToJobSignal, async (input: AllocateNodesToJobInput) => { |
| 64 | + if (!state.clusterStarted || state.clusterShutdown) { |
| 65 | + throw new Error('Cluster is not in a valid state for node allocation'); |
| 66 | + } |
| 67 | + // Allocate nodes to job logic |
| 68 | + }); |
| 69 | + |
| 70 | + setHandler(deallocateNodesForJobSignal, async (input: DeallocateNodesForJobInput) => { |
| 71 | + if (!state.clusterStarted || state.clusterShutdown) { |
| 72 | + throw new Error('Cluster is not in a valid state for node deallocation'); |
| 73 | + } |
| 74 | + // Deallocate nodes from job logic |
| 75 | + }); |
| 76 | + |
| 77 | + // Query handler |
| 78 | + setHandler(getClusterStatusQuery, () => { |
| 79 | + return { |
| 80 | + clusterStarted: state.clusterStarted, |
| 81 | + clusterShutdown: state.clusterShutdown, |
| 82 | + numNodes: Object.keys(state.nodes).length, |
| 83 | + numAssignedNodes: Object.values(state.nodes).filter(n => n !== null).length, |
| 84 | + }; |
| 85 | + }); |
| 86 | + |
| 87 | + // Main workflow logic |
| 88 | + await condition(() => state.clusterStarted, 'Waiting for cluster to start'); |
| 89 | + // Perform operations while cluster is active |
| 90 | + while (!state.clusterShutdown) { |
| 91 | + // Example: perform periodic health checks |
| 92 | + await sleep(60000); // Sleep for 60 seconds |
| 93 | + } |
| 94 | + |
| 95 | + // Return workflow result |
| 96 | + return { |
| 97 | + maxAssignedNodes: state.maxAssignedNodes, |
| 98 | + numCurrentlyAssignedNodes: Object.values(state.nodes).filter(n => n !== null).length, |
| 99 | + numBadNodes: Object.values(state.nodes).filter(n => n === 'BAD').length, |
| 100 | + }; |
| 101 | + }, |
| 102 | +}); |
0 commit comments