ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Set timeOut for calling a program - Other thread ?

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

  • Set timeOut for calling a program - Other thread ?

    Hi

    is there anyway to set a timeOut/timer (ex: for 3 min) in RPGLE program 'A' ? )
    If program 'A' CALL(E) a program 'B' and if the time for calling program 'B' exceed 3 min then I want to end/terminate the calling for program 'B'

    Thank you

  • #2
    Re: Set timeOut for calling a program - Other thread ?

    I think a little more explanation is required as to what you are trying to achieve here.

    Are you saying that if program B does not complete within 3 minutes, then it should be ended? If this is what you want to achieve then you would need to submit program B in batch and have program A check for the timer and end the batch job, that is program B, if not already complete.

    Or if program does not get to the portion of calling program B within 3 mins of the start time you want to skip the call? In this case you can check the timestamps between the start of Program A and Program B and if the difference is greater than 3 mins, then skip the call to program B.

    While these are technically possible, I do not understand the need for such a mechanism.

    Comment


    • #3
      Re: Set timeOut for calling a program - Other thread ?

      The easiest way I think to do this is with DATAQ's. If you create 2 dataq's(RQSTQ - regular FIFO Queue, and RSPQ - Keyed Queue). Then Program A can write to RQSTQ and pass in some data(with the key in that data). Then program A will listen to the RSPQ by the key. On this listen you will want to set your wait time to be the number of seconds desired(180 in this case). In program B you will listen indefinitely( wait time -1 ) to the RQSTQ. Once a record is written there then you will need to get the current timestamp. Then do the normal processing. When you are ready to respond then you get the current timestamp again. Then you can use %diff to see if you went over 180 seconds. If so, then don't write out to the RSPQ. You will need to just loop back around to reading the RQSTQ. If you haven't timed out. Then write to the RSPQ and then loop back around.

      Program A
      Code:
      Write To RQSTQ;
      Read From RSPQ - wait 180 seconds;
      
      if dataFromRSPQ not equal *Blanks;
        processResponse( dataFromRSPQ );
      endif;
      Program B
      Code:
      dow stop = off;
        Read From RQST - Wait until there is data in queue;
        start_time = %TimeStamp();
        // do processing code
      
        stop_time = %TimeStamp();
        if Not %diff( stop_time : start_time : *seconds ) > 180;
          write to RSPQ;
        endif;
      enddo;
      This is meant to be pseudo code just to give you an idea of how to accomplish what you are asking for.

      Comment


      • #4
        Re: Set timeOut for calling a program - Other thread ?

        Consider what you want to happen when the program isn't responding. Do you just want to "kill" it, i.e. end it abnormally, no matter where it happens to be in it's processing? Possibly if everything is carefully written in that program, using committment control, etc, this might be safe to do?? but I would make sure you know what the ramifications are.

        If you do decide to do this, it can be done all in a single job (without need for submitting) by using signals. The pseudo code is like this:

        1) Set up a signal handler subprocedure.
        2) Set an alarm() signal to fire in 180 seconds.
        3) Call the program.
        4) After program ends, disable the alarm signal.
        5) If alarm handler fires, then you know it didn't complete -- send an *ESCAPE mesage back to the call stack that called the program. This will kill it.

        Of course, submitting the job is an easy way as well. Not sure that data queues are needed here, unless you have more advanced needs, you can do it like this:

        1) Create a message queue for monitoring this program with the CRTMSGQ command.
        2) Submit the job with SBMJOB. Use the MSGQ parameter to point to the new message queue.
        3) Sit on RCVMSG for 180 seconds on that message queue, waiting for the program to end.
        4) If you time out (don't get the message in 180 seconds) run the ENDJOB command.

        There are at least a few other ways to do this (data queues, sockets, spawn API, mult-threaded programming, etc.) but the two I've outlined above seem like the simplest.

        The biggest problem, like I said at the start, is what happens when the program gets killed suddenly. If the program isn't designed for this, it could cause some real problems. The best solution of all is to rewrite that program to do it's own timing out so it can clean up properly after itself when timeouts occur. If you have the source code for that program, that's definitely better than having the caller force it to time out.

        Comment


        • #5
          Re: Set timeOut for calling a program - Other thread ?

          Thank you guys , I wanna check that if I can kill program 'B' without problems as what Scott said . thanx for this note

          Comment


          • #6
            Re: Set timeOut for calling a program - Other thread ?

            yes, I meane that if program B does not complete within 3 minutes, then it should be ended.
            thank you

            Comment


            • #7
              Re: Set timeOut for calling a program - Other thread ?

              yes, I meane that if program B does not complete within 3 minutes, then it should be ended.
              thank you

              Comment


              • #8
                What if that program is an interactive program?

                Comment


                • #9
                  The signal/escape method should work fine in an interactive application.

                  Comment

                  Working...
                  X