dsql-admin/dsql-app/utils/cron.js
Benjamin Toby 8b7ea057d0 Updates
2024-12-06 14:24:26 +01:00

79 lines
3.1 KiB
JavaScript

// @ts-check
const httpsRequest = require("../package-shared/functions/backend/httpsRequest");
const cron = require("cron");
const path = require("path");
const { dbSchemaExecDbUpdate } = require("../functions/backend/dbSchemaExec");
const dbHandler = require("../package-shared/functions/backend/dbHandler");
module.exports = async function () {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
new cron.CronJob(
"20 * * * * *",
async () => {
/** @type {import("@/package-shared/types").DSQL_MYSQL_user_databases_Type[] | null} */ // @ts-ignore
const remoteConnectedDbs = await dbHandler(
`SELECT * FROM user_databases WHERE remote_connected = 1`
);
if (remoteConnectedDbs?.[0]) {
for (let i = 0; i < remoteConnectedDbs.length; i++) {
try {
const database = remoteConnectedDbs[i];
const {
user_id,
remote_connection_host,
remote_db_full_name,
remote_connection_key,
remote_connection_type,
db_full_name,
} = database;
if (database.remote_connection_type == "pull") {
const dbSchema = await httpsRequest({
url: remote_connection_host,
headers: {
Authorization: remote_connection_key,
"Content-Type": "application/json",
},
method: "GET",
path: `/api/query/get-schema?database=${remote_db_full_name?.replace(
/^datasquirel_user_\d+_/i,
""
)}`,
});
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | null | undefined} */
const remoteDbSchemaData =
JSON.parse(dbSchema).payload;
if (remoteDbSchemaData) {
const update = dbSchemaExecDbUpdate({
dbSchema: remoteDbSchemaData,
database: database,
userId: user_id,
});
console.log(update);
}
}
} catch (/** @type {any} */ error) {
console.log(error.message);
}
}
}
},
null,
true,
"America/Los_Angeles"
);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
};