@highstate/backend 0.7.2 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{index.mjs → index.js} +1254 -915
- package/dist/library/source-resolution-worker.js +55 -0
- package/dist/library/worker/main.js +207 -0
- package/dist/{terminal-CqIsctlZ.mjs → library-BW5oPM7V.js} +210 -87
- package/dist/shared/index.js +6 -0
- package/dist/utils-ByadNcv4.js +102 -0
- package/package.json +14 -18
- package/src/common/index.ts +3 -0
- package/src/common/local.ts +22 -0
- package/src/common/pulumi.ts +230 -0
- package/src/common/utils.ts +137 -0
- package/src/config.ts +40 -0
- package/src/index.ts +6 -0
- package/src/library/abstractions.ts +83 -0
- package/src/library/factory.ts +20 -0
- package/src/library/index.ts +2 -0
- package/src/library/local.ts +404 -0
- package/src/library/source-resolution-worker.ts +96 -0
- package/src/library/worker/evaluator.ts +119 -0
- package/src/library/worker/loader.ts +93 -0
- package/src/library/worker/main.ts +82 -0
- package/src/library/worker/protocol.ts +38 -0
- package/src/orchestrator/index.ts +1 -0
- package/src/orchestrator/manager.ts +165 -0
- package/src/orchestrator/operation-workset.ts +483 -0
- package/src/orchestrator/operation.ts +647 -0
- package/src/preferences/shared.ts +1 -0
- package/src/project/abstractions.ts +89 -0
- package/src/project/factory.ts +11 -0
- package/src/project/index.ts +4 -0
- package/src/project/local.ts +412 -0
- package/src/project/lock.ts +39 -0
- package/src/project/manager.ts +374 -0
- package/src/runner/abstractions.ts +146 -0
- package/src/runner/factory.ts +22 -0
- package/src/runner/index.ts +2 -0
- package/src/runner/local.ts +698 -0
- package/src/secret/abstractions.ts +59 -0
- package/src/secret/factory.ts +22 -0
- package/src/secret/index.ts +2 -0
- package/src/secret/local.ts +152 -0
- package/src/services.ts +133 -0
- package/src/shared/index.ts +10 -0
- package/src/shared/library.ts +77 -0
- package/src/shared/operation.ts +85 -0
- package/src/shared/project.ts +62 -0
- package/src/shared/resolvers/graph-resolver.ts +111 -0
- package/src/shared/resolvers/input-hash.ts +77 -0
- package/src/shared/resolvers/input.ts +314 -0
- package/src/shared/resolvers/registry.ts +10 -0
- package/src/shared/resolvers/validation.ts +94 -0
- package/src/shared/state.ts +262 -0
- package/src/shared/terminal.ts +13 -0
- package/src/state/abstractions.ts +222 -0
- package/src/state/factory.ts +22 -0
- package/src/state/index.ts +3 -0
- package/src/state/local.ts +605 -0
- package/src/state/manager.ts +33 -0
- package/src/terminal/docker.ts +90 -0
- package/src/terminal/factory.ts +20 -0
- package/src/terminal/index.ts +3 -0
- package/src/terminal/manager.ts +330 -0
- package/src/terminal/run.sh.ts +37 -0
- package/src/terminal/shared.ts +50 -0
- package/src/workspace/abstractions.ts +41 -0
- package/src/workspace/factory.ts +14 -0
- package/src/workspace/index.ts +2 -0
- package/src/workspace/local.ts +54 -0
- package/dist/index.d.ts +0 -760
- package/dist/library/worker/main.mjs +0 -164
- package/dist/runner/source-resolution-worker.mjs +0 -22
- package/dist/shared/index.d.ts +0 -85
- package/dist/shared/index.mjs +0 -54
- package/dist/terminal-Cm2WqcyB.d.ts +0 -1589
@@ -0,0 +1,82 @@
|
|
1
|
+
import type { WorkerData, WorkerRequest, WorkerResponse } from "./protocol"
|
2
|
+
import { parentPort, workerData } from "node:worker_threads"
|
3
|
+
import { createJiti, type Jiti } from "jiti"
|
4
|
+
import { pino } from "pino"
|
5
|
+
import { mapValues } from "remeda"
|
6
|
+
import { errorToString } from "../../common"
|
7
|
+
import { loadLibrary, type Library } from "./loader"
|
8
|
+
import { evaluateInstances, evaluateModules } from "./evaluator"
|
9
|
+
|
10
|
+
const data = workerData as WorkerData
|
11
|
+
|
12
|
+
const logger = pino({ name: "library-worker", level: data.logLevel })
|
13
|
+
let jiti: Jiti
|
14
|
+
let library: Library
|
15
|
+
|
16
|
+
try {
|
17
|
+
logger.info("worker started")
|
18
|
+
logger.trace({ data }, "worker data")
|
19
|
+
|
20
|
+
jiti = createJiti(import.meta.filename)
|
21
|
+
logger.debug({ filename: import.meta.filename }, "jiti created")
|
22
|
+
|
23
|
+
library = await loadLibrary(jiti, logger, data.modulePaths)
|
24
|
+
|
25
|
+
parentPort!.postMessage({
|
26
|
+
type: "library",
|
27
|
+
library: {
|
28
|
+
components: mapValues(library.components, component => component.model),
|
29
|
+
entities: library.entities,
|
30
|
+
},
|
31
|
+
} satisfies WorkerResponse)
|
32
|
+
|
33
|
+
logger.info("library loaded and sent")
|
34
|
+
} catch (error) {
|
35
|
+
logger.error({ error }, "failed to load library")
|
36
|
+
|
37
|
+
parentPort!.postMessage({
|
38
|
+
type: "error",
|
39
|
+
error: errorToString(error),
|
40
|
+
})
|
41
|
+
}
|
42
|
+
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
44
|
+
parentPort!.on("message", async message => {
|
45
|
+
try {
|
46
|
+
const request = message as WorkerRequest
|
47
|
+
|
48
|
+
switch (request.type) {
|
49
|
+
case "evaluate-composite-instances": {
|
50
|
+
const results = evaluateInstances(
|
51
|
+
logger,
|
52
|
+
library,
|
53
|
+
request.allInstances,
|
54
|
+
request.resolvedInputs,
|
55
|
+
request.instanceIds,
|
56
|
+
)
|
57
|
+
|
58
|
+
parentPort!.postMessage({
|
59
|
+
type: "instance-evaluation-results",
|
60
|
+
results,
|
61
|
+
} satisfies WorkerResponse)
|
62
|
+
break
|
63
|
+
}
|
64
|
+
case "evaluate-modules": {
|
65
|
+
const result = await evaluateModules(jiti, logger, request.modulePaths)
|
66
|
+
|
67
|
+
parentPort!.postMessage({
|
68
|
+
type: "module-evaluation-result",
|
69
|
+
result,
|
70
|
+
} satisfies WorkerResponse)
|
71
|
+
break
|
72
|
+
}
|
73
|
+
}
|
74
|
+
} catch (error) {
|
75
|
+
logger.error({ error }, "failed to evaluate")
|
76
|
+
|
77
|
+
parentPort!.postMessage({
|
78
|
+
type: "error",
|
79
|
+
error: errorToString(error),
|
80
|
+
} satisfies WorkerResponse)
|
81
|
+
}
|
82
|
+
})
|
@@ -0,0 +1,38 @@
|
|
1
|
+
import type { InstanceModel } from "@highstate/contract"
|
2
|
+
import type { InstanceEvaluationResult, ModuleEvaluationResult } from "../abstractions"
|
3
|
+
import type { LibraryModel, ResolvedInstanceInput } from "../../shared"
|
4
|
+
|
5
|
+
export type WorkerData = {
|
6
|
+
modulePaths: string[]
|
7
|
+
logLevel?: string
|
8
|
+
}
|
9
|
+
|
10
|
+
export type WorkerRequest =
|
11
|
+
| {
|
12
|
+
type: "evaluate-composite-instances"
|
13
|
+
allInstances: InstanceModel[]
|
14
|
+
resolvedInputs: Record<string, Record<string, ResolvedInstanceInput[]>>
|
15
|
+
instanceIds: string[]
|
16
|
+
}
|
17
|
+
| {
|
18
|
+
type: "evaluate-modules"
|
19
|
+
modulePaths: string[]
|
20
|
+
}
|
21
|
+
|
22
|
+
export type WorkerResponse =
|
23
|
+
| {
|
24
|
+
type: "library"
|
25
|
+
library: LibraryModel
|
26
|
+
}
|
27
|
+
| {
|
28
|
+
type: "instance-evaluation-results"
|
29
|
+
results: InstanceEvaluationResult[]
|
30
|
+
}
|
31
|
+
| {
|
32
|
+
type: "module-evaluation-result"
|
33
|
+
result: ModuleEvaluationResult
|
34
|
+
}
|
35
|
+
| {
|
36
|
+
type: "error"
|
37
|
+
error: string
|
38
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./manager"
|
@@ -0,0 +1,165 @@
|
|
1
|
+
import type { LibraryBackend } from "../library"
|
2
|
+
import type { ProjectBackend, ProjectLockManager } from "../project"
|
3
|
+
import type { RunnerBackend } from "../runner"
|
4
|
+
import type { SecretBackend } from "../secret"
|
5
|
+
import type { StateBackend, StateManager } from "../state"
|
6
|
+
import type { Logger } from "pino"
|
7
|
+
import { EventEmitter, on } from "node:events"
|
8
|
+
import { uuidv7 } from "uuidv7"
|
9
|
+
import { type ProjectOperation, type ProjectOperationRequest } from "../shared"
|
10
|
+
import { RuntimeOperation } from "./operation"
|
11
|
+
|
12
|
+
export type OperationEvents = Record<string, [ProjectOperation]>
|
13
|
+
export type InstanceLogsEvents = Record<string, [string]>
|
14
|
+
|
15
|
+
export class OperationManager {
|
16
|
+
constructor(
|
17
|
+
private readonly runnerBackend: RunnerBackend,
|
18
|
+
private readonly stateBackend: StateBackend,
|
19
|
+
private readonly libraryBackend: LibraryBackend,
|
20
|
+
private readonly projectBackend: ProjectBackend,
|
21
|
+
private readonly secretBackend: SecretBackend,
|
22
|
+
private readonly projectLockManager: ProjectLockManager,
|
23
|
+
private readonly stateManager: StateManager,
|
24
|
+
private readonly logger: Logger,
|
25
|
+
) {}
|
26
|
+
|
27
|
+
private readonly operationEE = new EventEmitter<OperationEvents>()
|
28
|
+
private readonly instanceLogsEE = new EventEmitter<InstanceLogsEvents>()
|
29
|
+
|
30
|
+
private readonly runtimeOperations = new Map<string, RuntimeOperation>()
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Watches for all project operations in the project.
|
34
|
+
*
|
35
|
+
* @param projectId The project ID to watch.
|
36
|
+
* @param signal The signal to abort the operation.
|
37
|
+
*/
|
38
|
+
public async *watchOperations(
|
39
|
+
projectId: string,
|
40
|
+
signal?: AbortSignal,
|
41
|
+
): AsyncIterable<ProjectOperation> {
|
42
|
+
for await (const [operation] of on(this.operationEE, projectId, { signal })) {
|
43
|
+
yield operation
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
* Watches for logs of the instance in the operation.
|
49
|
+
*
|
50
|
+
* @param operationId The operation ID to watch.
|
51
|
+
* @param instanceId The instance ID to watch.
|
52
|
+
* @param signal The signal to abort the operation.
|
53
|
+
*/
|
54
|
+
async *watchInstanceLogs(
|
55
|
+
operationId: string,
|
56
|
+
instanceId: string,
|
57
|
+
signal?: AbortSignal,
|
58
|
+
): AsyncIterable<string> {
|
59
|
+
const eventKey = `${operationId}/${instanceId}`
|
60
|
+
for await (const [log] of on(this.instanceLogsEE, eventKey, { signal })) {
|
61
|
+
yield log
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
/**
|
66
|
+
* Launches the project operation.
|
67
|
+
*
|
68
|
+
* @param request The operation request to launch.
|
69
|
+
*/
|
70
|
+
async launch(request: ProjectOperationRequest): Promise<ProjectOperation> {
|
71
|
+
const operation: ProjectOperation = {
|
72
|
+
id: uuidv7(),
|
73
|
+
projectId: request.projectId,
|
74
|
+
type: request.type,
|
75
|
+
instanceIds: request.instanceIds,
|
76
|
+
affectedInstanceIds: request.instanceIds,
|
77
|
+
status: "pending",
|
78
|
+
options: {
|
79
|
+
forceUpdateDependencies: request.options?.forceUpdateDependencies ?? false,
|
80
|
+
destroyDependentInstances: request.options?.destroyDependentInstances ?? true,
|
81
|
+
invokeDestroyTriggers: request.options?.invokeDestroyTriggers ?? false,
|
82
|
+
deleteUnreachableResources: request.options?.deleteUnreachableResources ?? false,
|
83
|
+
refresh: request.options?.refresh ?? false,
|
84
|
+
},
|
85
|
+
error: null,
|
86
|
+
startedAt: Date.now(),
|
87
|
+
completedAt: null,
|
88
|
+
}
|
89
|
+
|
90
|
+
this.logger.info({ operation }, "launching operation")
|
91
|
+
|
92
|
+
await this.stateBackend.putOperation(operation)
|
93
|
+
|
94
|
+
this.startOperation(operation)
|
95
|
+
|
96
|
+
return operation
|
97
|
+
}
|
98
|
+
|
99
|
+
/**
|
100
|
+
* Cancels the current operation.
|
101
|
+
* Does nothing if no operation is running.
|
102
|
+
*/
|
103
|
+
cancel(operationId: string): void {
|
104
|
+
const runtimeOperation = this.runtimeOperations.get(operationId)
|
105
|
+
if (runtimeOperation) {
|
106
|
+
runtimeOperation.cancel()
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
private startOperation(operation: ProjectOperation): void {
|
111
|
+
const runtimeOperation = new RuntimeOperation(
|
112
|
+
operation,
|
113
|
+
this.runnerBackend,
|
114
|
+
this.stateBackend,
|
115
|
+
this.libraryBackend,
|
116
|
+
this.projectBackend,
|
117
|
+
this.secretBackend,
|
118
|
+
this.projectLockManager.getLock(operation.projectId),
|
119
|
+
this.stateManager,
|
120
|
+
this.operationEE,
|
121
|
+
this.instanceLogsEE,
|
122
|
+
this.logger.child({ service: "RuntimeOperation", operationId: operation.id }),
|
123
|
+
)
|
124
|
+
|
125
|
+
this.runtimeOperations.set(operation.id, runtimeOperation)
|
126
|
+
void runtimeOperation.operateSafe().finally(() => this.runtimeOperations.delete(operation.id))
|
127
|
+
}
|
128
|
+
|
129
|
+
static async create(
|
130
|
+
runnerBackend: RunnerBackend,
|
131
|
+
stateBackend: StateBackend,
|
132
|
+
libraryBackend: LibraryBackend,
|
133
|
+
projectBackend: ProjectBackend,
|
134
|
+
secretBackend: SecretBackend,
|
135
|
+
projectLockManager: ProjectLockManager,
|
136
|
+
stateManager: StateManager,
|
137
|
+
logger: Logger,
|
138
|
+
) {
|
139
|
+
const operator = new OperationManager(
|
140
|
+
runnerBackend,
|
141
|
+
stateBackend,
|
142
|
+
libraryBackend,
|
143
|
+
projectBackend,
|
144
|
+
secretBackend,
|
145
|
+
projectLockManager,
|
146
|
+
stateManager,
|
147
|
+
logger.child({ service: "OperationManager" }),
|
148
|
+
)
|
149
|
+
|
150
|
+
// relaunch all active operations
|
151
|
+
const activeOperations = await stateBackend.getActiveOperations()
|
152
|
+
|
153
|
+
for (const operation of activeOperations) {
|
154
|
+
logger.info({ msg: "relaunching operation", operationId: operation.id })
|
155
|
+
|
156
|
+
// operator.startOperation(operation)
|
157
|
+
|
158
|
+
// cancel the operation for now
|
159
|
+
operation.status = "cancelled"
|
160
|
+
await stateBackend.putOperation(operation)
|
161
|
+
}
|
162
|
+
|
163
|
+
return operator
|
164
|
+
}
|
165
|
+
}
|