-
Notifications
You must be signed in to change notification settings - Fork 85
refactor: split provider-inventory into 2 interfaces #3197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+153
−33
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // !!! WARNING: we need this file to ensure that providers and model schemas are loaded before the entry point. | ||
| // To preserve the order of app related side-effects, use dynamic imports. But ideally, just get rid of them. | ||
| import "reflect-metadata"; | ||
| import "@akashnetwork/env-loader"; | ||
|
|
||
| export async function bootstrapEntry<T>(loadEntry: () => Promise<T>): Promise<T> { | ||
| await import("./providers/index.ts"); | ||
| return await loadEntry(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { HttpLoggerInterceptor } from "@akashnetwork/logging/hono"; | ||
| import { createOtelLogger } from "@akashnetwork/logging/otel"; | ||
| import { otel } from "@hono/otel"; | ||
| import { Hono } from "hono"; | ||
| import { container } from "tsyringe"; | ||
|
|
||
| import { APP_CONFIG } from "@src/providers/app-config.provider"; | ||
| import { ProviderInventoryRepository } from "@src/repositories/provider-inventory/provider-inventory.repository"; | ||
| import { healthzRouter } from "@src/routes"; | ||
| import { DiscoverySchedulerService } from "@src/services/discovery-scheduler/discovery-scheduler.service"; | ||
| import { HonoErrorHandlerService } from "@src/services/hono-error-handler/hono-error-handler.service"; | ||
| import { startServer } from "@src/services/start-server/start-server"; | ||
| import type { AppEnv } from "@src/types/app-context"; | ||
|
|
||
| export async function bootstrap(): Promise<void> { | ||
| const app = new Hono<AppEnv>(); | ||
| app.use("*", otel({ captureRequestHeaders: ["baggage"] })); | ||
| app.use(container.resolve(HttpLoggerInterceptor).intercept()); | ||
| app.route("/", healthzRouter); | ||
| app.onError(container.resolve(HonoErrorHandlerService).handle); | ||
|
|
||
| const server = await startServer(app, createOtelLogger({ context: "PROVIDERS_SYNC" }), process, { | ||
| port: container.resolve(APP_CONFIG).PORT, | ||
| beforeStart: async () => { | ||
| await container.resolve(ProviderInventoryRepository).resetOnlineSince(); | ||
| } | ||
| }); | ||
|
|
||
| if (server) { | ||
| try { | ||
| container.resolve(DiscoverySchedulerService).start(); | ||
| } catch (error) { | ||
| server.close(); | ||
| throw error; | ||
| } | ||
| } | ||
|
stalniy marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
apps/provider-inventory/src/providers/stream-boostrap.provider.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,93 @@ | ||
| import { bootstrap } from "./index"; | ||
| import { parentPort, Worker } from "node:worker_threads"; | ||
|
|
||
| void bootstrap(); | ||
| import type { RawAppConfig } from "./providers/raw-app-config.provider"; | ||
| import { bootstrapEntry } from "./bootstrap-entry"; | ||
|
|
||
| const SUPPORTED_INTERFACES = ["rest", "providers-sync"]; | ||
|
|
||
| bootstrap(process.env as RawAppConfig); | ||
|
|
||
| async function bootstrap(rawAppConfig: RawAppConfig): Promise<void> { | ||
| const INTERFACE = rawAppConfig.INTERFACE || "all"; | ||
| const port = parseInt(rawAppConfig.PORT?.toString() || "3092", 10) || 3092; | ||
|
|
||
| if (INTERFACE === "all") { | ||
| const workers = SUPPORTED_INTERFACES.map((interfaceName, index) => bootstrapInWorker({ PORT: String(port + index), INTERFACE: interfaceName })); | ||
|
stalniy marked this conversation as resolved.
|
||
| await Promise.all(workers.map(w => w.ready)); | ||
|
|
||
| const forwardSignal = (signal: string) => { | ||
| workers.forEach(w => w.ref.postMessage(signal)); | ||
| }; | ||
| process.on("SIGTERM", () => forwardSignal("SIGTERM")); | ||
| process.on("SIGINT", () => forwardSignal("SIGINT")); | ||
|
|
||
| await Promise.all(workers.map(w => w.exited)); | ||
| return; | ||
| } | ||
|
|
||
| if (parentPort) { | ||
| parentPort.on("message", (code: string) => { | ||
| if (code === "SIGTERM" || code === "SIGINT") { | ||
| parentPort?.unref(); | ||
| process.emit(code, code); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| let appModule: { bootstrap: () => Promise<void> }; | ||
| switch (INTERFACE) { | ||
| case "rest": | ||
| appModule = await bootstrapEntry(() => import("./rest-app.ts")); | ||
| break; | ||
| case "providers-sync": | ||
| appModule = await bootstrapEntry(() => import("./providers-sync-app.ts")); | ||
| break; | ||
| default: | ||
| throw new Error(`Received invalid interface: ${INTERFACE}. Valid values: ${SUPPORTED_INTERFACES.join(", ")}`); | ||
| } | ||
|
|
||
| await appModule.bootstrap(); | ||
| parentPort?.postMessage("ready"); | ||
| } | ||
|
|
||
| function bootstrapInWorker({ PORT, INTERFACE }: RawAppConfig) { | ||
| const ref = new Worker(__filename, { | ||
| env: { | ||
| ...process.env, | ||
| PORT: String(PORT), | ||
| INTERFACE: String(INTERFACE) | ||
| } | ||
| }); | ||
|
|
||
| const ready = new Promise<void>((resolve, reject) => { | ||
| const onMessage = (m: unknown) => { | ||
| if (m === "ready") { | ||
| cleanup(); | ||
| resolve(); | ||
| } | ||
| }; | ||
| const onError = (err: Error) => { | ||
| cleanup(); | ||
| reject(err); | ||
| }; | ||
| const onExit = (code: number) => { | ||
| cleanup(); | ||
| reject(new Error(`[${INTERFACE}] exited with code ${code} before ready`)); | ||
| }; | ||
| const cleanup = () => { | ||
| ref.off("message", onMessage); | ||
| ref.off("error", onError); | ||
| ref.off("exit", onExit); | ||
| }; | ||
| ref.on("message", onMessage); | ||
| ref.once("error", onError); | ||
| ref.once("exit", onExit); | ||
| }); | ||
|
|
||
| const exited = new Promise<void>(resolve => { | ||
| ref.once("exit", resolve); | ||
| ref.once("error", resolve); | ||
| }); | ||
|
stalniy marked this conversation as resolved.
|
||
|
|
||
| return { ref, ready, exited }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.