Calling DSAttachJob/DSDetachJob in a loop exits after first

A forum for discussing DataStage<sup>®</sup> basics. If you're not sure where your question goes, start here.

Moderators: chulett, rschirm, roy

Post Reply
anamika
Participant
Posts: 16
Joined: Sat Feb 27, 2016 9:43 am
Location: Ottawa

Calling DSAttachJob/DSDetachJob in a loop exits after first

Post by anamika »

Hi,
I have a BASIC routine that calls DSAttachJob inside a loop. However, the code exits after the first iteration in the loop.
The loop itself works fine - commenting/removing the call to DSAttachJob shows that the loop is working correctly. As soon as I introduce DSAttachJob inside the loop, the loop terminates after the first iteration.
The overall objective of the script is to read files in a directory one at a time and upload the same.
BTW, I have other methods of doing the above and they work fine - I would like to try and find out as to why the DSAttachJob inside a loop is not working as expected.
Any ideas, suggestions, experiences appreciated.

Code: Select all

$INCLUDE DSINCLUDE JOBCONTROL.H

    Equate RoutineName To 'seqfile_upd'
*    Equate Arg1 To tb_name
    Equate Arg1 To data_dir
    Equate Arg2 To schema_dir

OPENPATH data_dir TO dirlist ELSE print "Error Opening Directory " data_dir
       SELECT dirlist;
       Loop
          While READNEXT fname
          tb_name = field (fname,".",1)
*          PRINT tb_name ;
*          PRINT fname ;

         JobHandle = ''
		 
         JobHandle = DSAttachJob ("jb_data_upload", DSJ.ERRFATAL) ;

         ErrCode = DSSetParam (JobHandle,"base_nm", tb_name)
         ErrCode = DSSetParam (JobHandle,"data_dir", data_dir)
         ErrCode = DSSetParam (JobHandle,"schema_dir", schema_dir)

         ErrCode = DSRunJob(JobHandle, DSJ.RUNNORMAL)
         ErrCode = DSWaitForJob(JobHandle)

         Status = DSGetJobInfo(JobHandle, DSJ.JOBSTATUS)
         JobEndTime = DSRTimestamp()

         If Status = DSJS.RUNFAILED Then
     
            Call DSLogWarn("Log WArn", RoutineName)
         End
         Else
             Print "Job successfully executed"
             Call DSLogInfo("Job successfully executed", RoutineName)
         End
         
         deterr = DSDetachJob (JobHandle)
       Repeat
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Welcome.

So, it just exits the loop as if it was done with no error, or is it failing inside the second iteration of the loop?

It's been way too long since I've had to worry about the gory details but I don't see a need to release and attach to the same job over and over, can you not simply attach once up top and release when you are done?
-craig

"You can never have too many knives" -- Logan Nine Fingers
anamika
Participant
Posts: 16
Joined: Sat Feb 27, 2016 9:43 am
Location: Ottawa

Calling DSAttachJob/DSDetachJob in a loop exits after first

Post by anamika »

Thanks chulett.
Since I set the job parameters based on the values obtained in the loop, I need to use setparam and hence attach. But your suggestion could be the right method - I can give it a try and see what happens.

As well, I discovered the root cause of the loop termination after first DSAttachCall - the READNEXT returns empty and hence the loop terminates, which is logical, but why READNEXT comes up with no value is interesting....

Thanks
anamika
Participant
Posts: 16
Joined: Sat Feb 27, 2016 9:43 am
Location: Ottawa

Re: Calling DSAttachJob/DSDetachJob in a loop exits after fi

Post by anamika »

Ok - I think I recall why I was doing DSAttach/DSDetach in a loop - every time DSRunjob is called, the handle obtained from DSAttach is "unloaded". Therefore, I am forced to re-attach inside the loop - leads me back to my original code and back to square one, I guess.

As per the DS 9.x programmers guide :

"After a call of DSRunJob, the controlled job's handle is unloaded. If you require to run the same job again, you must use DSDetachJob and DSAttachJob to set a new handle. Note that you will also need to use DSWaitForJob, as you cannot attach to a job while it is running."

Did another set of tests and back to where I started. Forced to call Attach/Detach in a loop.

Any ideas, suggestions appreciated.

Thanks
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Did you verify that? I checked what I could find online as I seemed to recall the same thing but couldn't find any such reference. Then it seemed to me if it was 'unloaded', what was the point of the detach? Ah well. I'm sure Ray will be along later to give us the Full Wurlod. :wink:

Still unclear why adding that inside the loop would affect the iteration of the loop... the READNEXT... sorry.
-craig

"You can never have too many knives" -- Logan Nine Fingers
anamika
Participant
Posts: 16
Joined: Sat Feb 27, 2016 9:43 am
Location: Ottawa

Post by anamika »

No worries.
Yes, I did verify that - after the call to DSRunjob, the job handle is unloaded and not available anymore. And hence the need for calling DSAttach before every invocation of DSRunJob.
Some workarounds may be to use a subroutine, function and/or to store the names of the list returned from OPENPATH or find a way out of this "bug"..

Thanks for your help.
Post Reply