1 &ANALYZE-SUSPEND _VERSION-NUMBER UIB_v8r12
3 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _DEFINITIONS Procedure
4 /*--------------------------------------------------------------------------
5 File
: process
/bq-processor.p
6 Purpose
: Process queued programs on the system console
, rather than
7 in front of the user
, so they can get on with their work.
9 Syntax
: no parameters
- just run it
!
11 Description
: Console process for handling BatchQueue processing
13 Author
(s
) : Andrew McMillan
15 Notes
: A function to handle placing things onto the batch
16 queue has also been added to System Manager.
17 ------------------------------------------------------------------------*/
19 DEF VAR queue-name
AS CHAR NO-UNDO INITIAL "Batch-Queue".
20 DEF VAR user-name
AS CHAR NO-UNDO INITIAL "Batch-Queue".
21 DEF VAR bq-recid
AS RECID NO-UNDO.
22 DEF VAR last-today
AS DATE NO-UNDO.
23 DEF VAR start-time
AS INT NO-UNDO.
26 DEF VAR lock-filename
AS CHAR NO-UNDO.
28 ON 'ESC'
:U
ANYWHERE DO:
34 /* _UIB-CODE-BLOCK-END
*/
38 &ANALYZE-SUSPEND _UIB-PREPROCESSOR-BLOCK
40 /* ******************** Preprocessor Definitions
******************** */
42 &Scoped-define PROCEDURE-TYPE Procedure
46 /* _UIB-PREPROCESSOR-BLOCK-END
*/
50 /* ************************ Function Prototypes
********************** */
52 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD continue-processing Procedure
53 FUNCTION continue-processing
RETURNS LOGICAL
54 ( /* parameter-definitions
*/ ) FORWARD.
56 /* _UIB-CODE-BLOCK-END
*/
59 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD find-and-lock-queue-entry Procedure
60 FUNCTION find-and-lock-queue-entry
RETURNS RECID
61 ( /* parameter-definitions
*/ ) FORWARD.
63 /* _UIB-CODE-BLOCK-END
*/
66 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD post-processing Procedure
67 FUNCTION post-processing
RETURNS LOGICAL
68 ( /* parameter-definitions
*/ ) FORWARD.
70 /* _UIB-CODE-BLOCK-END
*/
73 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD setup-processing Procedure
74 FUNCTION setup-processing
RETURNS LOGICAL
75 ( /* parameter-definitions
*/ ) FORWARD.
77 /* _UIB-CODE-BLOCK-END
*/
81 /* *********************** Procedure Settings
************************ */
83 &ANALYZE-SUSPEND _PROCEDURE-SETTINGS
84 /* Settings for
THIS-PROCEDURE
88 Add Fields to
: Neither
89 Other Settings
: CODE-ONLY
COMPILE
91 &ANALYZE-RESUME _END-PROCEDURE-SETTINGS
93 /* ************************* Create Window
************************** */
95 &ANALYZE-SUSPEND _CREATE-WINDOW
96 /* DESIGN Window definition
(used by the UIB
)
97 CREATE WINDOW Procedure
ASSIGN
100 /* END WINDOW DEFINITION
*/
106 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _INCLUDED-LIB Procedure
107 /* ************************* Included-Libraries
*********************** */
109 {inc
/method
/m-txtrep.i
}
110 {inc
/method
/m-logging.i
}
112 /* _UIB-CODE-BLOCK-END
*/
117 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _MAIN-BLOCK Procedure
120 /* *************************** Main Block
*************************** */
131 /* _UIB-CODE-BLOCK-END
*/
135 /* ********************** Internal Procedures
*********************** */
137 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE apms-queue-manager Procedure
138 PROCEDURE apms-queue-manager
:
139 /*------------------------------------------------------------------------------
140 Purpose
: Identify this program as the APMS queue manager
141 ------------------------------------------------------------------------------*/
145 /* _UIB-CODE-BLOCK-END
*/
149 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE apms-system-manager Procedure
150 PROCEDURE apms-system-manager
:
151 /*------------------------------------------------------------------------------
152 Purpose
: Pretend we are the APMS system manager
, so that that functionality
153 is still available to programs running on the batch queue.
154 ------------------------------------------------------------------------------*/
158 /* _UIB-CODE-BLOCK-END
*/
162 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE clean-queue Procedure
163 PROCEDURE clean-queue
:
164 /*------------------------------------------------------------------------------
166 ------------------------------------------------------------------------------*/
167 FOR EACH BatchQueue
WHERE BatchQueue.RunStatus
BEGINS "LOCK "
168 AND BatchQueue.StartedOn
< (TODAY - 1)
170 BatchQueue.RunStatus
= "ABRT " + BatchQueue.RunStatus .
173 FOR EACH BatchQueue
WHERE BatchQueue.RunStatus
BEGINS "DONE"
174 AND BatchQueue.StartedOn
< (TODAY - 190) EXCLUSIVE-LOCK:
178 FOR EACH BatchQueue
WHERE BatchQueue.RunStatus
BEGINS "ABRT"
179 AND BatchQueue.StartedOn
< (TODAY - 700) EXCLUSIVE-LOCK:
185 /* _UIB-CODE-BLOCK-END
*/
189 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE get-username Procedure
190 PROCEDURE get-username
:
191 /*------------------------------------------------------------------------------
192 Purpose
: Return the current username for the person requesting this job
193 ------------------------------------------------------------------------------*/
194 DEF OUTPUT PARAMETER name
AS CHAR NO-UNDO.
198 /* _UIB-CODE-BLOCK-END
*/
202 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE post-process-queue-entry Procedure
203 PROCEDURE post-process-queue-entry
:
204 /*------------------------------------------------------------------------------
206 ------------------------------------------------------------------------------*/
207 DEF INPUT PARAMETER errors
AS CHAR NO-UNDO.
210 FIND BatchQueue
WHERE RECID(BatchQueue
) = bq-recid
EXCLUSIVE-LOCK.
211 BatchQueue.RunDate
= TODAY.
212 BatchQueue.RunTime
= TIME.
213 BatchQueue.Elapsed
= ETIME.
214 BatchQueue.RunStatus
= (IF TRIM(errors
) = "" THEN "DONE" ELSE "EROR") .
215 FIND CURRENT BatchQueue
NO-LOCK.
217 log
( 6, "Queue entry processed with final status '" + BatchQueue.RunStatus
+ "'" ).
219 user-name
= queue-name.
223 /* _UIB-CODE-BLOCK-END
*/
227 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE pre-process-queue-entry Procedure
228 PROCEDURE pre-process-queue-entry
:
229 /*------------------------------------------------------------------------------
231 ------------------------------------------------------------------------------*/
233 FIND BatchQueue
WHERE RECID(BatchQueue
) = bq-recid
NO-LOCK.
234 log
( 6, "Processing queue entry").
235 log
( 6, BatchQueue.RunProgram
).
236 log
( 6, REPLACE( BatchQueue.RunParameters
, "~n", "~~n") ).
239 FIND CURRENT BatchQueue
EXCLUSIVE-LOCK.
240 BatchQueue.StartedOn
= TODAY.
241 BatchQueue.StartedAt
= TIME.
242 FIND CURRENT BatchQueue
NO-LOCK.
245 user-name
= BatchQueue.UserName.
247 ETIME( Yes
).
/* Reset elapsed time counter
*/
251 /* _UIB-CODE-BLOCK-END
*/
255 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE process-queue-entry Procedure
256 PROCEDURE process-queue-entry
:
257 /*------------------------------------------------------------------------------
259 ------------------------------------------------------------------------------*/
260 DEF VAR error-messages
AS CHAR NO-UNDO INITIAL "".
261 DEF VAR i
AS INT NO-UNDO.
263 RUN pre-process-queue-entry.
265 OUTPUT TO VALUE(log-filename
) KEEP-MESSAGES APPEND.
266 RUN VALUE( BatchQueue.RunProgram
) ( BatchQueue.RunParameters
) NO-ERROR.
269 FIND BatchQueue
WHERE RECID(BatchQueue
) = bq-recid
NO-LOCK NO-ERROR.
270 IF ERROR-STATUS:ERROR THEN DO:
271 log
( 1, "Errors processing queue entry: ").
272 log
( 1, BatchQueue.RunProgram
).
273 log
( 1, BatchQueue.RunParameters
).
274 DO i
= 1 TO ERROR-STATUS:NUM-MESSAGES:
275 error-messages
= error-messages
+ "~n"
276 + STRING( ERROR-STATUS:GET-NUMBER(i
), "->>>>>>9" ) + ": "
277 + ERROR-STATUS:GET-MESSAGE(i
) .
278 log
( 1, " " + STRING( ERROR-STATUS:GET-NUMBER(i
), "->>>>>>9" ) + ": "
279 + ERROR-STATUS:GET-MESSAGE(i
) ).
283 RUN post-process-queue-entry
( error-messages
).
287 /* _UIB-CODE-BLOCK-END
*/
291 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE queue-loop Procedure
292 PROCEDURE queue-loop
:
293 /*------------------------------------------------------------------------------
294 Purpose
: Main loop for processing batch queue entries
295 ------------------------------------------------------------------------------*/
299 IF NOT continue-processing
() THEN LEAVE /* will not be restarted
*/.
300 bq-recid
= find-and-lock-queue-entry
().
301 FIND BatchQueue
WHERE RECID(BatchQueue
) = bq-recid
NO-LOCK NO-ERROR.
302 IF AVAILABLE(BatchQueue
) THEN
303 RUN process-queue-entry.
305 RUN update-queue-enabled.
307 IF last-today
<> TODAY THEN QUIT.
/* will be restarted each day
*/
308 IF TIME > start-time
+ 21600 THEN QUIT.
/* will be restarted every six hours
*/
315 /* _UIB-CODE-BLOCK-END
*/
319 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE update-queue-enabled Procedure
320 PROCEDURE update-queue-enabled
:
321 /*------------------------------------------------------------------------------
323 ------------------------------------------------------------------------------*/
325 FIND OfficeSetting
OF Office
WHERE OfficeSetting.SetName
= "Batch-Queue-Enabled"
326 EXCLUSIVE-LOCK NO-ERROR.
327 IF NOT AVAILABLE(OfficeSetting
) THEN DO:
328 CREATE OfficeSetting.
329 OfficeSettings.OfficeCode
= Office.OfficeCode.
330 OfficeSettings.SetName
= "Batch-Queue-Enabled".
332 OfficeSettings.SetValue
= STRING(TODAY,"99/99/9999") + " " + STRING(TIME).
333 FIND CURRENT OfficeSetting
NO-LOCK.
338 /* _UIB-CODE-BLOCK-END
*/
342 /* ************************ Function Implementations
***************** */
344 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION continue-processing Procedure
345 FUNCTION continue-processing
RETURNS LOGICAL
346 ( /* parameter-definitions
*/ ) :
347 /*------------------------------------------------------------------------------
349 Because
INPUT FROM doesn't accept a
NO-ERROR option
, and
350 INPUT FROM OS-DIR has a memory leak
, we have to do some
351 really obscure stuff here
!
353 First
, we output to a log so that we catch the error message
354 which
INPUT FROM generates when the file doesn't exist
, then
355 we check to see if our seek position has changed. If it has
, then we
356 assume it was because an error was written to the output stream.
359 We have to surround the
INPUT FROM with a
DO ON ERROR... because
360 otherwise the function does an effective
"RETURN ERROR".
362 We can't test
ERROR-STATUS:ERROR after this because it seems to be
366 -----------------------------------------------------------------------------*/
367 DEF VAR file-content
AS CHAR NO-UNDO INITIAL ?.
368 DEF VAR seekpos1
AS INT NO-UNDO.
369 DEF VAR seekpos2
AS INT NO-UNDO.
371 OUTPUT TO VALUE(log-filename
) KEEP-MESSAGES APPEND.
372 seekpos1
= SEEK( OUTPUT ).
373 DO ON ERROR UNDO, LEAVE:
374 INPUT FROM VALUE( lock-filename
).
376 seekpos2
= SEEK( OUTPUT ).
379 IF seekpos1
= seekpos2
THEN DO:
380 IMPORT UNFORMATTED file-content
NO-ERROR.
382 IF NOT ERROR-STATUS:ERROR AND file-content
<> "" AND file-content
<> ?
THEN RETURN Yes.
385 log
( 1, "Lock file missing - terminating" ).
386 log
( 1, file-content
).
387 log
( 1, STRING(seekpos1
) + " now " + STRING(seekpos2
)).
392 /* _UIB-CODE-BLOCK-END
*/
396 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION find-and-lock-queue-entry Procedure
397 FUNCTION find-and-lock-queue-entry
RETURNS RECID
398 ( /* parameter-definitions
*/ ) :
399 /*------------------------------------------------------------------------------
402 ------------------------------------------------------------------------------*/
403 DEF VAR bq-recid
AS RECID NO-UNDO INITIAL ?.
406 FIND FIRST BatchQueue
WHERE BatchQueue.RunStatus
= "TODO"
407 AND (BatchQueue.RunDate
<= TODAY OR BatchQueue.RunDate
= ?
)
408 AND (BatchQueue.RunTime
<= TIME OR BatchQueue.RunTime
= ?
)
409 EXCLUSIVE-LOCK NO-ERROR.
410 IF AVAILABLE(BatchQueue
) THEN DO:
411 BatchQueue.RunStatus
= "LOCK "
412 + STRING( TODAY, "99/99/9999") + " "
413 + STRING( TIME, "HH:MM:SS") + " "
415 bq-recid
= RECID(BatchQueue
).
416 log
( 3, BatchQueue.RunStatus
).
424 /* _UIB-CODE-BLOCK-END
*/
428 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION post-processing Procedure
429 FUNCTION post-processing
RETURNS LOGICAL
430 ( /* parameter-definitions
*/ ) :
431 /*------------------------------------------------------------------------------
434 ------------------------------------------------------------------------------*/
436 RETURN FALSE.
/* Function return value.
*/
440 /* _UIB-CODE-BLOCK-END
*/
444 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION setup-processing Procedure
445 FUNCTION setup-processing
RETURNS LOGICAL
446 ( /* parameter-definitions
*/ ) :
447 /*------------------------------------------------------------------------------
450 ------------------------------------------------------------------------------*/
452 log-filename
= SESSION:TEMP-DIRECTORY
453 + STRING( YEAR(TODAY), "9999" ) + "-"
454 + STRING( MONTH(TODAY), "99" ) + "-"
455 + STRING( DAY(TODAY), "99" ) + "_"
456 + user-name
+ ".LOG".
457 lock-filename
= SESSION:TEMP-DIRECTORY + user-name
+ ".LCK" .
458 OUTPUT TO VALUE( lock-filename
) .
459 PUT UNFORMATTED "Queue processing started at "
460 + STRING( TODAY, "99/99/9999") + " "
461 + STRING( TIME, "HH:MM:SS") SKIP.
464 log
( 0, "APMS Queue Manager running" ).
466 DEF VAR wh
AS WIDGET-HANDLE NO-UNDO.
467 wh
= SESSION:FIRST-CHILD.
468 wh
:PRIVATE-DATA = "apms-queue-manager," + STRING(THIS-PROCEDURE:HANDLE).
469 wh
:TITLE = "APMS Queue Manager".
471 RETURN ERROR-STATUS:ERROR.
475 /* _UIB-CODE-BLOCK-END
*/