none
How to create a parentjob for DurableSession or Session? RRS feed

  • Question

  • I want to create a parentjob for a DurableSession. How do I go about doing it? Did not find a ParentJob setting either in SessionStartInfo or Session/DurableSession API

    I am not looking for nodeprep command line tasks, but a complete job upon the completion of which my DurableSession/Session job should begin.

    I am using HPC pack 2012 R2 with latest QFE installed

    Thanks

    Sunday, February 12, 2017 3:08 AM

All replies

  • Hi,

      This property isn't been exposed in the session API. But as session job is "job" thus you can do it by the job interface to set this parentJob during session creation. If you have problem doing this, please let us know as we can provide simple sample codes here.


    Qiufang Shi

    Monday, February 13, 2017 8:01 AM
  • I may wrong on this as it is not easy for you to grab the job object from session API. The current only possible way for you to do is through Submission Filter which has the ability to modify the job before the job becomes queued.

    And we are evaluating whether to expose this property in the session API in the soon upcoming QFE.


    Qiufang Shi

    Monday, February 13, 2017 9:36 AM
  • Please provide a code sample of 'Submission Filter'.. Thanks
    Monday, February 13, 2017 10:53 AM
  • Please Check below sample codes. You need reference Microsoft.Hpc.Scheduler.dll, to use the submission filter, please check related doc.
    using System;
    using System.Collections.Generic;
    using System.Xml;
    using System.IO;
    using System.Reflection;
    
    
    using Microsoft.Hpc.Scheduler.AddInFilter.HpcClient;
    
    
    namespace SubmissionSOAParentJob
    {
        public class SOAParentJobFilter: ISubmissionFilter, IFilterLifespan  // note: IFilterLifespan is optional
        {
            public TextWriter logFile = null;
            private int someInternalState = 0;
    
            public SubmissionFilterResponse FilterSubmission(Stream jobXmlIn, out Stream jobXmlModified)
            {
                someInternalState++;
    
                // Create the Log file for the filter.
                SubmissionFilterResponse retval = SubmissionFilterResponse.SuccessNoJobChange;
    
                jobXmlModified = null;
    
                if ((retval = setupLogFile()) != 0)
                {
                    return retval;
                }
    
                // Load the job file as an XmlDocument.
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(jobXmlIn);
    
                    XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
                    nsMgr.AddNamespace("hpc", "http://schemas.microsoft.com/HPCS2008R2/scheduler/");
    
                    // Find the job node in the XML document.
                    XmlNode job = doc.SelectSingleNode("/hpc:Job", nsMgr);
    
                    if (job == null)
                    {
                        throw new Exception("No job in the xml file");
                    }
                    var jobId = job.Attributes["Id"].Value;
                    logFile.WriteLine("Validating job {0}", jobId);
    
                    // Check if env variables defined.
                    XmlNode envs = job["EnvironmentVariables"];
                    if (envs == null)
                    {
                        logFile.WriteLine("job {0} doesn't have any Environment Variables defined, success the job submission", jobId);
                        return SubmissionFilterResponse.SuccessNoJobChange;
                    }
    
                    string parentJobIdValue = null;
                    foreach (XmlNode variable in job["EnvironmentVariables"].ChildNodes)
                    {
                        var nameNode = variable["Name"];
                        if (nameNode != null && nameNode.InnerText.ToUpper() == "PARENTJOBID")
                        {
                            parentJobIdValue = variable["Value"].InnerText;
                            break;
                        }
                    }
    
                    if (parentJobIdValue == null)
                    {
                        logFile.WriteLine("job {0} doesn't have PARENTJOBID specified, success the job submission", jobId);
                        return SubmissionFilterResponse.SuccessNoJobChange;
                    }
                    int parentJobId;
                    if (!int.TryParse(parentJobIdValue, out parentJobId))
                    {
                        logFile.WriteLine("job {0} has PARENTJOBID specified, but invalid format {1}, fail the job submission", jobId, parentJobIdValue);
                        return SubmissionFilterResponse.FailJob;
                    }
                    if (parentJobId < 0) // you may also add logics here to check whether the job Id specified is a valid job
                    {
                        logFile.WriteLine("job {0} has PARENTJOBID specified, but invalid number {1}, fail the job submission", jobId, parentJobId);
                        return SubmissionFilterResponse.FailJob;
                    }
                    logFile.WriteLine("job {0} has PARENTJOBID specified with value {1}, submission filter validation pass, pass the job submission with job change", jobId, parentJobId);
    
                    jobXmlModified = new MemoryStream();
                    XmlAttributeCollection attrCol = job.Attributes;
                    XmlAttribute parentJobIds = attrCol["ParentJobIds"];
                    if (parentJobIds == null)
                    {
                        parentJobIds = doc.CreateAttribute("ParentJobIds");
                        parentJobIds.Value = parentJobId.ToString();
                        attrCol.SetNamedItem(parentJobIds);
                    }
                    else
                    {
                        parentJobIds.Value += "," + parentJobId.ToString();
                    }
    
                    doc.Save(jobXmlModified);
    
                    // Return a value of 1 to indicate that the values were changed.
                    retval = SubmissionFilterResponse.SuccessJobChanged;
    
                }
                catch (IOException e)
                {
                    logFile.WriteLine("Error Loading the XmlFile");
                    logFile.WriteLine(e.ToString());
    
                    throw;
                }
                catch (Exception e)
                {
                    logFile.WriteLine("Error Parsing the XmlFile");
                    logFile.WriteLine(e.ToString());
    
                    throw;
                }
                finally
                {
                    logFile.Close();
                }
    
                return retval;
            }
    
            private SubmissionFilterResponse setupLogFile()
            {
                try
                {
                    string assemblyPathInclusive = Assembly.GetExecutingAssembly().Location;
                    string assemblyPath = Path.GetDirectoryName(assemblyPathInclusive);
                    String logFileName = Path.Combine(assemblyPath, "SubmissionFilter.log");
                    logFile = new StreamWriter(logFileName,true);
                    return  SubmissionFilterResponse.SuccessNoJobChange;
                }
                catch (Exception ex)
                {
                    return SubmissionFilterResponse.FailJob;
                }
            }
    
            public void RevertSubmission(Stream jobXml)
            {
                // any cancelation code here (release licenses, etc)
            }
    
            // note that IFilterLifespan is optional.  If you do not need setup/teardown code executed
            // do not declare or implement IFilterLifespan 
    
            public void OnFilterLoad()
            {
                // setup code here
            }
    
            public void OnFilterUnload()
            {
                // teardown code here
            }
        }
    }
    


    Tuesday, February 14, 2017 2:33 AM