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
43 &Scoped-define DB-AWARE no
47 /* _UIB-PREPROCESSOR-BLOCK-END
*/
51 /* ************************ Function Prototypes
********************** */
53 &IF DEFINED(EXCLUDE-continue-processing) = 0 &THEN
55 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD continue-processing Procedure
56 FUNCTION continue-processing
RETURNS LOGICAL
57 ( /* parameter-definitions
*/ ) FORWARD.
59 /* _UIB-CODE-BLOCK-END
*/
64 &IF DEFINED(EXCLUDE-find-and-lock-queue-entry) = 0 &THEN
66 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD find-and-lock-queue-entry Procedure
67 FUNCTION find-and-lock-queue-entry
RETURNS RECID
68 ( /* parameter-definitions
*/ ) FORWARD.
70 /* _UIB-CODE-BLOCK-END
*/
75 &IF DEFINED(EXCLUDE-post-processing) = 0 &THEN
77 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD post-processing Procedure
78 FUNCTION post-processing
RETURNS LOGICAL
79 ( /* parameter-definitions
*/ ) FORWARD.
81 /* _UIB-CODE-BLOCK-END
*/
86 &IF DEFINED(EXCLUDE-setup-processing) = 0 &THEN
88 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD setup-processing Procedure
89 FUNCTION setup-processing
RETURNS LOGICAL
90 ( /* parameter-definitions
*/ ) FORWARD.
92 /* _UIB-CODE-BLOCK-END
*/
98 /* *********************** Procedure Settings
************************ */
100 &ANALYZE-SUSPEND _PROCEDURE-SETTINGS
101 /* Settings for
THIS-PROCEDURE
105 Add Fields to
: Neither
106 Other Settings
: CODE-ONLY
COMPILE
108 &ANALYZE-RESUME _END-PROCEDURE-SETTINGS
110 /* ************************* Create Window
************************** */
112 &ANALYZE-SUSPEND _CREATE-WINDOW
113 /* DESIGN Window definition
(used by the UIB
)
114 CREATE WINDOW Procedure
ASSIGN
117 /* END WINDOW DEFINITION
*/
121 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _INCLUDED-LIB Procedure
122 /* ************************* Included-Libraries
*********************** */
124 {inc
/method
/m-txtrep.i
}
125 {inc
/method
/m-logging.i
}
127 /* _UIB-CODE-BLOCK-END
*/
134 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _MAIN-BLOCK Procedure
137 /* *************************** Main Block
*************************** */
148 /* _UIB-CODE-BLOCK-END
*/
152 /* ********************** Internal Procedures
*********************** */
154 &IF DEFINED(EXCLUDE-apms-queue-manager) = 0 &THEN
156 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE apms-queue-manager Procedure
157 PROCEDURE apms-queue-manager
:
158 /*------------------------------------------------------------------------------
159 Purpose
: Identify this program as the APMS queue manager
160 ------------------------------------------------------------------------------*/
164 /* _UIB-CODE-BLOCK-END
*/
169 &IF DEFINED(EXCLUDE-apms-system-manager) = 0 &THEN
171 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE apms-system-manager Procedure
172 PROCEDURE apms-system-manager
:
173 /*------------------------------------------------------------------------------
174 Purpose
: Pretend we are the APMS system manager
, so that that functionality
175 is still available to programs running on the batch queue.
176 ------------------------------------------------------------------------------*/
180 /* _UIB-CODE-BLOCK-END
*/
185 &IF DEFINED(EXCLUDE-clean-queue) = 0 &THEN
187 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE clean-queue Procedure
188 PROCEDURE clean-queue
:
189 /*------------------------------------------------------------------------------
191 ------------------------------------------------------------------------------*/
192 FOR EACH BatchQueue
WHERE BatchQueue.RunStatus
BEGINS "LOCK "
193 AND BatchQueue.StartedOn
< (TODAY - 1)
195 BatchQueue.RunStatus
= "ABRT " + BatchQueue.RunStatus .
198 FOR EACH BatchQueue
WHERE BatchQueue.RunStatus
BEGINS "DONE"
199 AND BatchQueue.StartedOn
< (TODAY - 190) EXCLUSIVE-LOCK:
203 FOR EACH BatchQueue
WHERE BatchQueue.RunStatus
BEGINS "ABRT"
204 AND BatchQueue.StartedOn
< (TODAY - 700) EXCLUSIVE-LOCK:
210 /* _UIB-CODE-BLOCK-END
*/
215 &IF DEFINED(EXCLUDE-get-username) = 0 &THEN
217 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE get-username Procedure
218 PROCEDURE get-username
:
219 /*------------------------------------------------------------------------------
220 Purpose
: Return the current username for the person requesting this job
221 ------------------------------------------------------------------------------*/
222 DEF OUTPUT PARAMETER name
AS CHAR NO-UNDO.
224 /* log
( 3, "Running report for " + user-name
).
*/
227 /* _UIB-CODE-BLOCK-END
*/
232 &IF DEFINED(EXCLUDE-post-process-queue-entry) = 0 &THEN
234 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE post-process-queue-entry Procedure
235 PROCEDURE post-process-queue-entry
:
236 /*------------------------------------------------------------------------------
238 ------------------------------------------------------------------------------*/
239 DEF INPUT PARAMETER errors
AS CHAR NO-UNDO.
242 FIND BatchQueue
WHERE RECID(BatchQueue
) = bq-recid
EXCLUSIVE-LOCK.
243 BatchQueue.RunDate
= TODAY.
244 BatchQueue.RunTime
= TIME.
245 BatchQueue.Elapsed
= ETIME.
246 BatchQueue.RunStatus
= (IF TRIM(errors
) = "" THEN "DONE" ELSE "EROR") .
247 FIND CURRENT BatchQueue
NO-LOCK.
249 log
( 6, "Queue entry processed with final status '" + BatchQueue.RunStatus
+ "'" ).
251 user-name
= queue-name.
255 /* _UIB-CODE-BLOCK-END
*/
260 &IF DEFINED(EXCLUDE-pre-process-queue-entry) = 0 &THEN
262 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE pre-process-queue-entry Procedure
263 PROCEDURE pre-process-queue-entry
:
264 /*------------------------------------------------------------------------------
266 ------------------------------------------------------------------------------*/
268 FIND BatchQueue
WHERE RECID(BatchQueue
) = bq-recid
NO-LOCK.
269 log
( 6, "Processing queue entry").
270 log
( 6, BatchQueue.RunProgram
).
271 log
( 6, REPLACE( BatchQueue.RunParameters
, "~n", "~~n") ).
274 FIND CURRENT BatchQueue
EXCLUSIVE-LOCK.
275 BatchQueue.StartedOn
= TODAY.
276 BatchQueue.StartedAt
= TIME.
277 FIND CURRENT BatchQueue
NO-LOCK.
280 user-name
= BatchQueue.UserName.
282 ETIME( Yes
).
/* Reset elapsed time counter
*/
286 /* _UIB-CODE-BLOCK-END
*/
291 &IF DEFINED(EXCLUDE-process-queue-entry) = 0 &THEN
293 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE process-queue-entry Procedure
294 PROCEDURE process-queue-entry
:
295 /*------------------------------------------------------------------------------
297 ------------------------------------------------------------------------------*/
298 DEF VAR error-messages
AS CHAR NO-UNDO INITIAL "".
299 DEF VAR i
AS INT NO-UNDO.
301 RUN pre-process-queue-entry.
303 OUTPUT TO VALUE(log-filename
) KEEP-MESSAGES APPEND.
304 RUN VALUE( BatchQueue.RunProgram
) ( BatchQueue.RunParameters
) NO-ERROR.
307 FIND BatchQueue
WHERE RECID(BatchQueue
) = bq-recid
NO-LOCK NO-ERROR.
308 IF ERROR-STATUS:ERROR THEN DO:
309 log
( 1, "Errors processing queue entry: ").
310 log
( 1, BatchQueue.RunProgram
).
311 log
( 1, BatchQueue.RunParameters
).
312 DO i
= 1 TO ERROR-STATUS:NUM-MESSAGES:
313 error-messages
= error-messages
+ "~n"
314 + STRING( ERROR-STATUS:GET-NUMBER(i
), "->>>>>>9" ) + ": "
315 + ERROR-STATUS:GET-MESSAGE(i
) .
316 log
( 1, " " + STRING( ERROR-STATUS:GET-NUMBER(i
), "->>>>>>9" ) + ": "
317 + ERROR-STATUS:GET-MESSAGE(i
) ).
321 RUN post-process-queue-entry
( error-messages
).
325 /* _UIB-CODE-BLOCK-END
*/
330 &IF DEFINED(EXCLUDE-queue-loop) = 0 &THEN
332 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE queue-loop Procedure
333 PROCEDURE queue-loop
:
334 /*------------------------------------------------------------------------------
335 Purpose
: Main loop for processing batch queue entries
336 ------------------------------------------------------------------------------*/
340 IF NOT continue-processing
() THEN LEAVE /* will not be restarted
*/.
341 bq-recid
= find-and-lock-queue-entry
().
342 FIND BatchQueue
WHERE RECID(BatchQueue
) = bq-recid
NO-LOCK NO-ERROR.
343 IF AVAILABLE(BatchQueue
) THEN
344 RUN process-queue-entry.
346 RUN update-queue-enabled.
348 IF last-today
<> TODAY THEN QUIT.
/* will be restarted each day
*/
349 IF TIME > start-time
+ 21600 THEN QUIT.
/* will be restarted every six hours
*/
356 /* _UIB-CODE-BLOCK-END
*/
361 &IF DEFINED(EXCLUDE-update-queue-enabled) = 0 &THEN
363 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE update-queue-enabled Procedure
364 PROCEDURE update-queue-enabled
:
365 /*------------------------------------------------------------------------------
367 ------------------------------------------------------------------------------*/
369 FIND OfficeSetting
OF Office
WHERE OfficeSetting.SetName
= "Batch-Queue-Enabled"
370 EXCLUSIVE-LOCK NO-ERROR.
371 IF NOT AVAILABLE(OfficeSetting
) THEN DO:
372 CREATE OfficeSetting.
373 OfficeSettings.OfficeCode
= Office.OfficeCode.
374 OfficeSettings.SetName
= "Batch-Queue-Enabled".
376 OfficeSettings.SetValue
= STRING(TODAY,"99/99/9999") + " " + STRING(TIME).
377 FIND CURRENT OfficeSetting
NO-LOCK.
382 /* _UIB-CODE-BLOCK-END
*/
387 /* ************************ Function Implementations
***************** */
389 &IF DEFINED(EXCLUDE-continue-processing) = 0 &THEN
391 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION continue-processing Procedure
392 FUNCTION continue-processing
RETURNS LOGICAL
393 ( /* parameter-definitions
*/ ) :
394 /*------------------------------------------------------------------------------
396 Because
INPUT FROM doesn't accept a
NO-ERROR option
, and
397 INPUT FROM OS-DIR has a memory leak
, we have to do some
398 really obscure stuff here
!
400 First
, we output to a log so that we catch the error message
401 which
INPUT FROM generates when the file doesn't exist
, then
402 we check to see if our seek position has changed. If it has
, then we
403 assume it was because an error was written to the output stream.
406 We have to surround the
INPUT FROM with a
DO ON ERROR... because
407 otherwise the function does an effective
"RETURN ERROR".
409 We can't test
ERROR-STATUS:ERROR after this because it seems to be
413 -----------------------------------------------------------------------------*/
414 DEF VAR file-content
AS CHAR NO-UNDO INITIAL ?.
415 DEF VAR seekpos1
AS INT NO-UNDO.
416 DEF VAR seekpos2
AS INT NO-UNDO.
418 OUTPUT TO VALUE(log-filename
) KEEP-MESSAGES APPEND.
419 seekpos1
= SEEK( OUTPUT ).
420 DO ON ERROR UNDO, LEAVE:
421 INPUT FROM VALUE( lock-filename
).
423 seekpos2
= SEEK( OUTPUT ).
426 IF seekpos1
= seekpos2
THEN DO:
427 IMPORT UNFORMATTED file-content
NO-ERROR.
429 IF NOT ERROR-STATUS:ERROR AND file-content
<> "" AND file-content
<> ?
THEN RETURN Yes.
432 log
( 1, "Lock file missing - terminating" ).
433 log
( 1, file-content
).
434 log
( 1, STRING(seekpos1
) + " now " + STRING(seekpos2
)).
439 /* _UIB-CODE-BLOCK-END
*/
444 &IF DEFINED(EXCLUDE-find-and-lock-queue-entry) = 0 &THEN
446 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION find-and-lock-queue-entry Procedure
447 FUNCTION find-and-lock-queue-entry
RETURNS RECID
448 ( /* parameter-definitions
*/ ) :
449 /*------------------------------------------------------------------------------
452 ------------------------------------------------------------------------------*/
453 DEF VAR bq-recid
AS RECID NO-UNDO INITIAL ?.
456 FIND FIRST BatchQueue
WHERE BatchQueue.RunStatus
= "TODO"
457 AND (BatchQueue.RunDate
<= TODAY OR BatchQueue.RunDate
= ?
)
458 AND (BatchQueue.RunTime
<= TIME OR BatchQueue.RunTime
= ?
)
459 EXCLUSIVE-LOCK NO-ERROR.
460 IF AVAILABLE(BatchQueue
) THEN DO:
461 BatchQueue.RunStatus
= "LOCK "
462 + STRING( TODAY, "99/99/9999") + " "
463 + STRING( TIME, "HH:MM:SS") + " "
465 bq-recid
= RECID(BatchQueue
).
466 log
( 3, BatchQueue.RunStatus
).
474 /* _UIB-CODE-BLOCK-END
*/
479 &IF DEFINED(EXCLUDE-post-processing) = 0 &THEN
481 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION post-processing Procedure
482 FUNCTION post-processing
RETURNS LOGICAL
483 ( /* parameter-definitions
*/ ) :
484 /*------------------------------------------------------------------------------
487 ------------------------------------------------------------------------------*/
489 RETURN FALSE.
/* Function return value.
*/
493 /* _UIB-CODE-BLOCK-END
*/
498 &IF DEFINED(EXCLUDE-setup-processing) = 0 &THEN
500 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION setup-processing Procedure
501 FUNCTION setup-processing
RETURNS LOGICAL
502 ( /* parameter-definitions
*/ ) :
503 /*------------------------------------------------------------------------------
506 ------------------------------------------------------------------------------*/
508 log-filename
= SESSION:TEMP-DIRECTORY
509 + STRING( YEAR(TODAY), "9999" ) + "-"
510 + STRING( MONTH(TODAY), "99" ) + "-"
511 + STRING( DAY(TODAY), "99" ) + "_"
512 + user-name
+ ".LOG".
513 lock-filename
= SESSION:TEMP-DIRECTORY + user-name
+ ".LCK" .
514 OUTPUT TO VALUE( lock-filename
) .
515 PUT UNFORMATTED "Queue processing started at "
516 + STRING( TODAY, "99/99/9999") + " "
517 + STRING( TIME, "HH:MM:SS") SKIP.
520 log
( 0, "APMS Queue Manager running" ).
522 DEF VAR wh
AS WIDGET-HANDLE NO-UNDO.
523 SESSION:CONTEXT-HELP-FILE = STRING(THIS-PROCEDURE).
525 wh
= SESSION:FIRST-CHILD.
526 IF VALID-HANDLE(wh
) THEN DO:
527 wh
:PRIVATE-DATA = "apms-queue-manager," + STRING(THIS-PROCEDURE:HANDLE).
529 /* Pointless to set the title on Linux since we really do background
*/
530 IF OPSYS EQ "UNIX" THEN DO:
531 UNIX SILENT "apms_bq_handle
534 wh:TITLE = "APMS Queue Manager
".
539 &IF DEFINED(BQ-MGR) &THEN
540 log( 1, "BQ-MGR is defined
" ).
541 IF VALID-HANDLE(bq-mgr) THEN DO:
542 DEF VAR xx AS CHAR INITIAL 'no such user'.
543 RUN get-username IN bq-mgr ( OUTPUT xx ).
544 log( 3, "Report user is
" + xx ).
548 &IF DEFINED(SYS-MGR) &THEN
549 log( 1, "SYS-MGR is defined
" ).
552 RETURN ERROR-STATUS:ERROR.
556 /* _UIB-CODE-BLOCK-END */