Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Job control

former_member474499
Participant
0 Likes
2,579

Hello,

I need to run a program via job but I should take into consideration that not more than 2 jobs of the same program are runing. If I have more than 2 jobs, I should  wait till only 1 job is active and then submit the program via job.

Can you please tell me How can I do this control with abap? Is there any MF to do the control of number of jobs by program?

Thank you for your help.

Hello,

I need to run a program via job but I should take into consideration that not more than 2 jobs of the same program are runing. If I have more than 2 jobs, I should  wait till only 1 job is active and then submit the program via job.

Can you please tell me How can I do this control with abap? Is there any MF to do the control of number of jobs by program?

Thank you for your help.

19 REPLIES 19
Read only

ankurgodre
SAP Mentor
SAP Mentor
0 Likes
2,332

Hello Kima,

For controlling the job runs sequence there could be various ways depending on by what means the jobs are being scheduled.

For example, if your company is using Tivoli tool for setting up the job schedule, than you can ask the Tivoli Admin to schedule the two jobs in question dependent on each other based on the finish time of job.

Else, another easy method is to make a single job with different steps in it...

the steps can be difference based on the VARIANT of the program & after completion of certain steps you can design the job run so as to trigger the next steps which run through a different variant after completion of first job (Completion of all steps of first job).

Hope it helps!

BR

Ankur

Read only

former_member474499
Participant
0 Likes
2,332

Hello Ankur,

Thank you a lot for your answer.

But these solutions don't help me to solve the issue. What I need is an ABAP way to manage the number of jobs running.

Actually I am using the FM 'BP_FIND_JOBS_WITH_PROGRAM' to get the number of active jobs running for the same program and after I check if the number is >= to 2 I submit my program and call the FM job_close with the event sap_end_of_job so as to submit my program when the job in the parameter ends. But unfortunately this method doesn't work and I don't want to use loop.

Can you please tell me if there is another solution?

  1. BR.

Kima

Read only

0 Likes
2,332

Hi Kima,

Is the code that you using giving you any error?

Or is it that it simply does not trigger the job?

Can you paste the piece of code where you try to accomplish this functionality s that I can get an better idea on this?

Thanks

Ankur

Read only

0 Likes
2,332

Hello Kima,

You may also want to explore & try the below FMs:

>> GET_JOB_RUNTIME_INFO

>> BP_JOB_SELECT_SM37B

BR

Ankur

Read only

former_member474499
Participant
0 Likes
2,332

Hello Ankur,

This code doesn't return any error but the third job is automatically in status 'Active' instead of waiting that one of the 2 running jobs ends.

Here is the code used :

DATA wt_joblist TYPE TABLE OF tbtcjob.
DATA : w_job TYPE n.

CALL FUNCTION 'BP_FIND_JOBS_WITH_PROGRAM'
   EXPORTING
     abap_program_name             = 'Z_HK_TEST'
*   ABAP_VARIANT_NAME             = ' '
*   EXTERNAL_PROGRAM_NAME         = ' '
*   DIALOG                        = ' '
     status                        = 'R'
   TABLES


     joblist                       = wt_joblist
   EXCEPTIONS
     no_jobs_found                 = 1
     program_specification_missing = 2
     invalid_dialog_type           = 3
     job_find_canceled             = 4
     OTHERS                        = 5.
IF sy-subrc = 0.
   DESCRIBE TABLE wt_joblist LINES w_job.
ENDIF.
IF w_job >= 2.
   "Error Message
ENDIF.

DATA: number           TYPE tbtcjob-jobcount,
    name             TYPE tbtcjob-jobname VALUE 'Z_TEST'.


CALL FUNCTION 'JOB_OPEN'
   EXPORTING
     jobname          = name
   IMPORTING
     jobcount         = number
   EXCEPTIONS
     cant_create_job  = 1
     invalid_job_data = 2
     jobname_missing  = 3
     OTHERS           = 4.
IF sy-subrc = 0.
   SUBMIT z_hk_test VIA JOB name NUMBER number
                     AND RETURN.
*      SAP_END_OF_JOB
ENDIF.
IF w_job >= 2.
   CALL FUNCTION 'JOB_CLOSE'
     EXPORTING
       jobcount             = number
       jobname              = name
       event_id             = 'SAP_END_OF_JOB'
       event_param          = 'Z_TEST'
       strtimmed            = 'X'
     EXCEPTIONS
       cant_start_immediate = 1
       invalid_startdate    = 2
       jobname_missing      = 3
       job_close_failed     = 4
       job_nosteps          = 5
       job_notex            = 6
       lock_failed          = 7
       OTHERS               = 8.

endif.

Thanks a lot for your help

BR

Kima

Read only

0 Likes
2,332

Hello Kima,

In your original post, you've mentioned that there are only two jobs, but in your reply above you mentioned about a third job too.

So please clarify how many jobs will be there with the same program name at a time?

If there are only two jobs, then you can use your IF condition as below:

CLEAR W_JOB.

CALL FUNCTION 'BP_FIND_JOBS_WITH_PROGRAM'

   EXPORTING

     abap_program_name             = 'Z_HK_TEST'

*   ABAP_VARIANT_NAME             = ' '

*   EXTERNAL_PROGRAM_NAME         = ' '

*   DIALOG                        = ' '

     status                        = 'R'

   TABLES

       joblist                       = wt_joblist

   EXCEPTIONS

     no_jobs_found                 = 1

     program_specification_missing = 2

     invalid_dialog_type           = 3

     job_find_canceled             = 4

     OTHERS                        = 5.

IF sy-subrc = 0.

   DESCRIBE TABLE wt_joblist LINES w_job.

ENDIF.

IF w_job = 2.

  

CALL FUNCTION 'JOB_OPEN'

   EXPORTING

     jobname          = name

   IMPORTING

     jobcount         = number

   EXCEPTIONS

     cant_create_job  = 1

     invalid_job_data = 2

     jobname_missing  = 3

     OTHERS           = 4.

IF sy-subrc = 0.

   SUBMIT z_hk_test VIA JOB name NUMBER number

                     AND RETURN.

*      SAP_END_OF_JOB

ENDIF.

ENDIF.

The above changes shall work if there are two jobs with same program name & you want to control run of second job after completion of first.

Hope this helps!

BR

Ankur

Read only

former_member474499
Participant
0 Likes
2,332

Hello Ankur,

Yes I said that we shouldn't have more than 2 jobs of the same program that run in the same time. Our program should verify this before running a new job of the same program. If we have two jobs running we should wait till one of the two ends.

I wish I was clear enough.

Thank you for your help

KR

Kima

Read only

former_member474499
Participant
0 Likes
2,332

Hello,

Any solution to my problem?

BR

Read only

Former Member
0 Likes
2,332

Hi

try something like the following

   REPORT ztest.

DATA : gv_lock_name TYPE syrepid,
       gv_subrc     TYPE sysubrc.


INITIALIZATION.
  CLEAR :gv_subrc,
               gv_lock_name.
  DO 2 TIMES.
    PERFORM lock_run USING sy-index
                   CHANGING gv_subrc.
    IF gv_subrc EQ '0'.
      EXIT.
    ENDIF.
  ENDDO.
  IF gv_subrc NE '0'.
    MESSAGE e398(00) WITH 'NO MORE THAN 2 EXCECUTION IN PARALEL'.
  ENDIF.

START-OF-SELECTION.
 
END-OF-SELECTION.
  PERFORM unlock_run.
*&---------------------------------------------------------------------*
*&      Form  lock_run
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->IV_TIMES   text
*----------------------------------------------------------------------*
FORM lock_run USING iv_times TYPE i
           CHANGING gv_subrc  TYPE sysubrc.
  DATA lv_cnt TYPE numc2.
  lv_cnt = iv_times.
  CONCATENATE 'ZZ_PROGRAM_' lv_cnt INTO gv_lock_name.
  CALL FUNCTION 'ENQUEUE_E_DSVAS_TRDIR'
    EXPORTING
      mode_trdir     = 'X'
      name           = gv_lock_name
      x_name         = ' '
      _scope         = '2'
      _wait          = ' '
      _collect       = ' '
    EXCEPTIONS
      foreign_lock   = 1
      system_failure = 2
      OTHERS         = 3.
  gv_subrc = sy-subrc.
ENDFORM.                    "lock_run

*&---------------------------------------------------------------------*
*&      Form  UNLOCK_REUN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM unlock_run.
  CALL FUNCTION 'DEQUEUE_E_DSVAS_TRDIR'
   EXPORTING
*   MODE_TRDIR       = 'X'
      name             = gv_lock_name
*   X_NAME           = ' '
*   _SCOPE           = '3'
*   _SYNCHRON        = ' '
*   _COLLECT         = ' '
            .

ENDFORM.                    "UNLOCK_RUN

Read only

former_member474499
Participant
0 Likes
2,332

Hello,

Thank you george for your answer.

The code you give works correctly so as to say that the transaction is running twice. But the problem Is that when I want to run the transaction for a third time, I should schedule it till one of the 2 runnig transaction stoppes.

Can you please suggest a solution.

Thank you in advance.

Read only

0 Likes
2,332

Hi kima

So youn can schedule the third transaction with start condition after the first job

Regards

Read only

former_member474499
Participant
0 Likes
2,332

Hello george,

do you mean that I should use the events in the MF job close? Because I tried the event at the end of job, but it doesn't work?

And suppose that while we had two jobs running, we ran the transaction for the third time and It was scheduled after the end of one of the running one, and we ran also a fourth time. At the end of one of the running jobs the third and the fouth jobs will run automatically and we'll have 3 running jobs at the same time. Or we should always have only 2 running jobs.

Do you have an idea of how should the program be so as to do that?

thank you a lot for your help.

BR

Read only

0 Likes
2,332

Hi kima

I mean that you can schedule in SE36 transaction your 4 jobs as following the 2 first a any periodic time you want the 2 last as jobs starting each one after of the 2 previous jorbs if you have 4 different transactions.

Read only

former_member474499
Participant
0 Likes
2,332

Hi george,

I should schedule the transaction via an ABAP¨Program. I don't have 4 different transactions but the same one. this program can be ran by different people (we can run it more than 4 times)

Read only

former_member474499
Participant
0 Likes
2,332

Hi george,

I should schedule the transaction via an ABAP¨Program. I don't have 4 different transactions but the same one. this program can be ran by different people (we can run it more than 4 times)

Read only

0 Likes
2,332

Hi kima

  I dont understand which is your issue excactly. If 2 user run the program everthing will be ok if some others try to run program before finished the first 2 an error will be trigrerd for those even the sessions of the program are online or background. If you want more than 2 session of this program to run in parallel jus change the following

INITIALIZATION.
  CLEAR :gv_subrc,
               gv_lock_name

  DO 2 TIMES. "Change the to 2 to any sessions number you want

Read only

former_member474499
Participant
0 Likes
2,332

Hi george,

I don't want to trigger an error but I wan't to schedule the job till one of the 2 running jobs stoppes. One one is finished, my job should start immediately.

Best regards.

Kima

Read only

0 Likes
2,332

Hi kima

  My opinion is let the SAP to control the system resurces instead of write a program to do that. If you want only 2 active sessions of this program raising an error to users it is enough.

Regards

Read only

former_member474499
Participant
0 Likes
2,332

Ok thank you a lot for your help.