Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitsuthar committed Oct 8, 2024
1 parent d82f1ce commit 1f76419
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 49 deletions.
20 changes: 8 additions & 12 deletions lib/nr-security-agent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,20 @@ function initialize() {
NRAgent.config.security.scan_schedule.delay = delay;
const allowSampling = NRAgent.config.security.scan_schedule.always_sample_traces;

if (delay > 0) {
if (delay >= 0) {
logger.debug("IAST delay is set to:", delay);
logger.info("Security Agent delay scan time is set to:", commonUtils.getScheduledScanTime(delay))
Agent.getAgent().delayed = true;
setTimeout(() => {
Agent.getAgent().delayed = false;
logger.info("IAST scanning delay of %s minutes over", delay);
commonUtils.executeCronSchedule();
commonUtils.honourDurationConfiguration();
commonUtils.executeCronSchedule();
if (!allowSampling) {
wsClient.initialize();
Agent.getAgent().status.setStatus('connecting');
Agent.setNRAgent(NRAgent);
}
}, delay * 60000);
}
logger.info("Security Agent is running with config:", JSON.stringify(NRAgent.config.security));
Expand All @@ -149,16 +154,7 @@ function initialize() {
Agent.getAgent().status.setStatus('connecting');
Agent.setNRAgent(NRAgent);
}
setTimeout(() => {
if (commonUtils.honourCronExpression() && !allowSampling) {
return;
}
if (!allowSampling) {
wsClient.initialize();
Agent.getAgent().status.setStatus('connecting');
Agent.setNRAgent(NRAgent);
}
}, delay * 60000);

}
else {
logger.warn("security.enabled flag is set to false");
Expand Down
39 changes: 2 additions & 37 deletions lib/nr-security-agent/lib/core/commonUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,12 @@ function executeCronSchedule() {
}
logger.debug("Schedule config is set to:", schedule);
schedule = schedule.replace(/\?/g, '*');
logger.info("Security Agent scheduled time is set to:", getScheduledScanTime(Math.ceil(cron.timeout(schedule) / 60000)))
const job = new cron.CronJob(
schedule, // cronTime
function () {
logger.debug('Cron schedule invoked');
logger.info("Security Agent scheduled time is set to:", getScheduledScanTime(Math.ceil(cron.timeout(schedule) / 60000)))
refreshAgent()
}, // onTick
null, // onComplete
Expand Down Expand Up @@ -543,42 +545,6 @@ function shutDownAgent() {
logger.warn("Security Agent status is disabled");
}

function honourCronExpression() {
let honourCronFlag = false;
try {
const { Agent } = require('./agent');
const wsClient = Agent.getAgent().client;
let schedule = NRAgent.config.security.scan_schedule.schedule;
if (lodash.isEmpty(schedule)) {
return;
}
logger.debug("Schedule config is set to:", schedule);

schedule = schedule.replace(/\?/g, '*');
honourCronFlag = true;
Agent.getAgent().delayed = true;
logger.info("Security Agent scheduled time is set to:", getScheduledScanTime(Math.floor(cron.timeout(schedule) / 60000)))
setTimeout(() => {
Agent.getAgent().delayed = false;
if (!NRAgent.config.security.scan_schedule.always_sample_traces) {
wsClient.initialize();
}
else {
wsClient.obeyReconnect();
}
Agent.getAgent().status.setStatus('connecting');
Agent.setNRAgent(NRAgent);
logger.info("Security Agent is running with config:", JSON.stringify(NRAgent.config.security));
honourDurationConfiguration();
executeCronSchedule();
}, cron.timeout(schedule));
} catch (error) {
logger.debug("Error while processing cron expression:", error);
return false;
}
return honourCronFlag;

}



Expand Down Expand Up @@ -611,5 +577,4 @@ module.exports = {
honourDurationConfiguration,
sendErrorToErrorInbox,
getScheduledScanTime,
honourCronExpression
};

0 comments on commit 1f76419

Please sign in to comment.