|
| 1 | +import { KV, Kvm } from '@nats-io/kv' |
| 2 | +import { FlowJob } from './flowJob' |
| 3 | +import { Job } from './job' |
| 4 | +import { Queue } from './queue' |
| 5 | +import { ChildToParentsKVValue, DependenciesKVValue } from './types' |
| 6 | + |
| 7 | +export class FlowQueue extends Queue { |
| 8 | + private parentChildrenStore: KV | null = null |
| 9 | + private childParentsStore: KV | null = null |
| 10 | + |
| 11 | + public override async setup(): Promise<void> { |
| 12 | + try { |
| 13 | + await super.setup() |
| 14 | + const kvm = await new Kvm(this.connection) |
| 15 | + this.parentChildrenStore = await kvm.create(`${this.name}_parent_id`) |
| 16 | + this.childParentsStore = await kvm.create(`${this.name}_parents`) |
| 17 | + } catch (e) { |
| 18 | + console.error(`Error connecting to JetStream: ${e}`) |
| 19 | + throw e |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public async addFlowJob(tree: FlowJob, priority: number = 1): Promise<void> { |
| 24 | + const deepestJobs = await this.traverseJobTree(tree) |
| 25 | + await this.addJobs(deepestJobs, priority) |
| 26 | + } |
| 27 | + |
| 28 | + private async traverseJobTree( |
| 29 | + node: FlowJob, |
| 30 | + parentId: string | null = null, |
| 31 | + ): Promise<Job[]> { |
| 32 | + const currentJob = node.job |
| 33 | + if (parentId) { |
| 34 | + currentJob.meta.parentId = parentId |
| 35 | + } |
| 36 | + |
| 37 | + const children = node.children || [] |
| 38 | + if (children.length === 0) { |
| 39 | + return [currentJob] |
| 40 | + } |
| 41 | + |
| 42 | + const newChildrenForCurrentJobCount: number = |
| 43 | + await this.updateChildDependencies({ |
| 44 | + parentJob: currentJob, |
| 45 | + childJobs: children.map((child) => child.job), |
| 46 | + }) |
| 47 | + |
| 48 | + await this.updateParentDependecies({ |
| 49 | + parentJob: currentJob, |
| 50 | + newChildrentCount: newChildrenForCurrentJobCount, |
| 51 | + }) |
| 52 | + |
| 53 | + const deepestJobs: Job[] = [] |
| 54 | + for (const child of children) { |
| 55 | + const traverseResult = await this.traverseJobTree(child, currentJob.id) |
| 56 | + deepestJobs.push(...traverseResult) |
| 57 | + } |
| 58 | + |
| 59 | + return deepestJobs |
| 60 | + } |
| 61 | + |
| 62 | + private async updateChildDependencies({ |
| 63 | + parentJob, |
| 64 | + childJobs, |
| 65 | + }: { |
| 66 | + parentJob: Job |
| 67 | + childJobs: Job[] |
| 68 | + }) { |
| 69 | + let newChildrenForCurrentJobCount: number = 0 |
| 70 | + for (const childJob of childJobs) { |
| 71 | + const childParentsKVEntry = await this.childParentsStore!.get(childJob.id) |
| 72 | + if (!childParentsKVEntry) { |
| 73 | + await this.childParentsStore!.put( |
| 74 | + childJob.id, |
| 75 | + JSON.stringify({ |
| 76 | + parentIds: [parentJob.id], |
| 77 | + }), |
| 78 | + ) |
| 79 | + newChildrenForCurrentJobCount++ |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + const parentsInfo: ChildToParentsKVValue = childParentsKVEntry.json() |
| 84 | + |
| 85 | + if (parentsInfo.parentIds.includes(parentJob.id)) continue |
| 86 | + |
| 87 | + parentsInfo.parentIds.push(parentJob.id) |
| 88 | + await this.childParentsStore!.put( |
| 89 | + childJob.id, |
| 90 | + JSON.stringify(parentsInfo), |
| 91 | + { |
| 92 | + previousSeq: childParentsKVEntry.revision, |
| 93 | + }, |
| 94 | + ) |
| 95 | + newChildrenForCurrentJobCount++ |
| 96 | + } |
| 97 | + |
| 98 | + return newChildrenForCurrentJobCount |
| 99 | + } |
| 100 | + |
| 101 | + private async updateParentDependecies({ |
| 102 | + parentJob, |
| 103 | + newChildrentCount, |
| 104 | + }: { |
| 105 | + parentJob: Job |
| 106 | + newChildrentCount: number |
| 107 | + }) { |
| 108 | + const existingParentDependencies = await this.parentChildrenStore!.get( |
| 109 | + parentJob.id, |
| 110 | + ) |
| 111 | + if (!existingParentDependencies) { |
| 112 | + await this.parentChildrenStore!.put( |
| 113 | + parentJob.id, |
| 114 | + JSON.stringify({ |
| 115 | + ...parentJob, |
| 116 | + childrenCount: newChildrentCount, |
| 117 | + }), |
| 118 | + ) |
| 119 | + } else { |
| 120 | + const parentDependencies: DependenciesKVValue = |
| 121 | + existingParentDependencies.json() |
| 122 | + parentDependencies.childrenCount += newChildrentCount |
| 123 | + await this.parentChildrenStore!.put( |
| 124 | + parentJob.id, |
| 125 | + JSON.stringify(parentDependencies), |
| 126 | + { |
| 127 | + previousSeq: existingParentDependencies.revision, |
| 128 | + }, |
| 129 | + ) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments