How to check program has already been executed
在有些情況下,Add-On 程式不能同時執行,這時則必需使用下面 Function 判斷程式是否已經有在執行,主要是透過產生 SM12 的 Lock Argument 的方式,確認目前執行時,沒有相同的 Lock Argument 存在
CALL FUNCTION 'ENQUEUE_ESINDX'
EXPORTING RELID = 'ZZ'
SRTFD = SY-CPROG
SRTF2 = 0
_SCOPE = '3'
EXCEPTIONS FOREIGN_LOCK = 1
SYSTEM_FAILURE = 2.
參數說明 :
RELID = 'ZZ' "Hard code
SRTFD = SY-CPROG "Enqueue 的參數2 - 目前執行的程式名稱
SRTF2 = 0 "Enqueue 的參數2 - 依情況決定是否使用,例如 0 已被 Lock, 則加 1,最多只能到 5 (表示同時只能執行五次此程式)
_SCOPE = '3' "請參考 SAP _SCOPE
透過上面程式,在 START-OF-SELECTION 時,即可防止相同程式在同一時間被兩名 USER or 不同 SESSION 執行
SAMPLE CODE:
REPORT Z_EX_CHECK_PROG_RUN.
DATA: PRG LIKE INDX-SRTFD.
DATA: L_MSG(200) TYPE C.
START-OF-SELECTION.
PRG = 'Z_EX_CHECK_PROG_RUN'.
CALL FUNCTION 'ENQUEUE_ESINDX'
EXPORTING RELID = 'ZZ'
SRTFD = PRG
SRTF2 = 0
_SCOPE = '3'
EXCEPTIONS FOREIGN_LOCK = 1
SYSTEM_FAILURE = 2.
IF SY-SUBRC NE 0.
CONCATENATE 'ERROR:' PRG 'is already running'
INTO L_MSG
SEPARATED BY SPACE.
MESSAGE L_MSG TYPE 'I'.
ENDIF.
END-OF-SELECTION.
How to check background JOB name has already been executed
但有時相同程式排 Background JOB,但有可能會同時執行但進行不同的工作,此時就要改用 Background JOB NAME 來卡控
CALL FUNCTION 'GET_JOB_RUNTIME_INFO'
* IMPORTING
* EVENTID =
* EVENTPARM =
* EXTERNAL_PROGRAM_ACTIVE =
* JOBCOUNT =
* JOBNAME =
* STEPCOUNT =
* EXCEPTIONS
* NO_RUNTIME_INFO = 1
* OTHERS = 2
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
[CAUTION] : When you execute below program. You have to execute this program by SM36 and assign the same JOB NAME.
SAMPLE CODE:
REPORT Z_EX_CHECK_JOB_NAME.
TABLES: BKPF.
DATA: L_JOBNAME LIKE TBTCM-JOBNAME.
DATA: LT_TBTCO LIKE TBTCO.
DATA: L_RUN TYPE C.
START-OF-SELECTION.
CALL FUNCTION 'GET_JOB_RUNTIME_INFO'
IMPORTING
* EVENTID =
* EVENTPARM =
* EXTERNAL_PROGRAM_ACTIVE =
* JOBCOUNT =
JOBNAME = L_JOBNAME
* STEPCOUNT =
EXCEPTIONS
NO_RUNTIME_INFO = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE 'No JOB NAME' TYPE 'E'.
ELSE.
WRITE L_JOBNAME.
WRITE :/ ''.
WRITE : SY-DATUM, SY-UZEIT, SY-CPROG, SY-MANDT.
WRITE :/ ''.
CLEAR: L_RUN, LT_TBTCO.
SELECT DISTINCT O~JOBNAME O~JOBCOUNT O~SDLUNAME
INTO (LT_TBTCO-JOBNAME,LT_TBTCO-JOBCOUNT,LT_TBTCO-SDLUNAME)
FROM TBTCO AS O
INNER JOIN TBTCP AS P
ON P~JOBNAME = O~JOBNAME
AND P~JOBCOUNT = O~JOBCOUNT
WHERE P~PROGNAME = SY-CPROG
AND O~STATUS = 'R'
AND O~JOBNAME = L_JOBNAME
AND O~AUTHCKMAN = SY-MANDT.
ADD 1 TO L_RUN.
ENDSELECT.
WRITE :/ ''.
WRITE : 'L_RUN:', L_RUN.
ENDIF.
IF L_RUN > 1.
MESSAGE 'Job name has already executed.' TYPE 'E'.
ENDIF.
WAIT UP TO 30 SECONDS.
END-OF-SELECTION.
沒有留言:
張貼留言