AnsweredAssumed Answered

Starting Workflow on a schedule

Question asked by srowsell on Aug 23, 2013
Latest reply on Sep 9, 2013 by srowsell
I have been trying this for a while, and gotten to the point I will describe below.

The following is a script which I intend to have run on a schedule (more on that below).  For debugging purposes I have also set it up as a rule which runs when I want it to run, but the end result is to have it run once per day:

var results=search.luceneSearch("+PATH:\"/app:company_home/st:sites/cm:contract-management/cm:documentLibrary/cm:ActiveContracts//*\" +TYPE:\"{duca.contracts.model}contract\" -ASPECT:\"{duca.contracts.model}inWorkflow\" -ASPECT:\"{duca.contracts.model}suppDoc\"");
var today=new Date();

for (var i=0;i<results.length;i++)
{   
   var expYear=(results.properties["contract:expirationDate"]).getFullYear();
   var expMonth=(results.properties["contract:expirationDate"]).getMonth();
   var expDate=(results.properties["contract:expirationDate"]).getDate();
   
   var startEvaluationPeriod = new Date(expYear,expMonth,expDate);
   startEvaluationPeriod.setDate(-results.properties["contract:noticeDays"]);
   
   if (today>startEvaluationPeriod)
   {
      var contractWorkflow = actions.create("start-workflow");
      contractWorkflow.parameters.workflowName="activiti$alfGroupReview";
      contractWorkflow.parameters["bpm:workflowDescription"] = "The attached contract has entered its review period.  It will expire in "+results.properties["contract:noticeDays"]+" days.";
      contractWorkflow.parameters["bpm:groupAssignee"] = people.getGroup("GROUP_ContractManagement");
      contractWorkflow.parameters["bpm:workflowDueDate"]=results.properties["contract:expirationDate"];
      contractWorkflow.execute(results);
      
      results.addAspect("contract:inWorkflow");
      results.save();   
   }
}

This script runs just fine if I comment out the lines within the if block, but I get the following exception in the alfresco.log if I don't:


11:45:53,159 ERROR [org.springframework.extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 07232130 Failed to execute script 'workspace://SpacesStore/57803310-97ce-4989-8a2e-ce22834d7615': null
org.alfresco.scripts.ScriptException: 07232130 Failed to execute script 'workspace://SpacesStore/57803310-97ce-4989-8a2e-ce22834d7615': null


This strikes me as a nonsense exception, since it's trying to tell me that the script it's trying to run doesn't exist.  But I am open to the possibility that I'm not interpreting it correctly.  (I can verify that the script is getting into the if block; I tried commenting out the workflow lines and putting in other instructions, and it executed those instructions.)

Once I have that fixed, my intention is to execute it once per day by editing the scheduled-action-services-context.xml as follows:


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
   
        <!–
    Define the model factory used to generate object models suitable for use with freemarker templates.
    –>
    <bean id="templateActionModelFactory" class="org.alfresco.repo.action.scheduled.FreeMarkerWithLuceneExtensionsModelFactory">
    <property name="serviceRegistry">
    <ref bean="ServiceRegistry"/>
    </property>
    </bean>

    <!–Execute the script /Company Home/Data Dictionary/Script/exampleScript.js –>
    <bean id="runScriptAction" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">
    <property name="actionName">
    <value>script</value>
    </property>
    <property name="parameterTemplates">
    <map>
    <entry>
    <key>
    <value>script-ref</value>
    </key>

    <!– Note that as of Alfresco 4.0, due to a Spring upgrade, the FreeMarker ${foo} entries must be escaped –>

    <value>\$\{selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:findExpiringContracts.js"' )\}</value>

    <!– This value tag is for Alfresco 3.x, because the escape of FreeMarker entries will fail otherwise –>

    <!–<value>#{"${"}selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:exampleScript.js"' )#{"}"}</value>–>

    </entry>
    </map>
    </property>
    <property name="templateActionModelFactory">
    <ref bean="templateActionModelFactory"/>
    </property>
    <property name="dictionaryService">
    <ref bean="DictionaryService"/>
    </property>
    <property name="actionService">
    <ref bean="ActionService"/>
    </property>
    <property name="templateService">
    <ref bean="TemplateService"/>
    </property>
    </bean>

    <!– Script execution config –>
    <bean id="runScript" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">
    <property name="transactionMode">
    <value>UNTIL_FIRST_FAILURE</value>
    </property>
    <property name="compensatingActionMode">
    <value>IGNORE</value>
    </property>
    <property name="searchService">
    <ref bean="SearchService"/>
    </property>
    <property name="templateService">
    <ref bean="TemplateService"/>
    </property>
    <property name="queryLanguage">
    <value>lucene</value>
    </property>
    <property name="stores">
    <list>
    <value>workspace://SpacesStore</value>
    </list>
    </property>
    <property name="queryTemplate">
    <value>PATH:"/app:company_home/cm:customSpace"</value>
    </property>
    <!–Cron script execution every day at Midnight–>
    <property name="cronExpression">
    <value>0 0/2 * * * ?</value>
    </property>
    <property name="jobName">
    <value>jobD</value>
    </property>
    <property name="jobGroup">
    <value>jobGroup</value>
    </property>
    <property name="triggerName">
    <value>triggerD</value>
    </property>
    <property name="triggerGroup">
    <value>triggerGroup</value>
    </property>
    <property name="scheduler">
    <ref bean="schedulerFactory"/>
    </property>
    <property name="actionService">
    <ref bean="ActionService"/>
    </property>
    <property name="templateActionModelFactory">
    <ref bean="templateActionModelFactory"/>
    </property>
    <property name="templateActionDefinition">
    <ref bean="runScriptAction"/> <!– This is name of the action (bean) that gets run –>
    </property>
    <property name="transactionService">
    <ref bean="TransactionService"/>
    </property>
    <property name="runAsUser">
    <value>System</value>
    </property>
    </bean>

    </beans>


Right now it's set to run every two minutes, for testing purposes, but of course it isn't working (if only because the script it's trying to run isn't working).

Can anyone here point out where I've gone wrong?

EDIT:  I have confirmed that it's the line:

contractWorkflow.execute(results);

which is objectionable.  Obviously the node at results exists; the aspect is being applied to it.  So there is somehow a problem in this line which is invisible to me.

EDIT 2:  I have also confirmed that the scheduled action is doing nothing.  I added in some instructions to the now working script – with the above line commented out – which would reveal whether or not the script is being executed, and it isn't.  I copied and modified the scheduled-action-services-context.xml file from several resources, and if it's not working, I really don't understand why.

EDIT 3:  The workflow is now working.  I didn't have the workflow "activiti$alfGroupReview" deployed.  There remains the scheduled action issue problem, if anyone has any hints they would like to share.

Outcomes