AnsweredAssumed Answered

shell service tasks not showing in runtime tables

Question asked by aoliver on Jun 19, 2012
Latest reply on Jun 29, 2012 by aoliver
Hello,
I am an activiti noob and am having issues with a Shell Task implementation.  Since Shell Tasks are not available in the Activiti Designer yet, i created a Service Task (Java Delegate) that mimics the Shell Task Behavior.  These Shell Tasks are running BI processes and take several minutes.  I'm using the Activiti REST API to invoke the workflows.  Activiti-Explorer and Activiti-REST are both configured to connect to an Oracle database.

3 Questions:
1) While the Shell tasks are running, i see no information in the ACT_RU_EXECUTION table.  I would expect to see my process instance row there while these tasks are running.
2) How can i get the  REST call to return once the workflow is invoked instead of returning after the workflow has completed?
3) In Activiti Explorer, when i run the attached workflow, no other activity is allowed in the activiti-explorer app.  I am even unable to log into another session.  Why is Activiti Explorer locking up like this?


Here is a snippet of the service task xml:
    <serviceTask id="NetGStageServiceTask" name="NetGStageService Task" activiti:class="com.scrippsnetworks.ibi.revenueWorkflows.serviceTasks.ShellServiceTask">
      <extensionElements>
        <activiti:field name="command">
          <activiti:expression>${ShellScriptRoot}/ADSD/adsd_staging${ShellScriptSuffix}</activiti:expression>
        </activiti:field>
        <activiti:field name="wait">
          <activiti:string>true</activiti:string>
        </activiti:field>
        <activiti:field name="outputVariable">
          <activiti:string>netGStageStatus</activiti:string>
        </activiti:field>
        <activiti:field name="errorCodeVariable">
          <activiti:string>netGStageReturnCode</activiti:string>
        </activiti:field>
        <activiti:field name="arg1">
          <activiti:expression>${DartRunDate}</activiti:expression>
        </activiti:field>
      </extensionElements>
    </serviceTask>


Here is the java delegate code:
public class ShellServiceTask implements JavaDelegate {

   final static Logger logger = Logger.getLogger(ShellServiceTask.class.getName());

   protected Expression command;
   protected Expression wait;
   protected Expression arg1;
   protected Expression arg2;
   protected Expression arg3;
   protected Expression arg4;
   protected Expression arg5;
   protected Expression outputVariable;
   protected Expression errorCodeVariable;
   protected Expression redirectError;
   protected Expression cleanEnv;
   protected Expression directory;

   String commandStr;
   String arg1Str;
   String arg2Str;
   String arg3Str;
   String arg4Str;
   String arg5Str;
   String waitStr;
   String resultVariableStr;
   String errorCodeVariableStr;
   Boolean waitFlag;
   Boolean redirectErrorFlag;
   Boolean cleanEnvBoolan;
   String directoryStr;

   @Override
   public void execute(DelegateExecution execution) throws Exception {
      logger.info("In " + command.getValue(execution) + " ShellTask script with variables = " + execution.getVariables());
      executeScript(execution);
      // create a new value for execution
      
      logger.info("Leaving " + command.getValue(execution) + " ShellTask script with variables = " + execution.getVariables());

   }

   private void readFields(DelegateExecution execution) {
       commandStr = getStringFromField(command, execution);
       arg1Str = getStringFromField(arg1, execution);
       arg2Str = getStringFromField(arg2, execution);
       arg3Str = getStringFromField(arg3, execution);
       arg4Str = getStringFromField(arg4, execution);
       arg5Str = getStringFromField(arg5, execution);
       waitStr = getStringFromField(wait, execution);
       resultVariableStr = getStringFromField(outputVariable, execution);
       errorCodeVariableStr = getStringFromField(errorCodeVariable, execution);

       String redirectErrorStr = getStringFromField(redirectError, execution);
       String cleanEnvStr = getStringFromField(cleanEnv, execution);

       waitFlag = waitStr == null || waitStr.equals("true");
       redirectErrorFlag = redirectErrorStr != null && redirectErrorStr.equals("true");
       cleanEnvBoolan = cleanEnvStr != null && cleanEnvStr.equals("true");
       directoryStr = getStringFromField(directory, execution);
   }

   public String toString() {
      return "command="+command+", arg1="+arg1+", arg2="+arg2+", arg3="+arg3+", arg4="+arg4+", arg5="+arg5+
            ", wait="+wait+", outputVariable="+outputVariable+", errorCodeVariable="+errorCodeVariable;
   }
   
   public void executeScript(DelegateExecution execution) {
      logger.info("starting execute");
       
      readFields(execution);
       
      List<String> argList = new ArrayList<String>();
         argList.add(commandStr);
       
         if (arg1Str != null)
            argList.add(arg1Str);
         if (arg2Str != null)
            argList.add(arg2Str);
         if (arg3Str != null)
            argList.add(arg3Str);
         if (arg4Str != null)
            argList.add(arg4Str);
         if (arg5Str != null)
            argList.add(arg5Str);
       
         ProcessBuilder processBuilder = new ProcessBuilder(argList);
       
         try {
            processBuilder.redirectErrorStream(redirectErrorFlag);
            if (cleanEnvBoolan) {
               Map<String, String> env = processBuilder.environment();
               env.clear();
            }
          
            if (directoryStr != null && directoryStr.length() > 0)
               processBuilder.directory(new File(directoryStr));
          
               Process process = processBuilder.start();
          
               if (waitFlag) {
                  int errorCode = process.waitFor();
             
                  if (resultVariableStr != null) {
                     String result = convertStreamToStr(process.getInputStream());
                     execution.setVariable(resultVariableStr, result);
                  }
             
                  if (errorCodeVariableStr != null) {
                     execution.setVariable(errorCodeVariableStr, Integer.toString(errorCode));
                  }

               }
         } catch (Exception e) {
            logger.info("Could not execute shell command with error = " + e);
            throw new ActivitiException("Could not execute shell command" , e);
         }

   }
      
   public static String convertStreamToStr(InputStream is) throws IOException {
      if (is != null) {
         Writer writer = new StringWriter();
         char[] buffer = new char[1024];
         try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
               writer.write(buffer, 0, n);
            }
         } finally {
            is.close();
         }
         return writer.toString();
      } else {
         return "";
      }
   }

   protected String getStringFromField(Expression expression, DelegateExecution execution) {
      if (expression != null) {
         Object value = expression.getValue(execution);
         if (value != null) {
            return value.toString();
         }
      }
      return null;
   }
}


What can i change to solve these 3 issues?

thanks

Outcomes