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