Package sos.spooler

Examples of sos.spooler.Subprocess


    /**
     * Create and execute subprocess
     */
    public Subprocess executeSubprocess(String command, String commandParameters, HashMap environment) throws Exception {
   
        Subprocess subprocess = null;
        boolean terminated = true;
       
        try {
            subprocess = spooler_task.create_subprocess();
           
            // execute subprocesses as a process group to have all child processes being killed if the timeout is exceeded 
            subprocess.set_own_process_group(true);
           
            subprocess.set_ignore_error(this.isIgnoreError());
            subprocess.set_ignore_signal(this.isIgnoreSignal());
            subprocess.set_priority_class(this.getPriorityClass());
            if (this.getTimeout() > 0) subprocess.set_timeout(this.getTimeout());

            // execute the command and parameters in background
            String commandLine = command + " " + commandParameters;
           
            // hand all order parameters that start with "env_" or "environment_" as environment variables to the subprocess
            String[] parameterNames = this.getParameters().names().split(";");
            for(int i=0; i<parameterNames.length; i++) {
                /*if (parameterNames[i].startsWith("env_")) {
                    subprocess.set_environment(parameterNames[i].substring(4).toUpperCase(), this.getParameters().value(parameterNames[i]));
                } else if (parameterNames[i].startsWith("environment_")) {
                    subprocess.set_environment(parameterNames[i].substring(12).toUpperCase(), this.getParameters().value(parameterNames[i]));
                }*/
                commandLine = myReplaceAll(commandLine,"\\$\\{" + parameterNames[i] + "\\}", this.getParameters().value(parameterNames[i]).replaceAll("[\\\\]", "\\\\\\\\"));
            }
            
 
            // set specific environment variables
            subprocess.set_environment("SCHEDULER_TRIGGER_FILE", this.getTriggerFilename());
           
            if (environment != null) {
               Iterator envIterator = environment.keySet().iterator();
                while(envIterator.hasNext()) {
                   Object envName  = envIterator.next();
                   Object envValue = environment.get(envName.toString());
                   commandLine = myReplaceAll(commandLine,"\\$\\{" + envName.toString() + "\\}", envValue.toString().replaceAll("[\\\\]", "\\\\\\\\"));
               }
            }
           
            // operating system environment variables
            if (this.envvars != null) {
              Iterator envIterator = this.envvars.keySet().iterator();
               while(envIterator.hasNext()) {
                  Object envName  = envIterator.next();
                  Object envValue = this.envvars.get(envName.toString());
                  commandLine = myReplaceAll(commandLine,"\\$\\{" + envName.toString() + "\\}", envValue.toString().replaceAll("[\\\\]", "\\\\\\\\"));
              }
           }
                      
            // adding environment variables from parameters with attribute env=yes
            if (this.additional_envvars != null) {
              Iterator envIterator = this.additional_envvars.keySet().iterator();
               while(envIterator.hasNext()) {
                  String envName  = (String) envIterator.next();
                  String envValue = (String) this.additional_envvars.get(envName);
                  if (envName == null) continue;
                  int varBegin = envValue.indexOf("${");
                  while (varBegin > -1) {
                      int varEnd = envValue.indexOf("}", varBegin+2);
                      if (varEnd > 0) {
                          String varName = envValue.substring(varBegin+2, varEnd);
                          boolean hasBasename = varName.startsWith("basename:");
                          if (hasBasename) varName = varName.substring(9);
                          if (this.getParameters().value(varName) != null) {
                              if (hasBasename) {
                                  envValue = myReplaceAll(envValue, "\\$\\{basename:" + varName + "\\}", new File(this.getParameters().value(varName)).getName().replaceAll("[\\\\]", "\\\\\\\\"));
                              } else {
                                  envValue = myReplaceAll(envValue, "\\$\\{" + varName + "\\}", this.getParameters().value(varName).replaceAll("[\\\\]", "\\\\\\\\"));
                              }
                              this.getLogger().debug9("environment variable substituted: " + varName);
                          } else {
                              this.getLogger().info("unsubstitutable variable found for environment: " + varName);
                          }
                      }
                      varBegin = envValue.indexOf("${", varEnd+1);
                  }
                  this.getLogger().debug1(".. setting environment variable: "  + envName + "=" + envValue);
                  subprocess.set_environment(envName, envValue);
              }
           }
           
           
            // execute the command
            this.getLogger().info("executing command: " + commandLine);
            subprocess.start(commandLine);

            // wait for the specified timeout for termination of the subprocess
            if (this.getTimeout() > 0) {
                terminated = subprocess.wait_for_termination(this.getTimeout());
            } else {
                subprocess.wait_for_termination();
            }
            if (!terminated){
                this.getLogger().warn("timeout reached for subprocess, process will be terminated");
                subprocess.kill();
                subprocess.wait_for_termination();
            } else {
            }
           
            boolean stdErrEmpty = true;
            String stdErrString = "";
            String stdOutString = "";

            this.getLogger().info("output reported to stdout for " + commandLine + ":");
            while(this.stdoutStream != null && this.stdoutStream.ready()) {
                String stdOutLine = stdoutStream.readLine();
                this.getLogger().info(stdOutLine);
                stdOutString += stdOutLine + "\n";
            }

            this.getLogger().info("output reported to stderr for " + commandLine + ":");
            while(this.stderrStream != null && this.stderrStream.ready()) {
                String stdErrLine = stderrStream.readLine();
                this.getLogger().info(stdErrLine);
                if (stdErrLine.trim().length()>0) stdErrEmpty = false;
                stdErrString += stdErrLine + "\n";
            }
            if (spooler_job.order_queue() != null){
                spooler_task.order().params().set_var("scheduler_order_stderr_output", stdErrString);
                spooler_task.order().params().set_var("scheduler_order_stdout_output", stdOutString);
                spooler_task.order().params().set_var("scheduler_order_exit_code", String.valueOf(subprocess.exit_code()));
                spooler_task.order().params().set_var("scheduler_order_terminated", (terminated ? "true" : "false"));
            }
           
            if((subprocess.exit_code() != 0)){
                if (this.isIgnoreError()) this.getLogger().info("command terminated with exit code: " + subprocess.exit_code());
                else throw new Exception("command terminated with exit code: " + subprocess.exit_code());                   
            }
            if((subprocess.termination_signal() != 0)){
                if (this.isIgnoreSignal()) this.getLogger().info("command terminated with signal: " + subprocess.termination_signal());
                else throw new Exception("command terminated with signal: " + subprocess.termination_signal());                 
            }
            if(!this.isIgnoreStderr() && !stdErrEmpty) {
                throw new Exception("command terminated with output to stderr:\n" + stdErrString);
            }
                       
View Full Code Here


    public boolean spooler_process() {

        Order order = null;
        String orderId = "(none)";
        Subprocess subprocess = null;
       
        try {

            try {
       
                 this.setLogger(new SOSSchedulerLogger(spooler_log));
                if (spooler_job.order_queue() != null) {
                    order = spooler_task.order();
                    orderId = order.id();
               
                    if (order.params().value("configuration_path") != null && order.params().value("configuration_path").length() > 0) {
                        this.setConfigurationPath(order.params().value("configuration_path"));
                    } else if (spooler_task.params().value("configuration_path") != null && spooler_task.params().value("configuration_path").length() > 0) {
                        this.setConfigurationPath(spooler_task.params().value("configuration_path"));
                    }

                    if (order.params().value("configuration_file") != null && order.params().value("configuration_file").length() > 0) {
                        this.setConfigurationFilename(order.params().value("configuration_file"));
                    } else if (spooler_task.params().value("configuration_file") != null && spooler_task.params().value("configuration_file").length() > 0) {
                        this.setConfigurationFilename(spooler_task.params().value("configuration_file"));
                    }

                    // load and assign configuration
                    this.initConfiguration();
                }

                // prepare parameters and attributes
                this.prepare();

            } catch (Exception e) {
                throw new Exception("error occurred preparing order: " + e.getMessage());
            }
           

            try { // to process order
                if (this.getCommand() == null || this.getCommand().length() == 0)
                    throw new Exception("no command was specified to process order");
               
                subprocess = this.executeSubprocess();

            } catch (Exception e) {
                throw new Exception(e.getMessage());
            }
           
            return (spooler_task.job().order_queue() != null) ? true : false;
           
        } catch (Exception e) {
            spooler_log.warn("error occurred processing order [" + orderId + "]: " + e.getMessage());
            return false;
        } finally {
            try { this.cleanup(); } catch (Exception e) {};
            if (subprocess != null) try { subprocess.close(); } catch (Exception e) {}
        }
    }
View Full Code Here

      String[] commands = command.split("\n");
      getLogger().debug6("Found " + commands.length + " commands.");
      // neu: mit subprocess

      for (int i = 0; i < commands.length && !timedOut; i++) {
        Subprocess subProc = spooler_task.create_subprocess();
        subProc.set_own_process_group(ownProcessGroup);
        subProc.set_ignore_error(ignoreError);
        subProc.set_ignore_signal(ignoreSignal);
        subProc.set_priority_class(priorityClass);
        try {
          setEnvironment(orderPayload, subProc);
        }
        catch (Exception e) {
          throw new Exception("Error occured setting environment variables: " + e);
        }
        // execute interpreter
        if (program != null && program.length() > 0) {
          subProc.start(program + " " + commands[i]);
          spooler_log.info("executing \"" + program + " " + commands[i] + "\"");
        }
        else {
          subProc.start(commands[i]);
          spooler_log.info("executing \"" + commands[i] + "\"");
        }
        if (orderPayload != null && orderPayload.var("timeout") != null && orderPayload.var("timeout").toString().length() > 0
            && !orderPayload.var("timeout").toString().equals("0")) {
          spooler_log.info("executable file is launched with process id " + subProc.pid() + " for timeout in "
              + orderPayload.var("timeout").toString() + "s");
          boolean terminated = subProc.wait_for_termination(Double.parseDouble(orderPayload.var("timeout").toString()));
          if (!terminated) {
            spooler_log.info("timeout reached, process will be terminated.");
            subProc.kill();
            subProc.wait_for_termination();
            timedOut = true;
          }

        }
        else {
          spooler_log.info("executable file is launched with process id " + subProc.pid());
          subProc.wait_for_termination();

        }
        if (!timedOut)
          spooler_log.info("file executed");
        spooler_log.debug9("Exit code: " + subProc.exit_code());

        boolean stdErrEmpty = true;
        String stdErrString = "";
        String stdOutString = "";
        spooler_log.info("std_out for " + commands[i] + ":");
        while (stdoutStream != null && stdoutStream.ready()) {
          String stdOutLine = stdoutStream.readLine();
          spooler_log.info(stdOutLine);
          stdOutString += stdOutLine + "\n";
        }
        spooler_log.info("std_err for " + commands[i] + ":");

        while (stderrStream != null && stderrStream.ready()) {
          String stdErrLine = stderrStream.readLine();
          spooler_log.info(stdErrLine);
          if (stdErrLine.trim().length() > 0)
            stdErrEmpty = false;
          stdErrString += stdErrLine + "\n";
        }
        if (orderJob && order != null) {
          Variable_set realOrderPayload = order.params();
          SetVar(realOrderPayload, conStd_err_output, stdErrString);
          SetVar(realOrderPayload, conStd_out_output, stdOutString);
          SetVar(realOrderPayload, conExit_code, "" + subProc.exit_code());
          SetVar(realOrderPayload, "timed_out", "" + timedOut);
          // for compatibility with SubProcessJob
          SetVar(realOrderPayload, "scheduler_order_terminated", (!timedOut ? "true" : "false"));
          replaceAliases(realOrderPayload, outputParameterAliases);
        } // additionally set task parameters for use with copy-from:
        Variable_set taskParams = spooler_task.params();
        SetVar(taskParams, conStd_err_output, stdErrString);
        SetVar(taskParams, conStd_out_output, stdOutString);
        SetVar(taskParams, conExit_code, "" + subProc.exit_code());
        SetVar(taskParams, "timed_out", "" + timedOut);
        replaceAliases(taskParams, outputParameterAliases);

        if (timedOut && !ignoreTimeout) {
          throw new Exception("Process had to be killed because of timeout");
        }
        if ((subProc.exit_code() != 0)) {
          if (ignoreError)
            spooler_log.info("Command terminated with exit code: " + subProc.exit_code());
          else
            throw new Exception("Command terminated with exit code: " + subProc.exit_code());
        }
        if ((subProc.termination_signal() != 0)) {
          if (ignoreSignal)
            spooler_log.info("Command terminated with signal: " + subProc.termination_signal());
          else
            throw new Exception("Command terminated with signal: " + subProc.termination_signal());
        }
        if (!ignoreStderr && !stdErrEmpty) {
          throw new Exception("Command terminated with text in stderr:\n" + stdErrString);
        }
View Full Code Here

    /**
     * Create and execute subprocess
     */
    public Subprocess executeSubprocess(String command, String commandParameters, HashMap environment) throws Exception {
   
        Subprocess subprocess = null;
        boolean terminated = true;
       
        try {
            subprocess = spooler_task.create_subprocess();
           
            // execute subprocesses as a process group to have all child processes being killed if the timeout is exceeded 
            subprocess.set_own_process_group(true);
           
            subprocess.set_ignore_error(this.isIgnoreError());
            subprocess.set_ignore_signal(this.isIgnoreSignal());
            subprocess.set_priority_class(this.getPriorityClass());
            if (this.getTimeout() > 0) subprocess.set_timeout(this.getTimeout());

            // execute the command and parameters in background
            String commandLine = command + " " + commandParameters;
           
            // hand all order parameters that start with "env_" or "environment_" as environment variables to the subprocess
            String[] parameterNames = this.getParameters().names().split(";");
            for(int i=0; i<parameterNames.length; i++) {
                /*if (parameterNames[i].startsWith("env_")) {
                    subprocess.set_environment(parameterNames[i].substring(4).toUpperCase(), this.getParameters().value(parameterNames[i]));
                } else if (parameterNames[i].startsWith("environment_")) {
                    subprocess.set_environment(parameterNames[i].substring(12).toUpperCase(), this.getParameters().value(parameterNames[i]));
                }*/
                commandLine = myReplaceAll(commandLine,"\\$\\{" + parameterNames[i] + "\\}", this.getParameters().value(parameterNames[i]).replaceAll("[\\\\]", "\\\\\\\\"));
            }
            
 
            // set specific environment variables
            subprocess.set_environment("SCHEDULER_TRIGGER_FILE", this.getTriggerFilename());
           
            if (environment != null) {
               Iterator envIterator = environment.keySet().iterator();
                while(envIterator.hasNext()) {
                   Object envName  = envIterator.next();
                   Object envValue = environment.get(envName.toString());
                   commandLine = myReplaceAll(commandLine,"\\$\\{" + envName.toString() + "\\}", envValue.toString().replaceAll("[\\\\]", "\\\\\\\\"));
               }
            }
           
            // operating system environment variables
            if (this.envvars != null) {
              Iterator envIterator = this.envvars.keySet().iterator();
               while(envIterator.hasNext()) {
                  Object envName  = envIterator.next();
                  Object envValue = this.envvars.get(envName.toString());
                  commandLine = myReplaceAll(commandLine,"\\$\\{" + envName.toString() + "\\}", envValue.toString().replaceAll("[\\\\]", "\\\\\\\\"));
              }
           }
                      
            // adding environment variables from parameters with attribute env=yes
            if (this.additional_envvars != null) {
              Iterator envIterator = this.additional_envvars.keySet().iterator();
               while(envIterator.hasNext()) {
                  String envName  = (String) envIterator.next();
                  String envValue = (String) this.additional_envvars.get(envName);
                  if (envName == null) continue;
                  int varBegin = envValue.indexOf("${");
                  while (varBegin > -1) {
                      int varEnd = envValue.indexOf("}", varBegin+2);
                      if (varEnd > 0) {
                          String varName = envValue.substring(varBegin+2, varEnd);
                          boolean hasBasename = varName.startsWith("basename:");
                          if (hasBasename) varName = varName.substring(9);
                          if (this.getParameters().value(varName) != null) {
                              if (hasBasename) {
                                  envValue = myReplaceAll(envValue, "\\$\\{basename:" + varName + "\\}", new File(this.getParameters().value(varName)).getName().replaceAll("[\\\\]", "\\\\\\\\"));
                              } else {
                                  envValue = myReplaceAll(envValue, "\\$\\{" + varName + "\\}", this.getParameters().value(varName).replaceAll("[\\\\]", "\\\\\\\\"));
                              }
                              this.getLogger().debug9("environment variable substituted: " + varName);
                          } else {
                              this.getLogger().info("unsubstitutable variable found for environment: " + varName);
                          }
                      }
                      varBegin = envValue.indexOf("${", varEnd+1);
                  }
                  this.getLogger().debug1(".. setting environment variable: "  + envName + "=" + envValue);
                  subprocess.set_environment(envName, envValue);
              }
           }
           
           
            // execute the command
            this.getLogger().info("executing command: " + commandLine);
            subprocess.start(commandLine);

            // wait for the specified timeout for termination of the subprocess
            if (this.getTimeout() > 0) {
                terminated = subprocess.wait_for_termination(this.getTimeout());
            } else {
                subprocess.wait_for_termination();
            }
            if (!terminated){
                this.getLogger().warn("timeout reached for subprocess, process will be terminated");
                subprocess.kill();
                subprocess.wait_for_termination();
            } else {
            }
           
            boolean stdErrEmpty = true;
            String stdErrString = "";
            String stdOutString = "";

            this.getLogger().info("output reported to stdout for " + commandLine + ":");
            while(this.stdoutStream != null && this.stdoutStream.ready()) {
                String stdOutLine = stdoutStream.readLine();
                this.getLogger().info(stdOutLine);
                stdOutString += stdOutLine + "\n";
            }

            this.getLogger().info("output reported to stderr for " + commandLine + ":");
            while(this.stderrStream != null && this.stderrStream.ready()) {
                String stdErrLine = stderrStream.readLine();
                this.getLogger().info(stdErrLine);
                if (stdErrLine.trim().length()>0) stdErrEmpty = false;
                stdErrString += stdErrLine + "\n";
            }
            if (spooler_job.order_queue() != null){
                spooler_task.order().params().set_var("scheduler_order_stderr_output", stdErrString);
                spooler_task.order().params().set_var("scheduler_order_stdout_output", stdOutString);
                spooler_task.order().params().set_var("scheduler_order_exit_code", String.valueOf(subprocess.exit_code()));
                spooler_task.order().params().set_var("scheduler_order_terminated", (terminated ? "true" : "false"));
            }
           
            if((subprocess.exit_code() != 0)){
                if (this.isIgnoreError()) this.getLogger().info("command terminated with exit code: " + subprocess.exit_code());
                else throw new Exception("command terminated with exit code: " + subprocess.exit_code());                   
            }
            if((subprocess.termination_signal() != 0)){
                if (this.isIgnoreSignal()) this.getLogger().info("command terminated with signal: " + subprocess.termination_signal());
                else throw new Exception("command terminated with signal: " + subprocess.termination_signal());                 
            }
            if(!this.isIgnoreStderr() && !stdErrEmpty) {
                throw new Exception("command terminated with output to stderr:\n" + stdErrString);
            }
                       
View Full Code Here

TOP

Related Classes of sos.spooler.Subprocess

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.