The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to determine if a workflow is active from script

lyletaylor
Giga Contributor

We have a scenario where discovery is triggering an orchestration workflow to fix a particular issue if it is found during discovery. However, for some reason it gets triggered more than once which is causing issues due to a number of instances of the workflow running at the same time. Now, I know I need to determine why it's trying to kick it off more than once and fix that, but I also want to update the workflow so that it will simply exit if there is already another instance of that workflow running for that server. That way, I will never have more than one instance of the workflow trying to run the same actions on the same server at once.

So, what I'm trying to determine is how I can determine if there is already another active workflow context for that same server. I know I can query for active workflow contexts, but the key is that I need to determine if there is already one started for that particular server. So, the key in the query is effectively that the context is for that workflow and that an input variable to the workflow is set to the server name.

Anyone know a good way to do that?

Thanks,Lyle

3 REPLIES 3

Raju Koyagura
Tera Guru

How about writing a BR on workflow context to restrict the duplicate execution? Something like below...



(function executeRule(current, previous /*null when async*/) {


var gr = new GlideRecord("wf_context");


gr.addQuery("workflow_version", current.workflow_version);


gr.addQuery("id", current.id);


gr.addQuery("state", "executing");


gr.query();


if (gr.next()) {


current.state="cancelled";


//current.setAbortAction(true);


}






})(current, previous);


HI Raju,



Thanks for your answer. Unfortunately that doesn't do what I need. It's not that I can't have more than one instance of the workflow running at a time, it's that I can't have more than one instance per server that it's running for.



The way it works is that you pass in the name of the server as an input parameter, and then it performs actions on that server. It's valid to have it running on more than one server at a time, but not more than once at a time on any given server. So I need to be able to do a query like what you did above, but taking the value of the input parameter into account. That's what I'm not sure how to do correctly.



Thanks,
Lyle


lyletaylor
Giga Contributor

Well, I've come up with this potential solution that appears to work, but is still suboptimal. Basically it just finds all active contexts for the workflow and then queries the variables table for variables tied to those contexts to see if any of them have the value of the server in question.



I'm still open to a better solution if anyone knows one.



Thanks,


Lyle




var serverName = '<some_server_name>';


var alreadyExecuting = false;



var con = new GlideRecord('wf_context');


con.addActiveQuery();


con.addQuery('workflow_version.name', '<workflow_name>');


con.query();



var contexts = [];



while (con.next()) {


  contexts.push(con.sys_id.toString());


}



var vars = new GlideRecord('sys_variable_value');


vars.addQuery('document', 'wf_context');


vars.addQuery('document_key', contexts);


vars.addQuery('variable.element', 'u_servername');


vars.addQuery('value', serverName);


vars.query();



if (vars.hasNext()) {


  alreadyExecuting = true;


}



if (alreadyExecuting) {


  gs.print('The workflow is already executing for server: ' + serverName);


} else {


  gs.print('The workflow is NOT executing for server: ' + serverName);    


}