Skip to content

Commit

Permalink
Merge pull request #992 from hpcc-systems/yadhap/clean-up-unverified-…
Browse files Browse the repository at this point in the history
…user

Added a job that cleans up unverified users
  • Loading branch information
FancMa01 authored Jan 10, 2025
2 parents 3c6f0f3 + f836a13 commit d0fa693
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Tombolo/server/jobSchedular/job-scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const {
startJobPunctualityMonitoring,
} = require("../jobSchedularMethods/jobMonitoring.js");

const {removeUnverifiedUser} = require("../jobSchedularMethods/userManagementJobs.js");

class JobScheduler {
constructor() {
this.bree = new Bree({
Expand Down Expand Up @@ -135,6 +137,7 @@ class JobScheduler {
await this.startIntermediateJobsMonitoring();
await this.startJobPunctualityMonitoring();
await this.checkClusterReachability();
await removeUnverifiedUser.call(this);
logger.info("-----------------------------");
logger.info("Server is finished intializing, and is now running");
logger.info("-----------------------------");
Expand Down Expand Up @@ -369,6 +372,11 @@ class JobScheduler {
checkClusterReachability() {
return checkClusterReachability.call(this);
}

// User management jobs
removeUnverifiedUser() {
return removeUnverifiedUser.call(this);
}
}

module.exports = new JobScheduler();
30 changes: 30 additions & 0 deletions Tombolo/server/jobSchedularMethods/userManagementJobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require("path");
const logger = require("../config/logger");


async function removeUnverifiedUser() {
try {
let jobName = "remove-unverified-users-" + new Date().getTime();
this.bree.add({
name: jobName,
interval: "3600s",
path: path.join(__dirname, "..", "jobs", "userManagement", "removeUnverifiedUsers.js"),
worker: {
workerData: {
jobName: jobName,
WORKER_CREATED_AT: Date.now(),
},
},
});

this.bree.start(jobName);
logger.info("User management (remove unverified user) initialized ...");
} catch (err) {
console.error(err);
}
}


module.exports = {
removeUnverifiedUser,
};
38 changes: 38 additions & 0 deletions Tombolo/server/jobs/userManagement/removeUnverifiedUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// imports
const { parentPort } = require("worker_threads");
const { Op } = require("sequelize");


//Local Imports
const models = require("../../models");
const { user} = models;


(async () => {
try {
parentPort && parentPort.postMessage({level: "info", text : "Job to remove unverified users started ..."});

const now = Date.now();

// get all unverified users, if the user is unverified for more than 24 hours, delete the user
const unverifiedUsers = await user.findAll({
where: {
verifiedUser: false,
createdAt: {
[Op.lt]: now - 24 * 60 * 60 * 1000
}
}
});

parentPort && parentPort.postMessage({level: "info", text : `Number of unverified users to be removed: ${unverifiedUsers.length}`});

for (const user of unverifiedUsers) {
await user.destroy();
}

parentPort && parentPort.postMessage({level: "info", text : "Job to remove unverified user completed ..."});

} catch (error) {
parentPort && parentPort.postMessage({level: "error", text : error.message});
}
})();

0 comments on commit d0fa693

Please sign in to comment.