09-19-2016 04:59 AM - edited 03-01-2019 06:44 AM
Task Name | Schedule a workflow / Schedule a workflow for rollback by given SR ID |
Description | |
Prerequisites |
|
Category | Workflow |
Components | vSphere 5.x |
User Inputs | |
Output |
Instructions for Regular Workflow Use:
A big thank you goes to Tejeswar Sahu for the contribution.
This example will schedule a workflow which in turn will roll back a give service request ID:
The rollback workflow:
The scheduling workflow:
The run for the workflows:
The created schedule:
The schedule code in the custom task:
importPackage(com.cloupia.model.cIM);
importPackage(com.cloupia.service.cIM.inframgr.customactions);
importPackage(com.cloupia.fw.objstore);
importPackage(com.cloupia.feature.customactions);
importPackage(java.lang);
importPackage(java.util);
var SCHEDULE_HOURLY = 1;
var SCHEDULE_DAILY = 2;
var SCHEDULE_WEEKLY = 3;
var SCHEDULE_MONTHLY = 4;
var SCHEDULE_MINUTES = 5;
var RECURRENCE_NO_END = 1;
var RECURRENCE_NUMBER_OF_OCCURENCES = 2;
var RECURRENCE_END_BY_DATE = 3;
var RECURRENCE_ONLY_ONCE = 4;
var startTime = input.StartTime;
var frequency = input.FrequencyType;
var repeatCount = input.FrequencyInterval;
var recurrenceType = input.RecurrenceType;
var workflowName = input.workflowName;
var userId = input.UserID;
if(frequency == "HOURLY"){
frequency = SCHEDULE_HOURLY;
}else if(frequency == "DAILY"){
frequency = SCHEDULE_DAILY;
}else if(frequency == "WEEKLY"){
frequency = SCHEDULE_WEEKLY;
}else if(frequency == "MONTHLY"){
frequency = SCHEDULE_MONTHLY;
}else if(frequency == "MINUTES"){
frequency = SCHEDULE_MINUTES;
}
if(recurrenceType == "NO_END"){
recurrenceType = RECURRENCE_NO_END;
}else if(recurrenceType == "NUMBER_OF_OCCURENCES"){
recurrenceType = RECURRENCE_NUMBER_OF_OCCURENCES;
}else if(recurrenceType == "END_BY_DATE"){
recurrenceType = RECURRENCE_END_BY_DATE;
}else if(recurrenceType == "ONLY_ONCE"){
recurrenceType = RECURRENCE_ONLY_ONCE;
}
if(startTime <= System.currentTimeMillis())
{
throw new Exception("Start Time cannot be set to Past time");
}
var item = new Schedule();
var store = ObjStoreHelper.getStore(new CustomActionDefinition().getClass());
var list= store.query("name == '" + workflowName + "'");
var def = list.get(0);
var status;
status = WFSchedulerUtil.isWFScheduleExist(def.getId());
if(status){
throw new Exception("Workflow schedule already exists.");
}
var validator = new WFValidator(def);
validator.validate();
//long type
item.setStartTime(startTime);
//int type, Hourly, Daily,Weekly,Monthly,Minutes
item.setFrequency(frequency);
//int type
item.setRepeatCount(repeatCount);
//long type
var endTime = startTime + item.getFrequencyInMilliseconds();
item.setEndTime(endTime);
//int type, No End, Only Once,Fixed Numbr Of Times
item.setRecurrenceType(recurrenceType);
var scheduleId = WFSchedulerUtil.addSchedule(item);
//workflow Inputs
var wfInputDef = CustomActionUtil.getWorkflowInputFieldDefinitions(def.getId());
var inputIDs = [];
var inputValues = input.srId;
for(var i=0;i<wfInputDef.size();i++){
inputIDs[i] = wfInputDef.get(i).getName();
}
var map = new HashMap();
map.put(inputIDs[0],inputValues);
var inputSetId = CustomActionUtil.addWorkflowInputValues(map , def.getId(), "admin");
var itemData = new WFSchedulerData();
itemData.setScheduleId(item.getScheduleId());
itemData.setUserId(userId);
itemData.setScheduleId(scheduleId);
itemData.setSchedule(item);
itemData.setActionId(def.getId());
itemData.setInputSetId(inputSetId);
WFSchedulerUtil.addWFSchedulerData(itemData);
logger.addInfo("workflow task "+workflowName+" Scheduled successfully.");
Please Note:
//As there is no functionality available to add dynamically all workflow input but I have generalized the code as shown in below snipped
//If you need to add more inputs then you can do it by adding map value as shown below
var inputIDs = [];
var inputValues = input.srId;
for(var i=0;i<wfInputDef.size();i++){
inputIDs[i] = wfInputDef.get(i).getName();
}
var map = new HashMap();
map.put(inputIDs[0],inputValues_1);
.
.
.
map.put(inputIDs[1],inputValues_2);
map.put(inputIDs[2],inputValues_3);
As of now I have updated the workflow with single workflow input only
Caveat Note!!!
Limitations with scheduling, only have one “active” schedule per workflow!
Ticket is in place to get this fixed.
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: