13.3. Submitting and Monitoring Jobs

Engineering Knowledge Manager

Administration Guide | Chapter 13: EKM Client API |

13.2.10. Miscellaneous Operations |  | 13.4. Building and Running a Java Application
Prev |  | Next

## 13.3. Submitting and Monitoring Jobs

The library provides operations for running and monitoring jobs. The examples below show the basic operations.

Java

      JobManager rjm = conn.getJobManager();

      //set this path to one that contains commands.xml and SecInput.txt
      String jobFilesPath = "C:\\temp\\job_files";

      //set this path to where the job will run
      String clientDirectoryPath = "C:\\temp\\job_dir";

      String jobType = "TEST";

      File markerFile = File.createTempFile("job.", ".marker", new File(clientDirectoryPath));
      String markerFileName = markerFile.getName();
      String markerFilePath = markerFile.getAbsolutePath();

      Variant jobSpaceCreationResult = rjm.CreateJobSpace(clientDirectoryPath, markerFileName,
      UUID.randomUUID().toString(), new ArrayList<String>());
      String jobSpaceId = jobSpaceCreationResult.getMap().get("jobSpaceId").getString();

      System.out.println("Job created: " + jobSpaceId);

      boolean clientWorkDirVisible = jobSpaceCreationResult.getMap().get("clientWorkDirVisible").getBoolean();
      System.out.println("client working directory " + (clientWorkDirVisible ? "IS" : "is NOT") + " visible.");

      //do an upload if the client working directory is not visible to RSM...
      if (!clientWorkDirVisible)
      {
          String sourceLocalFilePath = jobFilesPath + File.separator + "commands.xml";
          String destinationRelativeFilePath = "commands.xml";
          System.out.println("uploading " + sourceLocalFilePath + " to " + destinationRelativeFilePath + ".");
          rjm.UploadFile(jobSpaceId, sourceLocalFilePath, destinationRelativeFilePath);
          sourceLocalFilePath = jobFilesPath + File.separator + "SecInput.txt";
          destinationRelativeFilePath = "SecInput.txt";
          System.out.println("uploading " + sourceLocalFilePath + " to " + destinationRelativeFilePath + ".");
          rjm.UploadFile(jobSpaceId, sourceLocalFilePath, destinationRelativeFilePath);
      }
      else
      {
          org.apache.commons.io.FileUtils.copyFile(new File(jobFilesPath + File.separator + "commands.xml"),
                  new File(clientDirectoryPath + File.separator + "commands.xml"));
          org.apache.commons.io.FileUtils.copyFile(new File(jobFilesPath + File.separator + "SecInput.txt"),
                  new File(clientDirectoryPath + File.separator + "SecInput.txt"));
      }

      String configQueueName = "Local";
      System.out.println("Submitting job to ConfiguredQueue: " + configQueueName);

      RsmSubmitData submitData = new RsmSubmitData();
      submitData.AutomaticInputs = true;
      submitData.AutomaticOutputs = true;
      submitData.ClientDirectory = clientDirectoryPath;
      submitData.CommandsXmlFileName = "commands.xml";
      submitData.DisplayName = "MyJob";
      submitData.JobType = jobType;
      submitData.ConfiguredQueue = configQueueName;
      String uniqueCallId = UUID.randomUUID().toString();

      List<String> patterns = new ArrayList<String>();
      patterns.add("force.out");
      String submitReturn = rjm.SubmitJob(uniqueCallId, submitData.serialize(), jobSpaceId, jobType, patterns);

      WebResult<String> webResult = AbstractSerializableData.deSerialize(submitReturn,
      new TypeToken<WebResult<String>>() {});

      String batchJobId = processWebResult(webResult);
      if (submitReturn != null)
      {
          System.out.println("Batch Job Id = " + batchJobId);
      }
      else
      {
          System.out.println("SubmitJob Result is null.");
      }

      // Check the status of the job every 5 seconds...
      JobStatus jobStatus = JobStatus.Unknown;
      do
      {
          String statusStr = rjm.GetJobStatus(uniqueCallId, jobSpaceId, batchJobId);
          WebResult<JobState> jobStateResult = AbstractSerializableData.deSerialize(statusStr,
          new TypeToken<WebResult<JobState>>() {});
          JobState jobState = processWebResult(jobStateResult);
          int theStatus = jobState.getJobStatus();
          jobStatus = JobStatus.getAsEnum(theStatus);

          System.out.println("=============================");
          System.out.println("GetJobStatus: " + jobStatus);
          System.out.println("IsReleased: " + jobState.isIsReleased());
          System.out.println("=============================");
          Thread.sleep(1000);
      }
      while (jobStatus == JobStatus.Running || jobStatus == JobStatus.Queued);

      ReleaseData relData = new ReleaseData(true, false);

      FinalizeData finalizeData = new FinalizeData(jobStatus);
      String finalizeDataStr = finalizeData.serialize();
      String finalizeResultStr = rjm.FinalizeJob(uniqueCallId, jobSpaceId, batchJobId, finalizeDataStr);
      webResult = AbstractSerializableData.deSerialize(finalizeResultStr, new TypeToken<WebResult<FinalizeResult>>() {});

      processWebResult(webResult);
      ReleaseData releaseData = new ReleaseData(true, false);
      String releaseDataStr = releaseData.serialize();
      String releaseJobResultStr = rjm.ReleaseJob(uniqueCallId, jobSpaceId, batchJobId, releaseDataStr);
      webResult = AbstractSerializableData.deSerialize(releaseJobResultStr, new TypeToken<WebResult<?>>() {});
      processWebResult(webResult);

      String jobConclusionInfo = rjm.GetJobConclusionInformation(jobSpaceId, batchJobId);
      JobConclusionInformation jobConclusionResult = AbstractSerializableData.deSerialize(jobConclusionInfo,
      new TypeToken<JobConclusionInformation>() {});
      System.out.println("=============================");
      System.out.println("FinalStatus: " + jobConclusionResult.getFinalStatus());
      System.out.println("IsReleased: " + jobConclusionResult.isIsReleased());
      System.out.println("ExitCodes: " + jobConclusionResult.getCommandExitCodes());
      System.out.println("=============================");

      ...

      // helper function
      public static <T extends Object> T processWebResult(WebResult<T> webResult) throws IngressException {
      	ServiceCallOutcome outcome = webResult.getOutComeEnum();
      	if (outcome.equals(ServiceCallOutcome.Failure) || outcome.equals(ServiceCallOutcome.Exception)) {
      		Throwable cause = new Throwable(webResult.getStackTrace());
      		IngressException e = new CommonsException("job.generalError", cause, webResult.getFailureMessage());
      		throw e;
      	}
      	T resultData = webResult.getData();
      	return resultData;
      }

Python (IronPython)

      # set this path to one that contains commands.xml and SecInput.txt
      job_files_path = "C:\\temp\\job_files"

      # set this path to where the job will run
      client_directory_path = "C:\\temp\\job_dir"

      jm = conn.getJobManager()
      configQueueName = "Local"
      markerName = tempfile.NamedTemporaryFile('w+b', delete=False).name
      associatedUncPathsForClientDirectory = List[str]([]).AsReadOnly()
      jobSpaceCreationData = JobSpaceCreationData(markerName, client_directory_path, str(uuid.uuid4()),
      associatedUncPathsForClientDirectory);
      jobSpaceCreationResult = jm.CreateJobSpace(jobSpaceCreationData);
      jobSpaceId = jobSpaceCreationResult.JobSpaceId

      # do an upload if the client working directory is not visible to RSM...
      if not jobSpaceCreationResult.CanSeeClientWorkingDirectory:
      	sourceLocalFilePath = os.path.join(job_files_path, 'commands.xml')
      	destinationRelativeFilePath = "commands.xml"
      	cancelHandle = None
      	print "uploading " + sourceLocalFilePath + " to " +  destinationRelativeFilePath + "."
      	jm.UploadFile(jobSpaceId, sourceLocalFilePath, destinationRelativeFilePath, cancelHandle)
      	sourceLocalFilePath = os.path.join(job_files_path, 'SecInput.txt')
      	destinationRelativeFilePath = "SecInput.txt"
      	print "uploading " + sourceLocalFilePath + " to " +  destinationRelativeFilePath + "."
      	jm.UploadFile(jobSpaceId, sourceLocalFilePath, destinationRelativeFilePath, cancelHandle)
      else:
      	shutil.copyfile(os.path.join(job_files_path, "commands.xml"), os.path.join(client_directory_path,
      	"commands.xml"))
      	shutil.copyfile(os.path.join(job_files_path, "SecInput.txt"), os.path.join(client_directory_path,
      	"SecInput.txt"))

      submitData = RsmSubmitData()
      submitData.AutomaticInputs = True
      submitData.AutomaticOutputs = True
      submitData.ClientDirectory = client_directory_path
      submitData.CommandsXmlFileName = "commands.xml"
      submitData.DisplayName = "MyJob"
      submitData.JobType = "TEST"
      submitData.ConfiguredQueue = configQueueName
      uniqueCallId = str(uuid.uuid4())

      solverTranscriptFileSearchPatterns = List[str](["force.out"]).AsReadOnly()
      submitReturnVal = jm.SubmitJob(uniqueCallId, jobSpaceId, submitData.Serialize(), PortalSubmitData(jobSpaceId,
      "TEST", solverTranscriptFileSearchPatterns))

      batchjobid = None
      if submitReturnVal is not None:
      	print "submitReturnVal:" + submitReturnVal
      	webResult = Serializer.DeserializeWebResult[str](submitReturnVal)
      	batchjobid = webResult.Data
      	print "Batch Job Id = " + batchjobid
      else:
      	print "submitReturnVal is null."

      # Check the status of the job every 5 seconds...
      stat = JobStatus.Running;
      while (stat == JobStatus.Running or stat == JobStatus.Queued):
      	statusStr = jm.GetJobStatus(uniqueCallId, jobSpaceId, batchjobid)
      	print "GetJobStatus returned:" + statusStr
      	statusWebResult = Serializer.DeserializeWebResult[JobState](statusStr)
      	jobState = statusWebResult.Data
      	stat = jobState.JobStatus;
      	print "============================="
      	print "GetJobStatus: " + stat.ToString()
      	print "IsReleased: " + jobState.IsReleased.ToString()
      	print "============================="
      	time.sleep(5);

      relData = ReleaseData(False, False)
      finData = FinalizeData(stat)
      jm.FinalizeJob(uniqueCallId, jobSpaceId, batchjobid, finData.Serialize())
      jm.ReleaseJob(uniqueCallId, jobSpaceId, batchjobid, relData.Serialize())

      infoStr = jm.GetJobConclusionInformation(jobSpaceId, batchjobid)
      print "GetJobConclusionInformation returned: " + infoStr
      infoWebResult = Serializer.DeserializeWebResult[JobConclusionInformation](infoStr)
      jInfo = infoWebResult.Data
      print "============================="
      print "final status: " + jInfo.FinalStatus.ToString()
      print "is released: " + jInfo.IsReleased.ToString()
      print "============================="

C#

      //set this path to one that contains commands.xml and SecInput.txt
      string jobFilesPath = "C:\\temp\\job_files";

      //set this path to where the job will run
      string clientDirectoryPath = "C:\\temp\\job_dir";

      string jobType = "TEST";

      string markerFileName = Path.GetRandomFileName();
      string markerFilePath = System.IO.Path.Combine(clientDirectoryPath, markerFileName);
      File.Create(markerFilePath);

      //List<string> inputFiles = new List<string> { "SecInput.txt", "commands.xml" };
      List<String> uncPaths = new List<string>();
      IJobSpaceCreationData jobSpaceCreationData = new JobSpaceCreationData(markerFileName, clientDirectoryPath,
      Guid.NewGuid().ToString(), uncPaths);
      IJobSpaceCreationResult jobSpaceCreationResult = rjm.CreateJobSpace(jobSpaceCreationData);
      string jobSpaceId = jobSpaceCreationResult.JobSpaceId;

      Console.WriteLine("Job created: " + jobSpaceId);

      Console.WriteLine("client working directory " + (jobSpaceCreationResult.CanSeeClientWorkingDirectory ?
      "IS" : "is NOT") + " visible.");

      //do an upload if the client working directory is not visible to RSM...
      if (!jobSpaceCreationResult.CanSeeClientWorkingDirectory)
      {
      	string sourceLocalFilePath = System.IO.Path.Combine(jobFilesPath, "commands.xml");
      	string destinationRelativeFilePath = "commands.xml";
      	Console.WriteLine("uploading " + sourceLocalFilePath + " to " + destinationRelativeFilePath + ".");
      	rjm.UploadFile(jobSpaceId, sourceLocalFilePath, destinationRelativeFilePath, null);
      	sourceLocalFilePath = System.IO.Path.Combine(jobFilesPath, "SecInput.txt");
      	destinationRelativeFilePath = "SecInput.txt";
      	Console.WriteLine("uploading " + sourceLocalFilePath + " to " + destinationRelativeFilePath + ".");
      	rjm.UploadFile(jobSpaceId, sourceLocalFilePath, destinationRelativeFilePath, null);
      }
      else
      {
      	System.IO.File.Copy(System.IO.Path.Combine(jobFilesPath, "commands.xml"),
      		System.IO.Path.Combine(clientDirectoryPath, "commands.xml"));
      	System.IO.File.Copy(System.IO.Path.Combine(jobFilesPath, "SecInput.txt"),
      		System.IO.Path.Combine(clientDirectoryPath, "SecInput.txt"));
      }

      string configQueueName = "Local";
      Console.WriteLine("Submitting job to ConfiguredQueue: " + configQueueName);

      RsmSubmitData submitData = new RsmSubmitData();
      submitData.AutomaticInputs = true;
      submitData.AutomaticOutputs = true;
      submitData.ClientDirectory = clientDirectoryPath;
      submitData.CommandsXmlFileName = "commands.xml";
      submitData.DisplayName = "MyJob";
      submitData.JobType = jobType;
      submitData.ConfiguredQueue = configQueueName;
      string uniqueCallId = Guid.NewGuid().ToString();

      List<string> patterns = new List<string> { "force.out" };
      ReadOnlyCollection<string> solverTranscriptFileSearchPatterns = patterns.AsReadOnly();
      string submitReturn = rjm.SubmitJob(uniqueCallId, "", submitData.Serialize(), new PortalSubmitData(jobSpaceId,
      jobType, solverTranscriptFileSearchPatterns));
      string batchjobid = null;
      if (submitReturn != null)
      {
      	Console.WriteLine("SubmitJob Result:" + Environment.NewLine + submitReturn);
      	var webResult = Serializer.DeserializeWebResult<String>(submitReturn);
      	CheckForError(webResult);
      	batchjobid = webResult.Data;
      	Console.WriteLine("Batch Job Id = " + batchjobid);
      }
      else
      {
      	Console.WriteLine("SubmitJob Result is null.");
      }

      // Check the status of the job every 5 seconds...
      JobStatus stat = JobStatus.Unknown;
      do
      {
      	string statusStr = rjm.GetJobStatus(uniqueCallId, jobSpaceId, batchjobid);
      	var statusWebResult = Serializer.DeserializeWebResult<JobState>(statusStr);
      	CheckForError(statusWebResult);
      	JobState jobState = statusWebResult.Data;

      	stat = jobState.JobStatus;
      	Console.WriteLine("=============================");
      	Console.WriteLine("GetJobStatus: " + stat);
      	Console.WriteLine("IsReleased: " + jobState.IsReleased);
      	Console.WriteLine("=============================");
      	Thread.Sleep(5000);
      }
      while (stat == JobStatus.Running || stat == JobStatus.Queued);

      ReleaseData relData = new ReleaseData(true, false);

      FinalizeData finData = new FinalizeData(stat);
      Console.WriteLine(rjm.FinalizeJob(uniqueCallId, jobSpaceId, batchjobid, finData.Serialize()));
      Console.WriteLine(rjm.ReleaseJob(uniqueCallId, jobSpaceId, batchjobid, relData.Serialize()));

      string infoStr = rjm.GetJobConclusionInformation(jobSpaceId, batchjobid);
      Console.WriteLine(infoStr);
      var infoWebResult = Serializer.DeserializeWebResult<JobConclusionInformation>(infoStr);
      CheckForError(infoWebResult);
      JobConclusionInformation jInfo = infoWebResult.Data;
      Console.WriteLine("=============================");
      Console.WriteLine("FinalStatus: " + jInfo.FinalStatus);
      Console.WriteLine("IsReleased: " + jInfo.IsReleased);
      Console.WriteLine("ExitCodes: " + jInfo.CommandExitCodes);
      Console.WriteLine("=============================");

Contains proprietary and confidential information of ANSYS, Inc. and its subsidiaries and affiliates.

Prev | Up / Contents | Next

Release 18.2 - © ANSYS, Inc. All rights reserved.