Remove DB connection.
Move to using internal API to check current version.
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
// Functions to check the current applied version
|
||||
// updating will be handled by the processing service
|
||||
|
||||
import { MongoClient } from "mongodb";
|
||||
import { log } from "./logger";
|
||||
import type { Mongo } from "./config";
|
||||
|
||||
const collection = "data_ingress_meta";
|
||||
|
||||
|
||||
function uriBuild(cfg: Mongo): string {
|
||||
if(!cfg.MONGO_URI || !cfg.MONGO_DB || !cfg.MONGO_USER || !cfg.MONGO_PASS) {
|
||||
log('ERROR', "Missing MONGO Configuration - EXIT CODE: 35");
|
||||
process.exit(35);
|
||||
} else {
|
||||
log("DEBUG", `MongoDB Connection`, {
|
||||
uri: cfg.MONGO_URI,
|
||||
db: cfg.MONGO_DB,
|
||||
collection: collection,
|
||||
user: cfg.MONGO_USER,
|
||||
pass: "****",
|
||||
});
|
||||
};
|
||||
|
||||
return `mongodb://${encodeURIComponent(cfg.MONGO_USER)}:${encodeURIComponent(cfg.MONGO_PASS)}@${cfg.MONGO_URI}`
|
||||
}
|
||||
|
||||
let mongoClient: MongoClient | null = null;
|
||||
|
||||
async function getMongoClient(cfg: Mongo) {
|
||||
if (mongoClient) return mongoClient;
|
||||
|
||||
mongoClient = new MongoClient(uriBuild(cfg));
|
||||
await mongoClient.connect();
|
||||
return mongoClient;
|
||||
}
|
||||
|
||||
export async function isPackageProcessed(cfg: Mongo, serviceName: string, packageName: string): Promise<boolean> {
|
||||
try {
|
||||
const client = await getMongoClient(cfg);
|
||||
const database = client.db(cfg.MONGO_DB);
|
||||
const coll = database.collection(collection);
|
||||
|
||||
const result = await coll.findOne({ service_name: serviceName });
|
||||
|
||||
if (!result) {
|
||||
log('INFO', `No metadata found for ${serviceName}. Fetching PIS Data`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (result.latest_package === packageName) {
|
||||
log('INFO', 'No update needed');
|
||||
return true;
|
||||
}
|
||||
|
||||
log('INFO', `Version mismatch. DB: ${result.latest_package}, Current: ${packageName}. Update required`)
|
||||
return false;
|
||||
} catch (err) {
|
||||
log('ERROR', 'Failed to check Mongo for version state:', err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import { processAndStore } from './sss.js'
|
||||
|
||||
import { getLatestPackageName, getRequestStream } from './sources/gitea.js'
|
||||
import { processPisStream } from './process.js'
|
||||
import { isPackageProcessed } from './database.js'
|
||||
import { ConfigLoader } from './config.js'
|
||||
import { sendFileUpdateMessage } from './nats.js'
|
||||
|
||||
@@ -26,10 +25,8 @@ async function main() {
|
||||
process.exit(9);
|
||||
}
|
||||
|
||||
if (await isPackageProcessed(config.Mongo, SERVICE_NAME, packageInfo.name)) {
|
||||
log('INFO', `Database matches latest release. Exiting`);
|
||||
process.exit(0);
|
||||
}
|
||||
// Check if package already processed HERE...
|
||||
// exit(0) if done, else continue
|
||||
|
||||
const inputStream: Readable = await getRequestStream(packageInfo.assets[0].browser_download_url);
|
||||
const objectGenerator = processPisStream(config.General, inputStream);
|
||||
|
||||
Reference in New Issue
Block a user