From 56087b3a0f5a90333685eb6c9c453d1f3b1187c3 Mon Sep 17 00:00:00 2001 From: Drizzentic Date: Thu, 7 Sep 2023 16:42:22 +0300 Subject: [PATCH] Revert "Add node scripts scheduler " --- app/app.js | 4 --- app/conn/connection.js | 10 ------ app/scheduler/scheduler.js | 45 --------------------------- conf/tasks.json | 12 -------- package-lock.json | 46 +--------------------------- package.json | 4 +-- tasks/complete-active-visits.js | 54 --------------------------------- tasks/sample.js | 1 - 8 files changed, 2 insertions(+), 174 deletions(-) delete mode 100644 app/scheduler/scheduler.js delete mode 100644 conf/tasks.json delete mode 100644 tasks/complete-active-visits.js delete mode 100644 tasks/sample.js diff --git a/app/app.js b/app/app.js index cef9337..5d40eeb 100644 --- a/app/app.js +++ b/app/app.js @@ -4,7 +4,6 @@ const TIMEOUT_INTERVAL = 3 * 1000; // 3 seconds const { syncBaseTables } = require('./base-sync'); const { syncFlatTables } = require('./process-manager'); -const { scheduleTasks } = require('./scheduler/scheduler'); const dayTimeSync = async () => { console.log( @@ -77,7 +76,4 @@ const startSync = async () => { } }; -// schedule task -scheduleTasks(); - startSync(); diff --git a/app/conn/connection.js b/app/conn/connection.js index 2c1a3b9..ed41bcb 100644 --- a/app/conn/connection.js +++ b/app/conn/connection.js @@ -13,16 +13,6 @@ const connectionPool = mysql.createPool({ connectionLimit: config.mysql.connectionLimit, }); -const amrsConnectionPool = mysql.createPool({ - host: config.amrsmysql.host, - user: config.amrsmysql.user, - password: config.amrsmysql.password, - database: config.amrsmysql.database, - port: config.amrsmysql.port, - connectionLimit: config.amrsmysql.connectionLimit, -}); - module.exports = { connectionPool: connectionPool, - amrsConnectionPool: amrsConnectionPool }; diff --git a/app/scheduler/scheduler.js b/app/scheduler/scheduler.js deleted file mode 100644 index b9ee915..0000000 --- a/app/scheduler/scheduler.js +++ /dev/null @@ -1,45 +0,0 @@ -const cron = require('node-cron'); -const { spawn } = require('child_process'); -const fs = require('fs'); - -// Load tasks from tasks.json -const tasksFile = './conf/tasks.json'; -const tasks = JSON.parse(fs.readFileSync(tasksFile, 'utf8')); - -// Function to execute a Node.js script -const executeScript = (script) => { - const startTime = new Date(); - const child = spawn('node', [script]); - - child.stdout.on('data', (data) => { - console.log(`🔰 [${script}] stdout: ${data}`); - }); - - child.stderr.on('data', (data) => { - console.error(`🔰 [${script}] stderr: ${data}`); - }); - - child.on('close', (code) => { - const endTime = new Date(); - const executionTime = (endTime - startTime) / 1000; // in seconds - console.log( - `🔰 [${script}] exited with code ${code}. Execution time: ${executionTime} seconds.` - ); - }); -}; - -// Schedule tasks -const scheduleTasks = () => { - tasks.forEach((task) => { - if (task.enabled) { - cron.schedule(task.schedule, () => { - console.log(`🔰 Executing [${task.script}] task...`); - executeScript(task.script); - }); - } else { - console.log(`🔰 Task [${task.script}] is disabled.`); - } - }); -}; - -exports.scheduleTasks = scheduleTasks; diff --git a/conf/tasks.json b/conf/tasks.json deleted file mode 100644 index 70d36bd..0000000 --- a/conf/tasks.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "script": "./tasks/sample.js", - "schedule": "*/5 * * * * *", - "enabled": true - }, - { - "script": "./tasks/complete-visits.js", - "schedule": "*/5 * * * *", - "enabled": false - } -] diff --git a/package-lock.json b/package-lock.json index 1bc6465..0e857ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,10 +10,8 @@ "license": "ISC", "dependencies": { "axios": "^0.21.1", - "fs": "^0.0.1-security", "moment": "^2.29.1", - "mysql": "^2.18.1", - "node-cron": "^3.0.2" + "mysql": "^2.18.1" }, "devDependencies": { "jest": "^29.5.0", @@ -1964,11 +1962,6 @@ } } }, - "node_modules/fs": { - "version": "0.0.1-security", - "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", - "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -3751,17 +3744,6 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, - "node_modules/node-cron": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", - "integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", - "dependencies": { - "uuid": "8.3.2" - }, - "engines": { - "node": ">=6.0.0" - } - }, "node_modules/node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", @@ -4398,14 +4380,6 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, - "node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/v8-to-istanbul": { "version": "9.1.0", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.1.0.tgz", @@ -6054,11 +6028,6 @@ "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.1.tgz", "integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==" }, - "fs": { - "version": "0.0.1-security", - "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", - "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" - }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -7389,14 +7358,6 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, - "node-cron": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", - "integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", - "requires": { - "uuid": "8.3.2" - } - }, "node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", @@ -7846,11 +7807,6 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, - "uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" - }, "v8-to-istanbul": { "version": "9.1.0", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.1.0.tgz", diff --git a/package.json b/package.json index 17d9318..b260ad9 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,8 @@ "license": "ISC", "dependencies": { "axios": "^0.21.1", - "fs": "^0.0.1-security", "moment": "^2.29.1", - "mysql": "^2.18.1", - "node-cron": "^3.0.2" + "mysql": "^2.18.1" }, "devDependencies": { "jest": "^29.5.0", diff --git a/tasks/complete-active-visits.js b/tasks/complete-active-visits.js deleted file mode 100644 index 74bf6d4..0000000 --- a/tasks/complete-active-visits.js +++ /dev/null @@ -1,54 +0,0 @@ -/** - * - * This code implementation is not ideal for accomplishing the task efficiently.(Just a short-term remedy) - * A recommended approach would be to utilize the OpenMRS scheduler to automatically complete visits at midnight. - * The current implementation seems to be a short-term remedy and it has several issues; - * - One major concern is that it directly modifies the database without going through the OpenMRS service layer. - * - This can lead to inconsistencies and potential problems in the future. - * - Short-cuts - * - * It is crucial to follow the proper architecture and utilize the designated services provided by OpenMRS for any database-related operations. - * - */ - -const { amrsConnectionPool } = require('../app/conn/connection'); - -// Verify MySQL connection -amrsConnectionPool.connect((error) => { - if (error) { - console.error('Error connecting to MySQL database:', error.stack); - return; - } - - console.log('Connected to MySQL database as ID', connection.threadId); - - // Select rows with date_stopped and update them in batches - const batchSize = 1000; // Adjust this number based on the available memory - - const updateBatch = () => { - const selectQuery = `SELECT * FROM amrs.visit WHERE date_stopped IS NULL AND voided = 0 LIMIT ${batchSize}`; - connection.query(selectQuery, (error, results) => { - if (error) throw error; - - if (results.length === 0) { - console.log('All rows updated successfully!'); - connection.end(); - return; - } - - results.forEach((row) => { - // Update the row by adding now() to date_stopped and setting date_changed and changed_by - const updateQuery = `UPDATE amrs.visit SET date_stopped = NOW(), date_changed = NOW(), changed_by = 168362 WHERE visit_id = ${row.visit_id}`; - connection.query(updateQuery, (error, result) => { - if (error) throw error; - - console.log(`Row ${row.visit_id} updated successfully!`); - }); - }); - - updateBatch(); - }); - }; - - updateBatch(); -}); diff --git a/tasks/sample.js b/tasks/sample.js deleted file mode 100644 index 1e8bddd..0000000 --- a/tasks/sample.js +++ /dev/null @@ -1 +0,0 @@ -console.log("Hello there!!");