sys_trigger not working well
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11 hours ago
Hi All,
I am stuck in one place in script. I created some function that in script include and try to run from background script. Everything is working fine, even logs giving correct value of sys_trigger time but it is not executing and ready state is showing as RED.
Please find below code that I am using for creating sys_trigger record.
var PostCloneSOP = Class.create();
PostCloneSOP.prototype = {
initialize: function() {},
postCloneSOP: function() {
gs.info('PostCloneSOP script started Time: ' + gs.nowDateTime());
//Platform updates
gs.info('PostCloneSOP - function = addAgentBackToRemoteChecksWithRetry started');
try {
this.addAgentBackToRemoteChecksWithRetry();
} catch (err) {
gs.error('PostCloneSOP - function = addAgentBackToRemoteChecksWithRetry error: ' + err.toString());
}
gs.info('PostCloneSOP script stopped time: ' + gs.nowDateTime());
},
addAgentBackToRemoteChecksWithRetry: function() {
gs.info('==============================');
gs.info('PostCloneSOP - Step 4 STARTED');
gs.info('==============================');
// ---------------------------------------------------
// Retry configuration
// ---------------------------------------------------
var maxRetries = 3;
var retryDelayMinutes = 2;
var retryCount = parseInt(gs.getProperty('postclone.remotechecks.retry.count', '0'), 10);
gs.info('Retry count from property: ' + retryCount);
gs.info('Max retries allowed: ' + maxRetries);
if (retryCount >= maxRetries) {
gs.error('Max retry limit reached. STOPPING retries.');
return;
}
// ---------------------------------------------------
// Decide agent name
// ---------------------------------------------------
var expectedAgentName = '';
var instance = gs.getProperty('instance_name');
if (instance === 'dev') {
expectedAgentName = 'test1234';
} else if (instance === 'qa') {
expectedAgentName = 'test5678';
}
gs.info('Expected agent name to look for: ' + expectedAgentName);
// ---------------------------------------------------
// Check if agent exists
// ---------------------------------------------------
var agentGr = new GlideRecord('sn_agent_cmdb_ci_agent');
agentGr.addQuery('name', expectedAgentName);
agentGr.query();
if (!agentGr.next()) {
gs.info('Agent NOT found yet in sn_agent_cmdb_ci_agent');
gs.info('Incrementing retry count and scheduling next retry');
// Increment retry count
var nextRetry = retryCount + 1;
gs.setProperty(
'postclone.remotechecks.retry.count',
nextRetry
);
gs.info('Retry count updated to: ' + nextRetry);
gs.info('Scheduling retry after ' + retryDelayMinutes + ' minutes');
this._scheduleRetry(retryDelayMinutes);
return;
}
var agentSysId = agentGr.getUniqueValue();
gs.info('Agent FOUND!');
gs.info('Agent Name: ' + expectedAgentName);
gs.info('Agent SysId: ' + agentSysId);
// ---------------------------------------------------
// Get Remote Checks cluster
// ---------------------------------------------------
var clusterGr = new GlideRecord('sn_agent_cluster');
clusterGr.addQuery('name', 'Remote Checks');
clusterGr.query();
if (!clusterGr.next()) {
gs.error('Remote Checks cluster NOT found');
return;
}
var clusterSysId = clusterGr.getUniqueValue();
gs.info('Remote Checks cluster found | SysId: ' + clusterSysId);
// ---------------------------------------------------
// Check if mapping already exists
// ---------------------------------------------------
var mapGr = new GlideRecord('sn_agent_in_agentcluster');
mapGr.addQuery('agent', agentSysId);
mapGr.addQuery('cluster', clusterSysId);
mapGr.query();
if (mapGr.next()) {
gs.info('Agent already exists in Remote Checks cluster');
gs.info('Resetting retry count to 0');
gs.setProperty('postclone.remotechecks.retry.count', '0');
return;
}
// ---------------------------------------------------
// ADD AGENT TO REMOTE CHECKS CLUSTER
// ---------------------------------------------------
gs.info('Adding agent to Remote Checks cluster');
var newMap = new GlideRecord('sn_agent_in_agentcluster');
newMap.initialize();
newMap.setValue('agent', agentSysId);
newMap.setValue('cluster', clusterSysId);
newMap.insert();
gs.info('Agent successfully added to Remote Checks cluster');
gs.info('Resetting retry count to 0');
gs.setProperty('postclone.remotechecks.retry.count', '0');
gs.info('==============================');
gs.info('PostCloneSOP - Step 4 COMPLETED');
gs.info('==============================');
},
_scheduleRetry: function(delayMinutes) {
gs.info('---------- _scheduleRetry START ----------');
gs.info('Delay (minutes) received: ' + delayMinutes);
// Remove old retry triggers (safety)
var oldTrigger = new GlideRecord('sys_trigger');
oldTrigger.addQuery('name', 'PostCloneSOP - Remote Checks Retry');
oldTrigger.query();
var deletedCount = 0;
while (oldTrigger.next()) {
deletedCount++;
oldTrigger.deleteRecord();
}
gs.info('Old retry triggers deleted: ' + deletedCount);
// Calculate runtime
var runTime = new GlideDateTime();
gs.info('Current time: ' + runTime.getDisplayValue());
// runTime.addMinutes(delayMinutes);
runTime.addSeconds(delayMinutes * 60);
gs.info('Next execution time (after delay): ' + runTime.getDisplayValue());
// Create new trigger
var triggerGr = new GlideRecord('sys_trigger');
triggerGr.initialize();
triggerGr.name = 'PostCloneSOP - Remote Checks Retry';
//triggerGr.next_action = runTime;
triggerGr.setValue('next_action',runTime.getValue());
triggerGr.script =
'new PostCloneSOP().addAgentBackToRemoteChecksWithRetry();';
var triggerSysId = triggerGr.insert();
gs.info('New sys_trigger created');
gs.info('Trigger SysId: ' + triggerSysId);
gs.info('Trigger Name: ' + triggerGr.name);
gs.info('Trigger next_action: ' + runTime.getDisplayValue());
gs.info('---------- _scheduleRetry END ----------');
},
type: 'PostCloneSOP'
};
As we can see difference in current and next_action time as 2 min but sys_trigger record not run and state is showing as RED in ready state.
Please suggest me how we can run this trigger as I need to run this 3 times but it not happening. Not sure what I am doing wrong.
Thanks in advance!
Brij
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8 hours ago
Hi @Brijmohan,
give it some time and refresh the list to see if the status has changed.
I believe that the red colour indicates that it is after it was supposed to be but was not yet started...
No AI was used in the writing of this post. Pure #GlideFather only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago
Hi @GlideFather ,
Thanks for your response. I found why it was not working. Trigger type was empty earlier. I added this value as Run once and executed then it worked without any issue.
Regards,
Brij
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago
perfect! @Brijmohan then accept your message above for solution to close this thread
No AI was used in the writing of this post. Pure #GlideFather only
