HLOPROC1 ;ALB/CJM- Process Manager - 10/4/94 1pm
;;1.6;HEALTH LEVEL SEVEN;**126**;Oct 13, 1995
;
;
GETWORK(PROCESS) ;
;finds a process that needs to be started
;
N NAME,IEN,GOTWORK
;this is how HL7 can be stopped via Taskman
I $$S^%ZTLOAD D STOPHL7 Q 0
S GOTWORK=0
S IEN=+$G(PROCESS("IEN"))
F S IEN=$O(^HLD(779.3,"C",1,IEN)) Q:IEN=$G(PROCESS("IEN")) I IEN D Q:GOTWORK
.N PROC,COUNT,QUEUED,RUNNING
.Q:'$$GETPROC(IEN,.PROC)
.Q:PROC("VMS SERVICE")
.Q:PROC("NAME")="PROCESS MANAGER"
.S PROCESS("COUNT")=1
.S QUEUED=+$G(^HLC("HL7 PROCESS COUNTS","QUEUED",PROC("NAME")))
.S:QUEUED<0 QUEUED=0
.S RUNNING=+$G(^HLC("HL7 PROCESS COUNTS","RUNNING",PROC("NAME")))
.S:RUNNING<0 RUNNING=0
.S COUNT=QUEUED+RUNNING
.I COUNT<PROC("MINIMUM") S GOTWORK=1,PROCESS("IEN")=IEN,PROCESS("NAME")=PROC("NAME"),PROCESS("COUNT")=(PROC("MINIMUM")-COUNT) Q
.I COUNT<PROC("MAXIMUM"),$$FMDIFF^XLFDT($$NOW^XLFDT,PROC("LAST DT/TM"),2)>PROC("WAIT SECONDS"),'QUEUED S GOTWORK=1,PROCESS("IEN")=IEN,PROCESS("NAME")=PROC("NAME"),PROCESS("COUNT")=1 Q
I 'GOTWORK K PROCESS
Q GOTWORK
;
DOWORK(PROCESS) ;
;starts a process
;
;don't start a new task if stopped
Q:$$CHKSTOP^HLOPROC
;
N ZTRTN,ZTDESC,ZTSAVE,ZTIO,ZTSK,I,ZTDTH
S:'$G(PROCESS("COUNT")) PROCESS("COUNT")=1
F I=1:1:PROCESS("COUNT") D
.S ZTRTN="PROCESS^HLOPROC"
.S ZTDESC="HL7 - "_PROCESS("NAME")
.S ZTIO=""
.S ZTSAVE("PROCNAME")=PROCESS("NAME")
.S ZTDTH=$H
.D ^%ZTLOAD
.I $D(ZTSK) D
..;lock before changing counts
..L +HL7("COUNTING PROCESSES"):20
..I $$INC^HLOSITE($NA(^HLC("HL7 PROCESS COUNTS","QUEUED",PROCESS("NAME"))))
..S $P(^HLD(779.3,PROCESS("IEN"),0),"^",6)=$$NOW^XLFDT,^HLTMP("HL7 QUEUED PROCESSES",ZTSK)=$H_"^"_PROCESS("NAME")
..L -HL7("COUNTING PROCESSES")
Q
;
GETPROC(IEN,PROCESS) ;
;given the ien of the HL7 Process Registry entry, returns the entry as a subscripted array in .PROCESS
;
;Output: Function returns 0 on failure, 1 on success
;
N NODE
S NODE=$G(^HLD(779.3,IEN,0))
Q:NODE="" 0
S PROCESS("NAME")=$P(NODE,"^")
S PROCESS("IEN")=IEN
S PROCESS("MINIMUM")=+$P(NODE,"^",3)
S PROCESS("MAXIMUM")=+$P(NODE,"^",4)
S PROCESS("WAIT SECONDS")=+($P(NODE,"^",5))*60
I 'PROCESS("WAIT SECONDS") S PROCESS("WAIT SECONDS")=1000
S PROCESS("LAST DT/TM")=$P(NODE,"^",6)
S PROCESS("VMS SERVICE")=$P(NODE,"^",15)
Q 1
;
STOPHL7 ;shut down HLO HL7
N ZTSK,DOLLARJ
;let other processes know that starting/stopping is underway
S $P(^HLD(779.1,1,0),"^",9)=0
S ZTSK=""
F S ZTSK=$O(^HLTMP("HL7 QUEUED PROCESSES",ZTSK)) Q:ZTSK="" D DQ^%ZTLOAD
S DOLLARJ=""
F S DOLLARJ=$O(^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)) Q:DOLLARJ="" S ZTSK=$P($G(^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)),"^",2) I ZTSK]"" D PCLEAR^%ZTLOAD(ZTSK) I $$ASKSTOP^%ZTLOAD(ZTSK)
D CHKQUED
Q
;
STARTHL7 ;start HL7 system, but first do some cleanup
;
D RECOUNT()
;
;set the system status flag to active
S $P(^HLD(779.1,1,0),"^",9)=1
;
;don't start a process manager if already running
L +^HLTMP("PROCESS MANAGER"):1
Q:'$T
;
;start the HL7 Process Manager, which will start everything else
N PROCESS
S PROCESS("NAME")="PROCESS MANAGER"
S PROCESS("IEN")=$O(^HLD(779.3,"B","PROCESS MANAGER",0))
D DOWORK(.PROCESS)
L -^HLTMP("PROCESS MANAGER")
Q
;
QUIT1(COUNT) ;just returns 1 as function value first time around,then 0, insuring that the DO WORK function is called just once
I '$G(COUNT) S COUNT=1 Q 1
Q 0
;
CHKDEAD(WORK) ;
;did any process terminate without erasing itself?
;WORK (pass by reference, not required) by the Process Manager that is not used and not required
N DOLLARJ S DOLLARJ=""
L +HL7("COUNTING PROCESSES"):20
Q:'$T
F S DOLLARJ=$O(^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)) Q:DOLLARJ="" I DOLLARJ'=$J L +^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ):0 D:$T
.L -^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)
.N PROC
.S PROC=$P($G(^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)),"^",3)
.K ^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)
.Q:PROC=""
.I $$INC^HLOSITE($NA(^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)),-1)<0,$$INC^HLOSITE($NA(^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)),1)
L -HL7("COUNTING PROCESSES")
Q
CHKQUED ;did any queued task get dequeued without being erased?
N PROC,JOB
L +HL7("COUNTING PROCESSES"):20
Q:'$T
S JOB=""
F S JOB=$O(^HLTMP("HL7 QUEUED PROCESSES",JOB)) Q:JOB="" I '$$QUEUED(JOB) D
.N PROC
.S PROC=$P($G(^HLTMP("HL7 QUEUED PROCESSES",JOB)),"^",2)
.I PROC]"",$$INC^HLOSITE($NA(^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)),-1)<0,$$INC^HLOSITE($NA(^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)),1)
.K ^HLTMP("HL7 QUEUED PROCESSES",JOB)
L -HL7("COUNTING PROCESSES")
Q
;
QUEUED(TASK) ;
;function returns 0 if ZTSK is not queued to run, 1 if it is
N ZTSK
S ZTSK=TASK
D ISQED^%ZTLOAD
Q:ZTSK(0) 1
Q 0
;
CNTLIVE ;count the running processes
N JOB,COUNTS,PROC
L +HL7("COUNTING PROCESSES"):20
Q:'$T
S JOB=""
F S JOB=$O(^HLTMP("HL7 RUNNING PROCESSES",JOB)) Q:JOB="" S PROC=$P($G(^HLTMP("HL7 RUNNING PROCESSES",JOB)),"^",3) I PROC]"" S COUNTS(PROC)=$G(COUNTS(PROC))+1
S PROC="" F S PROC=$O(COUNTS(PROC)) Q:PROC="" S ^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)=COUNTS(PROC)
S PROC="" F S PROC=$O(^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)) Q:PROC="" S ^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)=+$G(COUNTS(PROC))
L -HL7("COUNTING PROCESSES")
Q
;
CNTQUED ;count the queued tasks
N JOB,COUNTS,PROC
L +HL7("COUNTING PROCESSES"):20
Q:'$T
S JOB=""
F S JOB=$O(^HLTMP("HL7 QUEUED PROCESSES",JOB)) Q:JOB="" S PROC=$P($G(^HLTMP("HL7 QUEUED PROCESSES",JOB)),"^",2) I PROC]"" S COUNTS(PROC)=$G(COUNTS(PROC))+1
S PROC="" F S PROC=$O(COUNTS(PROC)) Q:PROC="" S ^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)=COUNTS(PROC)
S PROC="" F S PROC=$O(^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)) Q:PROC="" S ^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)=+$G(COUNTS(PROC))
L -HL7("COUNTING PROCESSES")
Q
;
RECOUNT(RECOUNT) ;check that the processes that are supposed to be running actually are, same for the queued processes
;Input:
; RECOUNT (pass by reference, optional) not used, but passed in by the process manager
;
;
;check for processes that are supposed to be running or queued but aren't
D CHKDEAD(),CHKQUED
;
;recount the processes
D CNTLIVE,CNTQUED
Q
HLOPROC1 ;ALB/CJM- Process Manager - 10/4/94 1pm
+1 ;;1.6;HEALTH LEVEL SEVEN;**126**;Oct 13, 1995
+2 ;
+3 ;
GETWORK(PROCESS) ;
+1 ;finds a process that needs to be started
+2 ;
+3 NEW NAME,IEN,GOTWORK
+4 ;this is how HL7 can be stopped via Taskman
+5 IF $$S^%ZTLOAD
DO STOPHL7
QUIT 0
+6 SET GOTWORK=0
+7 SET IEN=+$GET(PROCESS("IEN"))
+8 FOR
SET IEN=$ORDER(^HLD(779.3,"C",1,IEN))
IF IEN=$GET(PROCESS("IEN"))
QUIT
IF IEN
Begin DoDot:1
+9 NEW PROC,COUNT,QUEUED,RUNNING
+10 IF '$$GETPROC(IEN,.PROC)
QUIT
+11 IF PROC("VMS SERVICE")
QUIT
+12 IF PROC("NAME")="PROCESS MANAGER"
QUIT
+13 SET PROCESS("COUNT")=1
+14 SET QUEUED=+$GET(^HLC("HL7 PROCESS COUNTS","QUEUED",PROC("NAME")))
+15 IF QUEUED<0
SET QUEUED=0
+16 SET RUNNING=+$GET(^HLC("HL7 PROCESS COUNTS","RUNNING",PROC("NAME")))
+17 IF RUNNING<0
SET RUNNING=0
+18 SET COUNT=QUEUED+RUNNING
+19 IF COUNT<PROC("MINIMUM")
SET GOTWORK=1
SET PROCESS("IEN")=IEN
SET PROCESS("NAME")=PROC("NAME")
SET PROCESS("COUNT")=(PROC("MINIMUM")-COUNT)
QUIT
+20 IF COUNT<PROC("MAXIMUM")
IF $$FMDIFF^XLFDT($$NOW^XLFDT,PROC("LAST DT/TM"),2)>PROC("WAIT SECONDS")
IF 'QUEUED
SET GOTWORK=1
SET PROCESS("IEN")=IEN
SET PROCESS("NAME")=PROC("NAME")
SET PROCESS("COUNT")=1
QUIT
End DoDot:1
IF GOTWORK
QUIT
+21 IF 'GOTWORK
KILL PROCESS
+22 QUIT GOTWORK
+23 ;
DOWORK(PROCESS) ;
+1 ;starts a process
+2 ;
+3 ;don't start a new task if stopped
+4 IF $$CHKSTOP^HLOPROC
QUIT
+5 ;
+6 NEW ZTRTN,ZTDESC,ZTSAVE,ZTIO,ZTSK,I,ZTDTH
+7 IF '$GET(PROCESS("COUNT"))
SET PROCESS("COUNT")=1
+8 FOR I=1:1:PROCESS("COUNT")
Begin DoDot:1
+9 SET ZTRTN="PROCESS^HLOPROC"
+10 SET ZTDESC="HL7 - "_PROCESS("NAME")
+11 SET ZTIO=""
+12 SET ZTSAVE("PROCNAME")=PROCESS("NAME")
+13 SET ZTDTH=$HOROLOG
+14 DO ^%ZTLOAD
+15 IF $DATA(ZTSK)
Begin DoDot:2
+16 ;lock before changing counts
+17 LOCK +HL7("COUNTING PROCESSES"):20
+18 IF $$INC^HLOSITE($NAME(^HLC("HL7 PROCESS COUNTS","QUEUED",PROCESS("NAME"))))
+19 SET $PIECE(^HLD(779.3,PROCESS("IEN"),0),"^",6)=$$NOW^XLFDT
SET ^HLTMP("HL7 QUEUED PROCESSES",ZTSK)=$HOROLOG_"^"_PROCESS("NAME")
+20 LOCK -HL7("COUNTING PROCESSES")
End DoDot:2
End DoDot:1
+21 QUIT
+22 ;
GETPROC(IEN,PROCESS) ;
+1 ;given the ien of the HL7 Process Registry entry, returns the entry as a subscripted array in .PROCESS
+2 ;
+3 ;Output: Function returns 0 on failure, 1 on success
+4 ;
+5 NEW NODE
+6 SET NODE=$GET(^HLD(779.3,IEN,0))
+7 IF NODE=""
QUIT 0
+8 SET PROCESS("NAME")=$PIECE(NODE,"^")
+9 SET PROCESS("IEN")=IEN
+10 SET PROCESS("MINIMUM")=+$PIECE(NODE,"^",3)
+11 SET PROCESS("MAXIMUM")=+$PIECE(NODE,"^",4)
+12 SET PROCESS("WAIT SECONDS")=+($PIECE(NODE,"^",5))*60
+13 IF 'PROCESS("WAIT SECONDS")
SET PROCESS("WAIT SECONDS")=1000
+14 SET PROCESS("LAST DT/TM")=$PIECE(NODE,"^",6)
+15 SET PROCESS("VMS SERVICE")=$PIECE(NODE,"^",15)
+16 QUIT 1
+17 ;
STOPHL7 ;shut down HLO HL7
+1 NEW ZTSK,DOLLARJ
+2 ;let other processes know that starting/stopping is underway
+3 SET $PIECE(^HLD(779.1,1,0),"^",9)=0
+4 SET ZTSK=""
+5 FOR
SET ZTSK=$ORDER(^HLTMP("HL7 QUEUED PROCESSES",ZTSK))
IF ZTSK=""
QUIT
DO DQ^%ZTLOAD
+6 SET DOLLARJ=""
+7 FOR
SET DOLLARJ=$ORDER(^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ))
IF DOLLARJ=""
QUIT
SET ZTSK=$PIECE($GET(^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)),"^",2)
IF ZTSK]""
DO PCLEAR^%ZTLOAD(ZTSK)
IF $$ASKSTOP^%ZTLOAD(ZTSK)
+8 DO CHKQUED
+9 QUIT
+10 ;
STARTHL7 ;start HL7 system, but first do some cleanup
+1 ;
+2 DO RECOUNT()
+3 ;
+4 ;set the system status flag to active
+5 SET $PIECE(^HLD(779.1,1,0),"^",9)=1
+6 ;
+7 ;don't start a process manager if already running
+8 LOCK +^HLTMP("PROCESS MANAGER"):1
+9 IF '$TEST
QUIT
+10 ;
+11 ;start the HL7 Process Manager, which will start everything else
+12 NEW PROCESS
+13 SET PROCESS("NAME")="PROCESS MANAGER"
+14 SET PROCESS("IEN")=$ORDER(^HLD(779.3,"B","PROCESS MANAGER",0))
+15 DO DOWORK(.PROCESS)
+16 LOCK -^HLTMP("PROCESS MANAGER")
+17 QUIT
+18 ;
QUIT1(COUNT) ;just returns 1 as function value first time around,then 0, insuring that the DO WORK function is called just once
+1 IF '$GET(COUNT)
SET COUNT=1
QUIT 1
+2 QUIT 0
+3 ;
CHKDEAD(WORK) ;
+1 ;did any process terminate without erasing itself?
+2 ;WORK (pass by reference, not required) by the Process Manager that is not used and not required
+3 NEW DOLLARJ
SET DOLLARJ=""
+4 LOCK +HL7("COUNTING PROCESSES"):20
+5 IF '$TEST
QUIT
+6 FOR
SET DOLLARJ=$ORDER(^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ))
IF DOLLARJ=""
QUIT
IF DOLLARJ'=$JOB
LOCK +^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ):0
IF $TEST
Begin DoDot:1
+7 LOCK -^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)
+8 NEW PROC
+9 SET PROC=$PIECE($GET(^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)),"^",3)
+10 KILL ^HLTMP("HL7 RUNNING PROCESSES",DOLLARJ)
+11 IF PROC=""
QUIT
+12 IF $$INC^HLOSITE($NAME(^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)),-1)<0
IF $$INC^HLOSITE($NAME(^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)),1)
End DoDot:1
+13 LOCK -HL7("COUNTING PROCESSES")
+14 QUIT
CHKQUED ;did any queued task get dequeued without being erased?
+1 NEW PROC,JOB
+2 LOCK +HL7("COUNTING PROCESSES"):20
+3 IF '$TEST
QUIT
+4 SET JOB=""
+5 FOR
SET JOB=$ORDER(^HLTMP("HL7 QUEUED PROCESSES",JOB))
IF JOB=""
QUIT
IF '$$QUEUED(JOB)
Begin DoDot:1
+6 NEW PROC
+7 SET PROC=$PIECE($GET(^HLTMP("HL7 QUEUED PROCESSES",JOB)),"^",2)
+8 IF PROC]""
IF $$INC^HLOSITE($NAME(^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)),-1)<0
IF $$INC^HLOSITE($NAME(^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)),1)
+9 KILL ^HLTMP("HL7 QUEUED PROCESSES",JOB)
End DoDot:1
+10 LOCK -HL7("COUNTING PROCESSES")
+11 QUIT
+12 ;
QUEUED(TASK) ;
+1 ;function returns 0 if ZTSK is not queued to run, 1 if it is
+2 NEW ZTSK
+3 SET ZTSK=TASK
+4 DO ISQED^%ZTLOAD
+5 IF ZTSK(0)
QUIT 1
+6 QUIT 0
+7 ;
CNTLIVE ;count the running processes
+1 NEW JOB,COUNTS,PROC
+2 LOCK +HL7("COUNTING PROCESSES"):20
+3 IF '$TEST
QUIT
+4 SET JOB=""
+5 FOR
SET JOB=$ORDER(^HLTMP("HL7 RUNNING PROCESSES",JOB))
IF JOB=""
QUIT
SET PROC=$PIECE($GET(^HLTMP("HL7 RUNNING PROCESSES",JOB)),"^",3)
IF PROC]""
SET COUNTS(PROC)=$GET(COUNTS(PROC))+1
+6 SET PROC=""
FOR
SET PROC=$ORDER(COUNTS(PROC))
IF PROC=""
QUIT
SET ^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)=COUNTS(PROC)
+7 SET PROC=""
FOR
SET PROC=$ORDER(^HLC("HL7 PROCESS COUNTS","RUNNING",PROC))
IF PROC=""
QUIT
SET ^HLC("HL7 PROCESS COUNTS","RUNNING",PROC)=+$GET(COUNTS(PROC))
+8 LOCK -HL7("COUNTING PROCESSES")
+9 QUIT
+10 ;
CNTQUED ;count the queued tasks
+1 NEW JOB,COUNTS,PROC
+2 LOCK +HL7("COUNTING PROCESSES"):20
+3 IF '$TEST
QUIT
+4 SET JOB=""
+5 FOR
SET JOB=$ORDER(^HLTMP("HL7 QUEUED PROCESSES",JOB))
IF JOB=""
QUIT
SET PROC=$PIECE($GET(^HLTMP("HL7 QUEUED PROCESSES",JOB)),"^",2)
IF PROC]""
SET COUNTS(PROC)=$GET(COUNTS(PROC))+1
+6 SET PROC=""
FOR
SET PROC=$ORDER(COUNTS(PROC))
IF PROC=""
QUIT
SET ^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)=COUNTS(PROC)
+7 SET PROC=""
FOR
SET PROC=$ORDER(^HLC("HL7 PROCESS COUNTS","QUEUED",PROC))
IF PROC=""
QUIT
SET ^HLC("HL7 PROCESS COUNTS","QUEUED",PROC)=+$GET(COUNTS(PROC))
+8 LOCK -HL7("COUNTING PROCESSES")
+9 QUIT
+10 ;
RECOUNT(RECOUNT) ;check that the processes that are supposed to be running actually are, same for the queued processes
+1 ;Input:
+2 ; RECOUNT (pass by reference, optional) not used, but passed in by the process manager
+3 ;
+4 ;
+5 ;check for processes that are supposed to be running or queued but aren't
+6 DO CHKDEAD()
DO CHKQUED
+7 ;
+8 ;recount the processes
+9 DO CNTLIVE
DO CNTQUED
+10 QUIT