REPORT ZIMRE000.
************************************************************************
* This program limits the number of parallel instances a particular
* heavy batch job (JOB_NAME) can have.
* The program has to be scheduled as the first step of a job, while the
* heavy batch job should be the second step. The first step (this
* program) checks the number of jobs already running, and if it is
* over the limit (MAX_NUM) it reschedules itself to run in X minutes
* (X selected by a radiobutton), and then aborts. A variant with
* the corresponding MAX_NUM, X and JOB_NAME has to be created before
* scheduling the job.
* To prevent a deadlock (too many batch job start at the same time, and
* they think that there are already many jobs running, and they are
* rescheduling themselves to run again X time later - causing the same
*  situation again) X is varied by a random +/- 1 minute.
************************************************************************
TABLES: TBTCO.
SELECTION-SCREEN BEGIN OF BLOCK BLK WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(40) A.
PARAMETERS: JOB_NAME(30).
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(40) B.
PARAMETERS: MAX_NUM TYPE N.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN COMMENT /1(50) C.
PARAMETERS: 5_MIN   RADIOBUTTON GROUP PERI,
            10_MIN  RADIOBUTTON GROUP PERI,
            30_MIN  RADIOBUTTON GROUP PERI,
            1_HOUR      RADIOBUTTON GROUP PERI,
            2_HOURS     RADIOBUTTON GROUP PERI,
            4_HOURS     RADIOBUTTON GROUP PERI,
            8_HOURS     RADIOBUTTON GROUP PERI.
SELECTION-SCREEN END OF BLOCK BLK.
*
INITIALIZATION.
  A = 'THE NAME OF THE BACH JOB:'.
  B = 'MAXIMUM NUMBER OF CONCURRENT BATCH JOBS:'.
  C = 'RESTART IN:'.
*
START-OF-SELECTION.
  DATA: COUNTER TYPE I.
  DATA: UZENET(40).
  DATA: DATUM TYPE D, TIME TYPE T.
  DATA: NEW_DATUM TYPE D, NEW_TIME TYPE T.
  DATA: AMOUNT TYPE T.
  DATA: NEW_JOBHEAD LIKE TBTCJOB.
  DATA: T TYPE I.
* Some preparations
  IF 5_MIN = 'X'.
    AMOUNT = 300.
  ELSEIF 10_MIN = 'X'.
    AMOUNT = 600.
  ELSEIF 30_MIN = 'X'.
    AMOUNT = 1800.
  ELSEIF 1_HOUR = 'X'.
    AMOUNT = 3600.
  ELSEIF 2_HOURS = 'X'.
    AMOUNT = 7200.
  ELSEIF 4_HOURS = 'X'.
    AMOUNT = 14400.
  ELSEIF 8_HOURS = 'X'.
    AMOUNT = 28800.
  ENDIF.
*
  GET RUN TIME FIELD T.
  GET TIME.
  DATUM = SY-DATUM.
  TIME = SY-UZEIT.
  TIME = TIME + AMOUNT.
  IF TIME  9. EXIT. ENDIF.
    ENDDO.
*
    CALL FUNCTION 'JOB_CLOSE'
         EXPORTING
              JOBNAME   = NEW_JOBHEAD-JOBNAME
              JOBCOUNT  = NEW_JOBHEAD-JOBCOUNT
              SDLSTRTDT = DATUM
              SDLSTRTTM = TIME.
* Abort the second step
    UZENET = 'It is not ERROR! Rescheduling!'.
    MESSAGE ID '00' TYPE 'A' NUMBER '208' WITH UZENET.
  ENDIF.