Updates
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { _n } from "@/client-exports";
|
||||
import getQueue from "@/package-shared/functions/backend/queues/get-queue";
|
||||
import updateQueue from "@/package-shared/functions/backend/queues/update-queue";
|
||||
import { DSQL_DATASQUIREL_PROCESS_QUEUE } from "@/package-shared/types/dsql";
|
||||
import debugLog from "@/package-shared/utils/logging/debug-log";
|
||||
|
||||
function debugLogFn(log: any, label?: string) {
|
||||
debugLog({ log, addTime: true, label, title: __filename.split("/").pop() });
|
||||
}
|
||||
|
||||
export default async function cronHandleQueue() {
|
||||
const INTERVAL = 5000;
|
||||
|
||||
while (true) {
|
||||
await (async () => {
|
||||
const lastQueueItemRes = (await getQueue({
|
||||
query: {
|
||||
query: {
|
||||
error: {
|
||||
value: "0",
|
||||
},
|
||||
running: {
|
||||
value: "0",
|
||||
},
|
||||
success: {
|
||||
value: "0",
|
||||
},
|
||||
},
|
||||
order: {
|
||||
field: "id",
|
||||
strategy: "ASC",
|
||||
},
|
||||
limit: 1,
|
||||
},
|
||||
})) as DSQL_DATASQUIREL_PROCESS_QUEUE[] | undefined;
|
||||
|
||||
const lastQueueItem = lastQueueItemRes?.[0];
|
||||
|
||||
if (!lastQueueItem) return;
|
||||
|
||||
debugLogFn(lastQueueItem.title, "Running Queue");
|
||||
|
||||
await updateQueue({
|
||||
queueId: _n(lastQueueItem.id),
|
||||
queue: {
|
||||
running: 1,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
switch (lastQueueItem.job_type) {
|
||||
/**
|
||||
* # Dummy Queue
|
||||
*/
|
||||
case "dummy":
|
||||
await Bun.sleep(20000);
|
||||
break;
|
||||
/**
|
||||
* # Unhandled
|
||||
*/
|
||||
default:
|
||||
return;
|
||||
}
|
||||
} catch (error: any) {
|
||||
debugLogFn(error.message, "ERROR");
|
||||
|
||||
await updateQueue({
|
||||
queueId: _n(lastQueueItem.id),
|
||||
queue: {
|
||||
running: 0,
|
||||
error: 1,
|
||||
error_message: String(error.message),
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await updateQueue({
|
||||
queueId: _n(lastQueueItem.id),
|
||||
queue: {
|
||||
success: 1,
|
||||
},
|
||||
});
|
||||
})();
|
||||
|
||||
await Bun.sleep(INTERVAL);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import createDbFromSchema from "@/package-shared/shell/createDbFromSchema";
|
||||
import grabDirNames from "@/package-shared/utils/backend/names/grab-dir-names";
|
||||
import debugLog from "@/package-shared/utils/logging/debug-log";
|
||||
import dbSchemaToType from "@/utils/backend/db-schema-to-type";
|
||||
import fs from "fs";
|
||||
|
||||
function debugLogFn(log: any, label?: string) {
|
||||
debugLog({ log, addTime: true, label, title: "watchMainDbSchemaJSONFile" });
|
||||
}
|
||||
|
||||
let syncing = 0;
|
||||
let timeout: any;
|
||||
|
||||
const DEBOUNCE = 500;
|
||||
|
||||
export default function watchMainDbSchemaJSONFile() {
|
||||
if (process.env.NODE_ENV?.match(/prod/)) return;
|
||||
if (process.env.DSQL_HOST_ENV?.match(/prod/)) return;
|
||||
|
||||
const { mainShemaJSONFilePath, mainDbTypeDefFile } = grabDirNames();
|
||||
|
||||
fs.watch(mainShemaJSONFilePath, (curr) => {
|
||||
if (syncing == 1) return;
|
||||
if (curr !== "change") return;
|
||||
clearTimeout(timeout);
|
||||
|
||||
debugLogFn("Main Schema JSON File Changed!");
|
||||
|
||||
syncing = 1;
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
const definitions = dbSchemaToType();
|
||||
|
||||
fs.writeFileSync(
|
||||
mainDbTypeDefFile,
|
||||
definitions?.join("\n\n") || "",
|
||||
"utf-8"
|
||||
);
|
||||
|
||||
createDbFromSchema({})
|
||||
.then((res) => {
|
||||
debugLogFn(res, "res");
|
||||
})
|
||||
.catch((err) => {
|
||||
debugLogFn(err, "res");
|
||||
})
|
||||
.finally(() => {
|
||||
setTimeout(() => {
|
||||
debugLogFn("Main Rebuilt Successfully!");
|
||||
syncing = 0;
|
||||
}, 1000);
|
||||
});
|
||||
}, DEBOUNCE);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
@moduletrace:registry=https://git.tben.me/api/packages/moduletrace/npm/
|
||||
@@ -0,0 +1,15 @@
|
||||
FROM node:20-bookworm
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -y apt-transport-https git ca-certificates curl gnupg python3 python3-pip make build-essential mariadb-client wget zip unzip xz-utils
|
||||
|
||||
RUN curl -fsSL https://bun.sh/install | bash
|
||||
ENV PATH="/root/.bun/bin:${PATH}"
|
||||
|
||||
COPY .npmrc /root/.bun/install/global/.npmrc
|
||||
RUN bun add -g @moduletrace/turbosync
|
||||
|
||||
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
|
||||
RUN chmod +x /usr/local/bin/entrypoint.sh
|
||||
|
||||
ENTRYPOINT ["entrypoint.sh"]
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd /app
|
||||
|
||||
if [ "$NODE_ENV" == "production" ]; then
|
||||
bun docker/cron/index.ts
|
||||
else
|
||||
bun --watch docker/cron/index.ts
|
||||
fi
|
||||
@@ -0,0 +1,28 @@
|
||||
import mysql from "serverless-mysql";
|
||||
|
||||
global.DSQL_DB_CONN = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
global.DSQL_USE_LOCAL = true;
|
||||
|
||||
import watchMainDbSchemaJSONFile from "./(functions)/watch-main-db-schema-json-file";
|
||||
import cronHandleQueue from "./(functions)/queue/handle-queue";
|
||||
|
||||
console.log("Running Cron Services ....");
|
||||
|
||||
/**
|
||||
* # Watch Main JSON DB Schema File
|
||||
*/
|
||||
watchMainDbSchemaJSONFile();
|
||||
|
||||
/**
|
||||
* # Handle Queue
|
||||
*/
|
||||
cronHandleQueue();
|
||||
Reference in New Issue
Block a user