Scheduled change task based on the maintenance schedule next day

Wybren1
Tera Guru

Hi developers,

The following question:

I want to create a (standard)change based on a existing maintenance schedules that's coming the next day.


More information:
- We have CI's with maintenance schedule. (Patch management)
- I want to create a change IF a maintenance schedule is coming up in the following ~12/24 hours.
- Each day a daily scheduled job or planned maintenance (not sure which is best) is checking if I have a maintenance window in the following 12 hours then create a 'standard change'.
*Eventually creating a standard change with the maintenance window and CI's attached.

Maintenance schedules are based on 'Every first/second/third monday of the month' etc.
Worth mentioning is that there are multiple Maintenance Schedules during a week maybe even the day itself.

Any idea what's the best approach for this?
I've been trying out scheduled jobs. But I cannot seem to find a way to query the 'start date'/'end date' within the cmn_schedules_span, since this is in the past.
Creating a scheduled job that's exactly the same as the maintenance window seems kind off pointless to me. (For instance creating a scheduled job that run every first monday etc)

1 ACCEPTED SOLUTION

ginesdoleratorm
ServiceNow Employee
ServiceNow Employee

Hi Wybren,



you can make use of the GlideSchedule API and its "whenNext()" method to easily get when the next span in your schedule is due to.


For example


var sysId = 'type_your_schedule_sys_id_here'


var schedule = new GlideSchedule(sysId);


var whenNext = schedule.whenNext();


gs.log("Next maintenance window starts in: " + whenNext + " milliseconds");


gs.log("That is: " + (whenNext/(1000*60*60)) + " hours");



So you can add a schedule jobs to run every (half a day) and check if a maintenance schedule starts in the following x hours.


So for example, adding in your script:


...


if ( (whenNext/(1000*60*60)) < 24){


    // maintenance starts in 24 hours ...


}



That's the simplest but effective thing you can do.



Instead, if you require more precision, like for example, I want to do something exactly 24 hours before the maintenance, you can run an script periodically which, in turn, creates schedule triggers to timeout exactly 24 hours before the maintenance period for each of the maintenance windows. When the schedule triggers timeout, then they should run the script which actually create your change.



The logic would be something like.


- Add a schedule job which run every 1 week.


- The schedule job script needs to get all the spans of your maintenance schedules from tomorrow to the next week + 1 day.


    To do that, you can make use of the method getSpans(start, end), also from the GlideSchedule API.


- For each span, create a schedule trigger (sys_trigger table), with the date of the span minus 24 hours.


    This will then trigger exactly 24 hours before the maintenance period.


    In the script of the schedule trigger you should therefore the change creation, or any other action you want to take.



Hope it helps,


Ginés.


View solution in original post

5 REPLIES 5

I am well aware of the token being a pain for other integrations, but given the above example is using the 'basic authentication'. So it should not have any problem in creating changes, be sure the user has 'permissions' on the standard change itself (user criteria)

We are running in Madrid without any problem creating standard changes.