ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Use the QUSLJOB API to whip thru WRKACTJOB

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use the QUSLJOB API to whip thru WRKACTJOB

    Use the QUSLJOB API to whip thru WRKACTJOB

    Look for any jobs in *MSGW and send a email using SNDDST
    to the user specified in the EmailAddress variable.

    Found this on the WEB and just "adjusted" it a bit

    Code:
          *
          * CrtUsrSpc: Create User Space for OS/400 API's
          *
         d QUSCRTUS        pr                  extpgm('QUSCRTUS')
         d   UsrSpc                      20A   const
         d   ExtAttr                     10A   const
         d   InitialSize                 10I 0 const
         d   InitialVal                   1A   const
         d   PublicAuth                  10A   const
         d   Text                        50A   const
         d   Replace                     10A   const
         d   ErrorCode                32766A   options(*nopass: *varsize)
          *
          * --- Prototype for API Retrive User Space
          *
         d QUSRTVUS        pr                  extpgm( 'QUSRTVUS' )
         d   QRtvUserSpace...
         d                               20
         d   QRtvStartingPosition...
         d                                8b 0
         d   QRtvLengthOfData...
         d                                8b 0
         d   QRtvReceiverVariable...
         d                            32048
         d   QRtvError...
         d                              256
    
          * --- Prototype for API Retrive List Job
          *
         d QUSLJOB         pr                  extpgm( 'QUSLJOB' )
         d   QJobUserSpace...
         d                               20
         d   QJobFormatName...
         d                                8
         d   QJobJobName...
         d                               26
         d   QFldStatus...
         d                               10
         d   QFldError...
         d                              256
         d   QJobType...
         d                                1
         d   QNbrFldRtn...
         d                                8b 0
         d   QKeyFldRtn...
         d                                8b 0 dim( 100 )
          *
         d qcmdexc         pr                  extpgm( 'QCMDEXC' )
         d   os400_cmd                 2000A   options( *varsize ) const
         d   cmdlength                   15P 5                     const
          *
          * Defined variables
          *
         d emailaddress    s             24    inz('podmaster@chemicalali.com')
         d size            s             10I 0
         d UsrSpcName      s             20    inz( 'DSPJOB    QTEMP     ' )
    
          *
          ******************************************************************
    
         dQUSA0100         DS
         d QUsrSpcOffset...
         d                         1      4B 0
         d QUsrSpcEntries...
         d                         9     12B 0
         d QUsrSpcEntrieSize...
         d                        13     16B 0
    
         dLJOBINPUT        ds                           qualified
         d  JobName...
         d                         1     10
         d  UserName...
         d                        11     20
         d  JobNumber...
         d                        21     26
         d  Status...
         d                        27     36
         d  UserSpace...
         d                        37     46
         d  UserSpaceLibrary...
         d                        47     56
         d  Format...
         d                        57     64
         d  JobType...
         d                        65     65
         d  Reserved01...
         d                        66     68
         d  Reserved02...
         d                        69     72B 0
          *
         dLJOB100          ds                           qualified
         d  JobName...
         d                         1     10
         d  UserName...
         d                        11     20
         d  JobNumber...
         d                        21     26
         d  InternalJobId...
         d                        27     42
         d  Status...
         d                        43     52
         d  JobType...
         d                        53     53
         d  JobSubType...
         d                        54     54
         d  Reserved01...
         d                        55     56
          *
         dLJOB200          ds                           qualified
         d  JobName...
         d                         1     10
         d  UserName...
         d                        11     20
         d  JobNumber...
         d                        21     26
         d  InternalJobId...
         d                        27     42
         d  Status...
         d                        43     52
         d  JobType...
         d                        53     53
         d  JobSubType...
         d                        54     54
         d  Reserved01...
         d                        55     56
         d  JobInfoStatus...
         d                        57     57
         d  Reserved02...
         d                        58     60
         d  NumberOfFieldsReturned...
         d                        61     64B 0
         d  ReturnedData...
         d                        65   1064
          *
         dLJOB200KEY       ds                           qualified
         d  KeyNumber01...
         d                         1      4B 0
         d  NumberOfKeys...
         d                         5      8B 0
          *
         dLJOBKEYINFO      ds                           qualified
         d  LengthOfInformation...
         d                         1      4b 0
         d  KeyField...
         d                         5      8b 0
         d  TypeOfData...
         d                         9      9
         d  Reserved01...
         d                        10     12
         d  LengthOfData...
         d                        13     16B 0
         d  KeyData...
         d                        17   1016
          *
          *  APIErrDef     Standard API error handling structure.                  *
          *
         dQUSEC            DS
         d  ErrorBytesProvided...
         d                         1      4B 0
         d  ErrorBytesAvailble...
         d                         5      8b 0
         d  ErrorExceptionId...
         d                         9     15
         d  ErrorReserved...
         d                        16     16
          *
         dAPIError         DS
         d APIErrorProvied...
         d                                     LIKE( ErrorBytesProvided )
         d                                     INZ( %LEN( APIError ) )
         d APIErrorAvailble...
         d                                     LIKE( ErrorBytesAvailble )
         d APIErrorMessageID...
         d                                     LIKE( ErrorExceptionId )
         d APIErrorReserved...
         d                                     LIKE( ErrorReserved )
         d APIErrorInformation...
         d                              240A
          *-----------------------------------------------------------------
          * program status dataarea
          *-----------------------------------------------------------------
         d PgmSts         SDS
         d   P1User              254    263
         d   W1Program       *PROC
          *--------------------------------------------------------------*
          * work fields                                                  *
          *--------------------------------------------------------------*
         d Variables       ds
         d   Q                            1    inz( '''' )
         d   Count                       15  0 inz(  0   )
         d   KeyCount                    15  0 inz(  0   )
         d   EndPos                      15  0 inz(  0   )
         d   JobbStatus                   4    inz( ' '  )
         d   Subsystem                   20    inz( ' '  )
         d   ReturnCode                   1    inz( ' '  )
         d   FormatName                   8    inz( ' ' )
         d   QualifedJobName...
         d                               26    inz( ' ' )
         d   JobStatus                   10    inz( ' ' )
         d   JobType                      1    inz( ' ' )
         d   NbrOfFldRtn                  8B 0 inz(  0  )
         d   KeyFldRtn                    8B 0 inz(  0  ) dim( 100 )
         d   StartingPosition...
         d                                8B 0 inz(  0  )
         d   LengthOfData...
         d                                8B 0 inz(  0  )
         d   KeyStartingPosition...
         d                                8B 0 inz(  0  )
         d   KeyLengthOfData...
         d                                8B 0 inz(  0  )
         d   ReceiverVariable...
         d                            32048
         d   OS400_Cmd                 2000    inz( ' '  )
         d   CmdLength                   15P 5 inz( %size( OS400_Cmd ) )
         d   True                         1    inz( *on  )
         d   False                        1    inz( *off )
          *
          /free
    
           //
           // Create a user space
           //
              size = 10000;
    
             // Create a user space
             QUSCRTUS(UsrSpcName: 'USRSPC': size: x'00': '*ALL':
              'Temp User Space for  QUSLJOB API':  '*YES': APIError);
    
    
           exsr CheckStatusOfJob;
    
           *inlr = *on;
           // *************************************************************
           // check status of an job
           // -------------------------------------------------------------
           begsr CheckStatusOfJob;
    
           // run API to fill user space with information about all iSeries job
    
           FormatName = 'JOBL0200';
           QualifedJobName = '*ALL      ' + '*ALL      ' + '*ALL  ';
           JobStatus = '*ACTIVE';
           JobType = '*';
           NbrOfFldRtn = 2;
           KeyFldRtn( 1 ) = 0101;
           KeyFldRtn( 2 ) = 1906;
           callp QUSLJOB( UsrSpcName : FormatName  : QualifedJobName :
                          JobStatus  : APIError    :
                          JobType    : NbrOfFldRtn : KeyFldRtn         );
    
            // if error message from the retrieve job API then dump program
    
           if APIErrorMessageID <> ' ';
             dump;
             ReturnCode = True;
             leavesr;
           endif;
    
           // run API to get user space attribute
    
           StartingPosition = 125;
           LengthOfData = 16;
           callp QUSRTVUS( UsrSpcName   : StartingPosition  :
                           LengthOfData : ReceiverVariable  :
                           APIError                           );
           QUSA0100 = ReceiverVariable;
    
            // if error message from the retrieve user space API then dump program
    
           if APIErrorMessageID <> ' ';
             dump;
             ReturnCode = True;
             leavesr;
           endif;
    
           // preperation to read from user space
    
           StartingPosition = QUsrSpcOffset + 1;
           LengthOfData = QUsrSpcEntrieSize;
    
           // read from user space
    
           for count = 1 to QUsrSpcEntries;
             callp QUSRTVUS( UsrSpcName   : StartingPosition  :
                             LengthOfData : ReceiverVariable  :
                             APIError                           );
             LJOB200 = ReceiverVariable;
             if APIErrorMessageID <> ' ';
               dump;
               ReturnCode = True;
               leavesr;
             endif;
    
             // check status of job
             JobbStatus = ' ';
             Subsystem = ' ';
             LJobKeyInfo = LJob200.ReturnedData;
             KeyStartingPosition = 1;
             KeyLengthOfData = LJobKeyInfo.LengthOfInformation;
             for keycount = 1 to LJob200.NumberOfFieldsReturned;
               LJobKeyInfo = %subst( LJob200.ReturnedData :
                                     KeyStartingPosition :
                                     KeyLengthOfData );
               KeyLengthOfData = LJobKeyInfo.LengthOfInformation;
               LJobKeyInfo = %subst( LJob200.ReturnedData :
                                     KeyStartingPosition :
                                     KeyLengthOfData );
               Endpos = LJobKeyInfo.LengthOfData;
               if     LJobKeyInfo.KeyField = 0101;
                  JobbStatus = %subst( LJobKeyInfo.KeyData : 1 :  Endpos );
               elseif LJobKeyInfo.KeyField = 1906;
                  Subsystem = %subst( LJobKeyInfo.KeyData : 1 : Endpos );
               endif;
               KeyStartingPosition = KeyStartingPosition + KeyLengthOfData;
             endfor;
    
             // if job in message wait then email message to address in
             // variable email address
    
             if Jobbstatus = 'MSGW';
                Subsystem = %trim( %subst( Subsystem : 11 : 10 ) ) + '/' +
                            %trim( %subst( Subsystem :  1 : 10 ) );
                os400_cmd = 'snddst type(*lmsg) ' +
                            'tointnet((' + Q + %trim(EmailAddress) +
                            Q + ')) dstd(' + Q    +
                            'Job  is in  *MSGW'   +
                            Q + ') longmsg(' + Q  +
                            'Job (' +
                            %trim( LJob200.JobName ) + '/' +
                            %trim( LJob200.UserName ) + '/' +
                            %trim( LJob200.JobNumber ) +
                            ') subsystem ' + %trim( Subsystem ) +
                            ' in status *MSGW' +
                            Q + ')';
                 monitor;
                   qcmdexc ( os400_cmd : %size ( os400_cmd ) );
                   on-error;
                     dump;
                endmon;
             endif;
    
             StartingPosition = StartingPosition + LengthOfData;
    
           endfor;
    
           endsr;
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

  • #2
    Re: Use the QUSLJOB API to whip thru WRKACTJOB

    This program works great! Is there any way it can also pull the message details for the job that's in *MSGW?
    Eric Pell

    Comment


    • #3
      Re: Use the QUSLJOB API to whip thru WRKACTJOB

      This program works great! Is there any way to get the program to also send the message details? I've tweaked it a bit to send the Job and User in subject and would love to have the message details passed in the message body.

      I also noticed that we get a good amount of printer messages and I changed the line

      if Jobbstatus = 'MSGW';

      to

      if Jobbstatus = 'MSGW' and Subsystem <> 'QSYS/QSPL';

      This ignores any printer related messages such as load forms, or the alignment errors.
      Eric Pell

      Comment


      • #4
        Re: Use the QUSLJOB API to whip thru WRKACTJOB

        sure do whatever you want with the program its yours...
        All you would need is the retrieve job info api....

        if possible repost with your changes we'd all like to see them

        jamie
        All my answers were extracted from the "Big Dummy's Guide to the As400"
        and I take no responsibility for any of them.

        www.code400.com

        Comment


        • #5
          Re: Use the QUSLJOB API to whip thru WRKACTJOB

          Well, I finally got around to posting my changes. This program has been working great for me. A little too great sometimes as one of my vendor's jobs likes to crash at 3:00am and it wakes me up. I added multiple email addresses to the program and also excluded the QSPL jobs from sending so I don't get any printer alignment errors.

          The code is here:


          PHP Code:
             *
             * 
          CrtUsrSpcCreate User Space for OS/400 API's
             *
            d QUSCRTUS        pr                  extpgm('
          QUSCRTUS')
            d   UsrSpc                      20A   const
            d   ExtAttr                     10A   const
            d   InitialSize                 10I 0 const
            d   InitialVal                   1A   const
            d   PublicAuth                  10A   const
            d   Text                        50A   const
            d   Replace                     10A   const
            d   ErrorCode                32766A   options(*nopass: *varsize)
             *
             * --- Prototype for API Retrive User Space
             *
            d QUSRTVUS        pr                  extpgm( '
          QUSRTVUS' )
            d   QRtvUserSpace...
            d                               20
            d   QRtvStartingPosition...
            d                                8b 0
            d   QRtvLengthOfData...
            d                                8b 0
            d   QRtvReceiverVariable...
            d                            32048
            d   QRtvError...
            d                              256

             * --- Prototype for API Retrive List Job
             *
            d QUSLJOB         pr                  extpgm( '
          QUSLJOB' )
            d   QJobUserSpace...
            d                               20
            d   QJobFormatName...
            d                                8
            d   QJobJobName...
            d                               26
            d   QFldStatus...
            d                               10
            d   QFldError...
            d                              256
            d   QJobType...
            d                                1
            d   QNbrFldRtn...
            d                                8b 0
            d   QKeyFldRtn...
            d                                8b 0 dim( 100 )
             *
            d qcmdexc         pr                  extpgm( '
          QCMDEXC' )
            d   os400_cmd                 2000A   options( *varsize ) const
            d   cmdlength                   15P 5                     const
             *
             * Defined variables
             *
            d emailaddress    s             24    inz('
          email1@domain.com')
            d emailaddress1   s             29    inz('
          email2@domain.com')
            d size            s             10I 0
            d UsrSpcName      s             20    inz( '
          DSPJOB    QTEMP     ' )

             *
             ******************************************************************

            dQUSA0100         DS
            d QUsrSpcOffset...
            d                         1      4B 0
            d QUsrSpcEntries...
            d                         9     12B 0
            d QUsrSpcEntrieSize...
            d                        13     16B 0

            dLJOBINPUT        ds                           qualified
            d  JobName...
            d                         1     10
            d  UserName...
            d                        11     20
            d  JobNumber...
            d                        21     26
            d  Status...
            d                        27     36
            d  UserSpace...
            d                        37     46
            d  UserSpaceLibrary...
            d                        47     56
            d  Format...
            d                        57     64
            d  JobType...
            d                        65     65
            d  Reserved01...
            d                        66     68
            d  Reserved02...
            d                        69     72B 0
             *
            dLJOB100          ds                           qualified
            d  JobName...
            d                         1     10
            d  UserName...
            d                        11     20
            d  JobNumber...
            d                        21     26
            d  InternalJobId...
            d                        27     42
            d  Status...
            d                        43     52
            d  JobType...
            d                        53     53
            d  JobSubType...
            d                        54     54
            d  Reserved01...
            d                        55     56
             *
            dLJOB200          ds                           qualified
            d  JobName...
            d                         1     10
            d  UserName...
            d                        11     20
            d  JobNumber...
            d                        21     26
            d  InternalJobId...
            d                        27     42
            d  Status...
            d                        43     52
            d  JobType...
            d                        53     53
            d  JobSubType...
            d                        54     54
            d  Reserved01...
            d                        55     56
            d  JobInfoStatus...
            d                        57     57
            d  Reserved02...
            d                        58     60
            d  NumberOfFieldsReturned...
            d                        61     64B 0
            d  ReturnedData...
            d                        65   1064
             *
            dLJOB200KEY       ds                           qualified
            d  KeyNumber01...
            d                         1      4B 0
            d  NumberOfKeys...
            d                         5      8B 0
             *
            dLJOBKEYINFO      ds                           qualified
            d  LengthOfInformation...
            d                         1      4b 0
            d  KeyField...
            d                         5      8b 0
            d  TypeOfData...
            d                         9      9
            d  Reserved01...
            d                        10     12
            d  LengthOfData...
            d                        13     16B 0
            d  KeyData...
            d                        17   1016
             *
             *  APIErrDef     Standard API error handling structure.                  *
             *
            dQUSEC            DS
            d  ErrorBytesProvided...
            d                         1      4B 0
            d  ErrorBytesAvailble...
            d                         5      8b 0
            d  ErrorExceptionId...
            d                         9     15
            d  ErrorReserved...
            d                        16     16
             *
            dAPIError         DS
            d APIErrorProvied...
            d                                     LIKE( ErrorBytesProvided )
            d                                     INZ( %LEN( APIError ) )
            d APIErrorAvailble...
            d                                     LIKE( ErrorBytesAvailble )
            d APIErrorMessageID...
            d                                     LIKE( ErrorExceptionId )
            d APIErrorReserved...
            d                                     LIKE( ErrorReserved )
            d APIErrorInformation...
            d                              240A
             *-----------------------------------------------------------------
             * program status dataarea
             *-----------------------------------------------------------------
            d PgmSts         SDS
            d   P1User              254    263
            d   W1Program       *PROC
             *--------------------------------------------------------------*
             * work fields                                                  *
             *--------------------------------------------------------------*
            d Variables       ds
            d   Q                            1    inz( '''' )
            d   Count                       15  0 inz(  0   )
            d   KeyCount                    15  0 inz(  0   )
            d   EndPos                      15  0 inz(  0   )
            d   JobbStatus                   4    inz( ' '  )
            d   Subsystem                   20    inz( ' '  )
            d   SubShort                    20    inz( ' '  )
            d   ReturnCode                   1    inz( ' '  )
            d   FormatName                   8    inz( ' ' )
            d   QualifedJobName...
            d                               26    inz( ' ' )
            d   JobStatus                   10    inz( ' ' )
            d   JobType                      1    inz( ' ' )
            d   NbrOfFldRtn                  8B 0 inz(  0  )
            d   KeyFldRtn                    8B 0 inz(  0  ) dim( 100 )
            d   StartingPosition...
            d                                8B 0 inz(  0  )
            d   LengthOfData...
            d                                8B 0 inz(  0  )
            d   KeyStartingPosition...
            d                                8B 0 inz(  0  )
            d   KeyLengthOfData...
            d                                8B 0 inz(  0  )
            d   ReceiverVariable...
            d                            32048
            d   OS400_Cmd                 2000    inz( ' '  )
            d   CmdLength                   15P 5 inz( %size( OS400_Cmd ) )
            d   True                         1    inz( *on  )
            d   False                        1    inz( *off )
             *
             /free

              //
              // Create a user space
              //
              size = 10000;

             // Create a user space
             QUSCRTUS(UsrSpcName: '
          USRSPC': size: x'00': '*ALL':
              '
          Temp User Space for  QUSLJOB API':  '*YES': APIError);


              exsr CheckStatusOfJob;

              *inlr = *on;
              // *************************************************************
              // check status of an job
              // -------------------------------------------------------------
              begsr CheckStatusOfJob;

              // run API to fill user space with information about all iSeries job

              FormatName = '
          JOBL0200';
              QualifedJobName = '
          *ALL      ' + '*ALL      ' + '*ALL  ';
              JobStatus = '
          *ACTIVE';
              JobType = '
          *';
              NbrOfFldRtn = 2;
              KeyFldRtn( 1 ) = 0101;
              KeyFldRtn( 2 ) = 1906;
              callp QUSLJOB( UsrSpcName : FormatName  : QualifedJobName :
                 JobStatus  : APIError    :
                 JobType    : NbrOfFldRtn : KeyFldRtn         );

            // if error message from the retrieve job API then dump program

              if APIErrorMessageID <> ' ';
             dump;
             ReturnCode = True;
             leavesr;
              endif;

              // run API to get user space attribute

              StartingPosition = 125;
              LengthOfData = 16;
              callp QUSRTVUS( UsrSpcName   : StartingPosition  :
                  LengthOfData : ReceiverVariable  :
                  APIError                           );
              QUSA0100 = ReceiverVariable;

            // if error message from the retrieve user space API then dump program

              if APIErrorMessageID <> ' ';
             dump;
             ReturnCode = True;
             leavesr;
              endif;

              // preperation to read from user space

              StartingPosition = QUsrSpcOffset + 1;
              LengthOfData = QUsrSpcEntrieSize;

              // read from user space

              for count = 1 to QUsrSpcEntries;
             callp QUSRTVUS( UsrSpcName   : StartingPosition  :
                 LengthOfData : ReceiverVariable  :
                 APIError                           );
             LJOB200 = ReceiverVariable;
             if APIErrorMessageID <> ' ';
               dump;
               ReturnCode = True;
               leavesr;
             endif;

             // check status of job
             JobbStatus = ' ';
             Subsystem = ' ';
             LJobKeyInfo = LJob200.ReturnedData;
             KeyStartingPosition = 1;
             KeyLengthOfData = LJobKeyInfo.LengthOfInformation;
             for keycount = 1 to LJob200.NumberOfFieldsReturned;
               LJobKeyInfo = %subst( LJob200.ReturnedData :
                   KeyStartingPosition :
                   KeyLengthOfData );
               KeyLengthOfData = LJobKeyInfo.LengthOfInformation;
               LJobKeyInfo = %subst( LJob200.ReturnedData :
                   KeyStartingPosition :
                   KeyLengthOfData );
               Endpos = LJobKeyInfo.LengthOfData;
               if     LJobKeyInfo.KeyField = 0101;
               JobbStatus = %subst( LJobKeyInfo.KeyData : 1 :  Endpos );
               elseif LJobKeyInfo.KeyField = 1906;
               Subsystem = %subst( LJobKeyInfo.KeyData : 1 : Endpos );
               endif;
               KeyStartingPosition = KeyStartingPosition + KeyLengthOfData;
             endfor;
             SubShort = %subst( Subsystem :  1 : 10 );
             // if job in message wait then email message to address in
             // variable email address
             if Jobbstatus = '
          MSGW' and SubShort <> 'QSPL';
             Subsystem = %trim( %subst( Subsystem : 11 : 10 ) ) + '
          /' +
                %trim( %subst( Subsystem :  1 : 10 ) );
             os400_cmd = '
          snddst type(*lmsg' +
                '
          tointnet((' + Q + %trim(EmailAddress1) +
                Q + '
          ) (' + Q + %trim(EmailAddress) +
                Q + '
          )) dstd(' + Q    +
                '
          Job ' + %trim( LJob200.JobName ) + ' from user '
                 + %trim( LJob200.UserName ) + ' 
          in *MSGW'   +
                Q + '
          longmsg(' + Q  +
                '
          Job (' +
                %trim( LJob200.JobName ) + '
          /' +
                %trim( LJob200.UserName ) +
                '
          subsystem ' + %trim( Subsystem ) +
                ' 
          in status *MSGW' +
                Q + '
          )';
              monitor;
                qcmdexc ( os400_cmd : %size ( os400_cmd ) );
                on-error;
               dump;
             endmon;
             endif;

             StartingPosition = StartingPosition + LengthOfData;

              endfor;

              endsr; 
          and my change comments are here:

          Eric Pell

          Comment


          • #6
            Re: Use the QUSLJOB API to whip thru WRKACTJOB

            Thanks for re-posting Eric....Nice site
            Ive used Gallery on one of the other sites I mess with..... Its nice and easy.

            Have a great weekend and lets see if we can get your posts up to at
            least double digits

            take care
            jamie
            All my answers were extracted from the "Big Dummy's Guide to the As400"
            and I take no responsibility for any of them.

            www.code400.com

            Comment

            Working...
            X