feat(schedules): add schedules server

This commit is contained in:
Mauricio Siu
2024-10-05 22:11:38 -06:00
parent 651bf3a303
commit 43555cdabe
19 changed files with 584 additions and 122 deletions

View File

@@ -8,6 +8,8 @@
"start": "node --experimental-specifier-resolution=node dist/index.js"
},
"dependencies": {
"pino": "9.4.0",
"pino-pretty": "11.2.2",
"@hono/zod-validator": "0.3.0",
"zod": "^3.23.4",
"react": "18.2.0",
@@ -16,7 +18,6 @@
"@hono/node-server": "^1.12.1",
"hono": "^4.5.8",
"dotenv": "^16.3.1",
"@upstash/qstash": "2.7.9",
"redis": "4.7.0",
"@nerimity/mimiqueue": "1.2.3"
},

View File

@@ -6,6 +6,7 @@ import { Queue } from "@nerimity/mimiqueue";
import { zValidator } from "@hono/zod-validator";
import { type DeployJob, deployJobSchema } from "./schema";
import { deploy } from "./utils";
import { logger } from "./logger";
const app = new Hono();
const redisClient = createClient({
@@ -14,12 +15,10 @@ const redisClient = createClient({
app.post("/deploy", zValidator("json", deployJobSchema), (c) => {
const data = c.req.valid("json");
queue.add(data, { groupName: data.serverId }).then((res) => {
console.log(res);
});
const res = queue.add(data, { groupName: data.serverId });
return c.json(
{
message: "Deployment started",
message: "Deployment Added",
},
200,
);
@@ -32,17 +31,18 @@ app.get("/health", async (c) => {
const queue = new Queue({
name: "deployments",
process: async (job: DeployJob) => {
console.log(job);
logger.info("Deploying job", job);
return await deploy(job);
},
redisClient,
});
const port = Number.parseInt(process.env.PORT || "3000");
(async () => {
await redisClient.connect();
await redisClient.flushAll();
logger.info("Cleaning Redis");
})();
console.log("Starting Server ✅", port);
const port = Number.parseInt(process.env.PORT || "3000");
logger.info("Starting Deployments Server ✅", port);
serve({ fetch: app.fetch, port });

10
apps/api/src/logger.ts Normal file
View File

@@ -0,0 +1,10 @@
import pino from "pino";
export const logger = pino({
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
});

View File

@@ -1,82 +0,0 @@
// import { Hono } from "hono";
// import { Client } from "@upstash/qstash";
// import { serve } from "@hono/node-server";
// import dotenv from "dotenv";
// import Redis from "ioredis";
// dotenv.config();
// const redis = new Redis({
// host: "localhost",
// port: 7777,
// password: "xlfvpQ0ma2BkkkPX",
// });
// // redis.set("test", "test");
// // console.log(await redis.get("test"));
// // console.log(await redis.get("user-1-processing"));
// const app = new Hono();
// console.log("QStash Token:", process.env.PUBLIC_URL);
// const qstash = new Client({
// token: process.env.QSTASH_TOKEN as string,
// });
// const queue = qstash.queue({
// queueName: "deployments",
// });
// // Endpoint que publica un mensaje en QStash
// app.post("/enqueue", async (c) => {
// const { userId, deploymentId } = await c.req.json();
// const response = await qstash.publishJSON({
// url: `${process.env.PUBLIC_URL}/process`, // Endpoint para procesar la tarea
// body: { userId, deploymentId }, // Datos del despliegue
// });
// return c.json({ message: "Task enqueued", id: response.messageId });
// });
// // Endpoint que recibe el mensaje procesado
// app.post("/process", async (c) => {
// const { userId, deploymentId } = await c.req.json();
// const isProcessing = await redis.get(`user-${userId}-processing`);
// console.log(`isProcessing for user ${userId}:`, isProcessing);
// if (isProcessing === "true") {
// console.log(
// `User ${userId} is already processing a deployment. Queuing the next one.`,
// );
// return c.json(
// {
// status: "User is already processing a deployment, waiting...",
// },
// {
// status: 400,
// },
// );
// }
// redis.set(`user-${userId}-processing`, "true");
// try {
// await new Promise((resolve) => setTimeout(resolve, 5000));
// } catch (error) {
// } finally {
// await redis.del(`user-${userId}-processing`);
// }
// return c.json({ status: "Processed", userId, deploymentId });
// });
// // Inicia el servidor en el puerto 3000
// const port = 3000;
// console.log(`Server is running on port http://localhost:${port}`);
// serve({
// fetch: app.fetch,
// port,
// });
// // 18