It's easily done using the Register Call Stack Termination User Exit Procedure (CEERTX).
Mark B. recently sent me a note asking if there was a way to perform some cleanup functions in his program when someone issues an ENDJOB command while his program is running. The answer, as you might expect, is "yes" and involves an API that actually covers more areas than just the ENDJOB command.
Mentioned in the ILE RPG Programmer's Guide under the topic Using Cancel Handlers, the API Register Call Stack Entry Termination User Exit Procedure (CEERTX) allows you to specify (register) a procedure that is to be called when the procedure calling the CEERTX API ends for any reason other than a return to the caller. The ILE RPG Programmer's Guide points out that this API can be used in situations such as a user ending the program with a system request "2" (End previous request) or answering an inquiry message with an option such as "C" (Cancel), but it also allows you to gain control when commands such as ENDJOB and ENDSBS are causing your job to end.
The API itself is very simple. It has one required parameter and two omissible parameters. The required parameter is a procedure pointer identifying the procedure you are registering and want called when the current application procedure ends without a return to its caller.
The first omissible parameter is a pointer to whatever data, if any, you want passed to the registered procedure identified by the previous required parameter. This pointer is the only parameter passed to the registered procedure.
The second omissible parameter is an output parameter from the CEERTX API providing feedback on whether or not the API call was successful. You can think of this parameter as serving the same general purpose as the QUSEC Error code data structure used by many non-CEE system APIs. In this article, we will not be passing this parameter. Not specifying this parameter when calling the CEERTX API will cause the API to send exception messages if a problem is found. This is similar to how Error code–based system APIs send an exception message when either the Error code parameter is not passed, or the Error code Bytes provided field is set to a value of 0, and an error is encountered during the running of the API.
With that introduction to the CEERTX API, here is a very simple example of registering the procedure CleanUp with the main procedure of an ILE program named IAMENDING (I am ending).
h dftactgrp(*no)
d SetCleanUp pr extproc('CEERTX')
d CleanUp_Ptr * const procptr
d InpPtr * options(*omit)
d FC 12a options(*omit)
d CleanUp pr
d InpPtr * const
d A s 10i 0
d B s 10i 0
/free
B /= A;
*inlr = *on;
return;
// **************************************************
begsr *inzsr;
SetCleanUp(%paddr(CleanUp) :*omit :*omit);
endsr;
/end-free
******************************************************
p CleanUp b
d CleanUp pi
d InpPtr * const
/free
dsply 'CleanUp is active';
/end-free
p CleanUp e
In the *INZSR subroutine, the program calls the CEERTX API (prototyped as SetCleanUp), passing the address of the CleanUp procedure as the first parameter. The second and third parameters, as mentioned earlier, are omitted.
Upon returning from the *INZSR subroutine, IAMENDING attempts to divide by 0. This causes the inquiry message RNQ0102 (Attempt to divide by zero (C G D F)) to be sent. If the operator responds to this message with C (Cancel), D (Obtain RPG formatted dump), S (Obtain system dump), or F (Obtain full formatted dump), these all result in the IAMENDING program ending without a return to the caller. This then causes the CleanUp procedure to be run, which for example purposes simply DSPLYs the text "CleanUp is active." In a production environment, this is where you would do whatever cleaning up is desired.
Assuming that you have stored the above program source in source file QRPGLESRC and that the library containing QRPGLESRC is in your current library list, then you can create the IAMENDING program with the following command:
CrtBndRPG Pgm(IAMENDING)
To test the program, you can enter the following command:
Call Pgm(IAMENDING)
You will get the inquiry message and, if you take one of the "cancel" options, you'll find the DSPLY output being written. Pretty simple, but before closing, I would like to point out a few notes.
If you have either a Monitor or a *PSSR in effect when an error is encountered in the application program, then the registered procedure will not be called. This is due to both the active monitor and the *PSSR leaving the application program, from a system point of view, in a "normal" state and the application program simply doing a return to the caller.
If, for example, we changed the main procedure statement
B /= A;
to
monitor;
B /= A;
on-error;
endmon;
then the attempt to divide by 0 (escape message MCH1211, Attempt made to divide by zero for fixed point operation) is to be handled by the monitor/on-error, and CleanUp is to not be called. The registered CleanUp procedure would, however, still be called for external to the application program events such as an ENDJOB.
To test an ENDJOB scenario, replace the original line
B /= A;
with
dow A = A;
enddo;
This will put IAMENDING into a rather tight loop when called. Call the program and then issue an ENDJOB against the job running IAMENDING. In the job log of the ended job, you'll find the message "Cleanup is active."
When an ENDJOB is run with OPTION(*CNTRLD), the registered procedure is not called until the system converts the controlled end to an immediate end. That is, the amount of time specified for the DELAY parameter expires. For those of you using an RPG function such as %SHTDN (Shut down), this is quite different and, I believe, a significant improvement. With %SHTDN, you are in a race condition to test the shut down being requested while the controlled end interval is still in effect and then complete your processing before the system converts the request to an immediate end. This race condition does not exist with the CEERTX registered procedure approach.
The example program provided in this article is registering the procedure CleanUp to the main procedure of IAMENDING. You can, however, also register cleanup procedures to subprocedures of your program. An example of doing this, and using the omissible second parameter as an input to the registered procedure, can be found in the previously referenced RPG Programmer's Guide under the topic Using Cancel Handlers.
In general, meaning there are exceptions depending on what the registered procedure is doing, when your registered procedure gets control, there is no time limit as to how long it can run. So make sure you don't fall into a looping condition with no exit. In one of my test cases for this article, I did introduce an endless loop. After running an ENDJOB against the program, I then had the opportunity to—very patiently, of course—wait 10 more minutes before I could run an ENDJOBABN to end my test case.
As usual, if you have any API questions, send them to me at
LATEST COMMENTS
MC Press Online