dsql-admin/dsql-app/utils/cron.js

79 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
// @ts-check
const httpsRequest = require("../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"
);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
};