Skip to content

Commit

Permalink
fix(sdks/actor/manager): fix manager builds with new workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Jan 22, 2025
1 parent ce7f441 commit ed15ca8
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 161 deletions.
2 changes: 1 addition & 1 deletion packages/toolchain/js-utils-embed/js/deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@bartlomieju/postgres": "jsr:@bartlomieju/postgres@^0.17.2",
"@cross/dir": "jsr:@cross/dir@^1.1.0",
"@hono/hono": "jsr:@hono/hono@^4.6.3",
"@luca/esbuild-deno-loader": "jsr:@luca/esbuild-deno-loader@^0.11.1",
"@luca/esbuild-deno-loader": "jsr:@rivet-gg/esbuild-deno-loader@^0.10.3-fork.3",
"@std/assert": "jsr:@std/assert@0.213",
"@std/async": "jsr:@std/async@^1.0.4",
"@std/cli": "jsr:@std/cli@^1.0.5",
Expand Down
10 changes: 1 addition & 9 deletions packages/toolchain/toolchain/src/tasks/deploy/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ pub async fn deploy(
.to_string(),
bundler: Some(config::build::javascript::Bundler::Deno),
deno: config::build::javascript::Deno {
// TODO(RVT-4382): Does not support workspaces, so we have to point to the
// manager's Deno config
config_path: Some(
manager_src_path
.join("manager")
.join("deno.jsonc")
.display()
.to_string(),
),
config_path: None,
import_map_url: None,
lock_path: Some(manager_src_path.join("deno.lock").display().to_string()),
},
Expand Down
7 changes: 6 additions & 1 deletion sdks/actor/manager/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"name": "@rivet-gg/actor-manager",
"version": "24.6.2-rc.1",
"private": true
"private": true,
"dependencies": {
"@rivet-gg/actor-common": "workspace:*",
"@rivet-gg/manager-protocol": "workspace:*",
"hono": "^4.6.17"
}
}
3 changes: 2 additions & 1 deletion sdks/actor/manager/src/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getLogger } from "../../common/src/log.ts";
// @ts-types="../../common/dist/log.d.ts"
import { getLogger } from "@rivet-gg/actor-common/log";

export const LOGGER_NAME = "actor-manager";

Expand Down
267 changes: 124 additions & 143 deletions sdks/actor/manager/src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,143 +1,124 @@
import type { ActorContext } from "@rivet-gg/actor-core";
import { RivetClient } from "@rivet-gg/api";
import { assertExists } from "@std/assert/exists";
import { Hono, type Context as HonoContext } from "hono";
import { cors } from "hono/cors";
import { setupLogging } from "../../common/src/log.ts";
import { PORT_NAME } from "../../common/src/network.ts";
import {
type RivetEnvironment,
assertUnreachable,
} from "../../common/src/utils.ts";
import {
ActorsRequestSchema,
type ActorsResponse,
type RivetConfigResponse,
} from "../../manager-protocol/src/mod.ts";
import { logger } from "./log.ts";
import { queryActor } from "./query_exec.ts";

export default class Manager {
private readonly endpoint: string;
private readonly rivetClient: RivetClient;
private readonly environment: RivetEnvironment;

constructor(private readonly ctx: ActorContext) {
const endpoint = Deno.env.get("RIVET_API_ENDPOINT");
assertExists(endpoint, "missing RIVET_API_ENDPOINT");
const token = Deno.env.get("RIVET_SERVICE_TOKEN");
assertExists(token, "missing RIVET_SERVICE_TOKEN");

this.endpoint = endpoint;

this.rivetClient = new RivetClient({
environment: endpoint,
token,
});

this.environment = {
project: this.ctx.metadata.project.slug,
environment: this.ctx.metadata.environment.slug,
};
}

static async start(ctx: ActorContext) {
setupLogging();

// biome-ignore lint/complexity/noThisInStatic: Must be used for default actor entrypoint
const manager = new this(ctx);
await manager.#run();
}

async #run() {
const portStr = Deno.env.get("PORT_HTTP");
if (!portStr) throw "Missing port";
const port = Number.parseInt(portStr);
if (!Number.isFinite(port)) throw "Invalid port";

const app = new Hono();

app.use(
"/*",
cors({
origin: (origin) => {
return origin.includes("localhost");
},
}),
);

app.get("/rivet/config", (c: HonoContext) => {
return c.json({
// HACK(RVT-4376): Replace DNS address used for local dev envs with public address
endpoint: this.endpoint.replace("rivet-server", "127.0.0.1"),
project: this.environment.project,
environment: this.environment.environment,
} satisfies RivetConfigResponse);
});

app.post("/actors", async (c: HonoContext) => {
// Get actor
const body = ActorsRequestSchema.parse(await c.req.json());
const actor = await queryActor(
this.rivetClient,
this.environment,
body.query,
);

// Fetch port
const httpPort = actor.network.ports[PORT_NAME];
assertExists(httpPort, "missing port");
const hostname = httpPort.hostname;
assertExists(hostname);
const port = httpPort.port;
assertExists(port);

let isTls = false;
switch (httpPort.protocol) {
case "https":
isTls = true;
break;
case "http":
case "tcp":
isTls = false;
break;
case "tcp_tls":
case "udp":
throw new Error(`Invalid protocol ${httpPort.protocol}`);
default:
assertUnreachable(httpPort.protocol);
}

const path = httpPort.path ?? "";

const endpoint = `${
isTls ? "https" : "http"
}://${hostname}:${port}${path}`;

return c.json({ endpoint } satisfies ActorsResponse);
});

app.all("*", (c) => {
return c.text("Not Found", 404);
});

logger().info("server running", { port });
const server = Deno.serve(
{
port,
hostname: "0.0.0.0",
// Remove "Listening on ..." message
onListen() {},
},
app.fetch,
);

logger().debug("rivet endpoint", {
endpoint: this.endpoint,
project: this.ctx.metadata.project.slug,
environment: this.ctx.metadata.environment.slug,
});

await server.finished;
}
}
//export default class Manager {
// private readonly endpoint: string;
// private readonly rivetClient: RivetClient;
// private readonly environment: RivetEnvironment;
//
// constructor(private readonly ctx: ActorContext) {
// const endpoint = Deno.env.get("RIVET_API_ENDPOINT");
// assertExists(endpoint, "missing RIVET_API_ENDPOINT");
// const token = Deno.env.get("RIVET_SERVICE_TOKEN");
// assertExists(token, "missing RIVET_SERVICE_TOKEN");
//
// this.endpoint = endpoint;
//
// this.rivetClient = new RivetClient({
// environment: endpoint,
// token,
// });
//
// this.environment = {
// project: this.ctx.metadata.project.slug,
// environment: this.ctx.metadata.environment.slug,
// };
// }
//
// static async start(ctx: ActorContext) {
// setupLogging();
//
// // biome-ignore lint/complexity/noThisInStatic: Must be used for default actor entrypoint
// const manager = new this(ctx);
// await manager.#run();
// }
//
// async #run() {
// const portStr = Deno.env.get("PORT_HTTP");
// if (!portStr) throw "Missing port";
// const port = Number.parseInt(portStr);
// if (!Number.isFinite(port)) throw "Invalid port";
//
// const app = new Hono();
//
// app.use(
// "/*",
// cors({
// origin: (origin) => {
// return origin.includes("localhost");
// },
// }),
// );
//
// app.get("/rivet/config", (c: HonoContext) => {
// return c.json({
// // HACK(RVT-4376): Replace DNS address used for local dev envs with public address
// endpoint: this.endpoint.replace("rivet-server", "127.0.0.1"),
// project: this.environment.project,
// environment: this.environment.environment,
// } satisfies RivetConfigResponse);
// });
//
// app.post("/actors", async (c: HonoContext) => {
// // Get actor
// const body = ActorsRequestSchema.parse(await c.req.json());
// const actor = await queryActor(
// this.rivetClient,
// this.environment,
// body.query,
// );
//
// // Fetch port
// const httpPort = actor.network.ports[PORT_NAME];
// assertExists(httpPort, "missing port");
// const hostname = httpPort.hostname;
// assertExists(hostname);
// const port = httpPort.port;
// assertExists(port);
//
// let isTls = false;
// switch (httpPort.protocol) {
// case "https":
// isTls = true;
// break;
// case "http":
// case "tcp":
// isTls = false;
// break;
// case "tcp_tls":
// case "udp":
// throw new Error(`Invalid protocol ${httpPort.protocol}`);
// default:
// assertUnreachable(httpPort.protocol);
// }
//
// const path = httpPort.path ?? "";
//
// const endpoint = `${
// isTls ? "https" : "http"
// }://${hostname}:${port}${path}`;
//
// return c.json({ endpoint } satisfies ActorsResponse);
// });
//
// app.all("*", (c) => {
// return c.text("Not Found", 404);
// });
//
// logger().info("server running", { port });
// const server = Deno.serve(
// {
// port,
// hostname: "0.0.0.0",
// // Remove "Listening on ..." message
// onListen() {},
// },
// app.fetch,
// );
//
// logger().debug("rivet endpoint", {
// endpoint: this.endpoint,
// project: this.ctx.metadata.project.slug,
// environment: this.ctx.metadata.environment.slug,
// });
//
// await server.finished;
// }
//}
16 changes: 10 additions & 6 deletions sdks/actor/manager/src/query_exec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import type { Rivet, RivetClient } from "@rivet-gg/api";
import { assertEquals } from "@std/assert";
import { PORT_NAME } from "../../common/src/network.ts";
import { assertUnreachable } from "../../common/src/utils.ts";
// @ts-types="../../common/dist/network.d.ts"
import { PORT_NAME } from "@rivet-gg/actor-common/network";
// @ts-types="../../common/dist/utils.d.ts"
import { assertUnreachable } from "@rivet-gg/actor-common/utils";
// @ts-types="../../common/dist/utils.d.ts"
import type {
ActorTags,
BuildTags,
RivetEnvironment,
} from "../../common/src/utils.ts";
} from "@rivet-gg/actor-common/utils";
import type { Rivet, RivetClient } from "@rivet-gg/api";
// @ts-types="../../manager-protocol/dist/query.d.ts"
import type {
ActorQuery,
CreateRequest,
} from "../../manager-protocol/src/query.ts";
} from "@rivet-gg/manager-protocol/query";
import { assertEquals } from "@std/assert";
import { logger } from "./log.ts";

export async function queryActor(
Expand Down

0 comments on commit ed15ca8

Please sign in to comment.