Need to update queue from a process

Arun Singh
2012-06-18
2013-06-12
  • Arun Singh
    Arun Singh
    2012-06-18

    Using process in STAX script I am running Python script(using PySTAFv3 in python script), Now I want to print message of python script to STAXMon window.

    I have created a Handle
    Have called the process to start python script (can pass handle number to python script)
    while running process I want to print the message from python script to STAXMon ..How to do this ?

    any other help on this is appreciated.

    Thanks

     
  • Arun Singh
    Arun Singh
    2012-06-19

    I am passing STADHandle to Python and using queue I am sending message to that particular Handle.
    In STAX Job I am launching the process which is calling python file and start executing, Now python is also sending message to STAX handle but now I want to get message from the queue at the time it added to the queue and want to print   till it get end Notification.  How to read message from queue and print it on run time ?

    <!- SNIP from STAX file
       <function name="main">
          <sequence>
                 <script>
                    handlename = "atry"
                 </script>
          <message>"Welcome To ABC"</message>
    <message>"This script establish the connection"</message>
    <call function = "'staf-newhandle'">
                {
                   'name': handlename
                }</call>
                <script>
                   Handle_number = STAXResult   
                </script>
        <message>"Handle_number is %s" %Handle_number</message>
                <message>"Handle %s Created Successfully" %handlename</message>
                <message>"Starting Python Process"</message>
    <message>"%s" %STAXJobID</message>
                <call function = "'stafprocess'">
                {
                   'server': "local",
                   'command': "python c:\\STAF\\trial\try.py %s %s"  %(STAXJobID,Handle_number)
                }</call>
                <message>"Waiting for End Notification"</message>
                <script>
                    import time
                    time.sleep(3000)
                </script>
    <call function = "'staf-queue-listmsg'">
    {
      'server':"local",
      'handle': "%s" %Handle_number
    }</call>
    <message>STAXResult</message>
    <message>"Test Completed"</message>
                <message>"Deleting handle %s Handle_number %s " %(handlename,handlename)</message>
    <call function = "'staf-deletehandle'">
                {
                   'handle': Handle_number
                }</call>
                <message>"handle %s Handle_number %s deleted " %(handlename,Handle_number)</message>

          </sequence>
       </function>
    ->

    Python File code:
    <!- SNIP
    from PySTAFv3 import *
    import sys
    JobID_fromSTAX = sys.argv;
    Handle_fromSTAX = sys.argv;

    handle = STAFHandle("MyPythonTest")

    message = 'Python script with handle %d added to queue' % (handle.handle)
      
    request = 'QUEUE MESSAGE %s handle %s ' % (wrapData(message), wrapData(Handle_fromSTAX))
    result = handle.submit("local", "QUEUE", request)
    ->

     
  • Sharon Lucas
    Sharon Lucas
    2012-06-19

    So you didn't really want to send a message to the STAX Monitor's "Messages" section as I had originally thought you did from your original description.

    You want to send a message from your Python script (executed via a <process> element in your STAX job) to a handle you created in your STAX job and receive the message.  If you explained more what you're trying to do and why you think you want to do it this way, I might be able to suggest a better way.  For example, if you simply want to know when the Python script ends, then you'll know that when the <process> element ends and you don't need to send messages between the Python script and the STAX job.

    But, if you really have a need for sending a message from the Python script to the STAX job, then look at the "Sample STAX Job 3 - Creating a STAF Handle and Using it's Queue" example in the STAX User's Guide at http://staf.sourceforge.net/current/STAX/staxug.html#Header_SampleJob3.   It shows how you can use a STAF Handle that you create in your STAX job and use it to submit a QUEUE GET request.  Note that to be able to get a message off the STAF Handle's queue while the process is running you'll need to be running these two actions in parallel (e.g. using the <parallel> element).

     
  • Arun Singh
    Arun Singh
    2012-06-19

    In short, I want to do the following:

    1. will Launch a python script using process from STAX script
    2. Python script will run the certain test.
    3. I want to print step by step message of my test execution to STAX Monitor.
    4. Once my test will end, It will send PASS or FAIL message to STAX script
    5. After receiving Test notification, STAX will show the result.

     
  • Sharon Lucas
    Sharon Lucas
    2012-06-19

    Ok, then you don't need to use the QUEUE service or create a new STAF Handle in your STAX job.  All you have to do is pass the STAX Job ID to the Python script in your <process> element and have your Python script submit a 'SEND JOB %s MESSAGE %s' request to the STAX service on the machine where the STAX service is running specifying the STAX Job ID for the value of the JOB option to send a message to the STAX Monitor's "Message" section.  For example:

    STAX job:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE stax SYSTEM "stax.dtd">
    <stax>
      <defaultcall function="Main"/>
      <function name="Main">
    
         <sequence>
            <message log="1">'Send a message from Python script to STAX Monitor'</message>                   
            <process name="'MyPythonScript'">
              <location>'local'</location>
              <command mode="'shell'">'python C:/test/sendMsg.py %s' % (STAXJobID)</command>
              <stderr mode="'stdout'"/>
              <returnstdout/>
            </process>       
            <log message="1">
              'Python Script RC=%s, STAFResult=%s, STAXResult=\n%s' % (RC, STAFResult, STAXResult)
            </log>
          </sequence>   
        
      </function>      
    </stax/>
    

    And here's the content of C:\test\sendMsy.py:

    from PySTAFv3 import *
    import sys

    # Get STAX Job ID passed as an argument
    staxJobID = sys.argv

    # Set to the hostname or /IP address of your STAX service machine
    # (or 'local' if the Python script is run on your STAX service machine)
    staxMachine = 'local'

    # Create a STAF handle works to use to submit STAF service requests

    try:
        handle = STAFHandle('MyPythonTest')
    except STAFException as e:
        print('Error registering with STAF, RC: %d Result: %s' % (e.rc, e.result))
        sys.exit(e.rc)

    # Send a status message to the STAX Monitor

    message = 'Python script is in Step 1'
    request = 'SEND JOB %s MESSAGE %s' % (staxJobID, wrapData(message))
    result = handle.submit(staxMachine, 'STAX', request)

    if (result.rc != STAFResult.Ok):
        print('Error: RC=%s Result=%s\nSTAF %s STAX %s' % \
           (result.rc, result.result, staxMachine, request))
        sys.exit(1)

    print('\n*** Test successful ***')

    # Un-register the STAF handle

    result = handle.unregister()

    if (result != STAFResult.Ok):
        print('Error unregistering with STAF, RC: %d' % result)

    # Test passed - Return 0
    sys.exit(0)

    Then when I run this STAX job via the STAX Monitor, I see the following in the "Messages" tab of the STAX Monitor:

    20120619-13:59:59 Send a message from Python script to STAX Monitor                                                                                                                                                      
    20120619-13:59:59 Python script is in Step 1
    20120619-13:59:59 Python Script RC=0, STAFResult=None, STAXResult=
    []

     
  • Arun Singh
    Arun Singh
    2012-06-20

    Thanks It's working fine ..  will ping you again if get stuck .. I am developing automation framework while STAF is base :-)