Search code examples
datastage

DataStage calling user-defined server routine in job batch


Currently, I want to send a mail with log information inside a job batch whenever it got aborted status. My notification activity works fine but I just want to try to send it via job batch code.
I have created a server routine in DataStage which will send mail based on the job name passed through the job batch. Below is my code for the server routine test.

$INCLUDE DSINCLUDE JOBCONTROL.H
MailArg<-1> = "From: sender@gmail.com"
MailArg<-1> = "To: receiver@gmail.com"
MailArg<-1> = "Subject: Test"
MailArg<-1> = "Body: Job " : Input : " aborted."
mail = DSSendMail(MailArg)
ErrorCode = 0      ;* set this to non-zero to stop the stage/job

When I check this server routine with DSRoutineChecker, the email sent successfully. Then I do some searching and people say that I can call my own defined routine with DEFFUN in job batch code.

But it didn't work for me. Below is the code in the job batch.

* Setup job_2, run it, wait for it to finish, and test for success
      hJob1 = DSAttachJob("job_2", DSJ.ERRFATAL)
      If NOT(hJob1) Then
         Call DSLogFatal("Job Attach Failed: job_2", "JobControl")
         Abort
      End
      ErrCode = DSSetDisableProjectHandler(hJob1, @FALSE)
      ErrCode = DSSetDisableJobHandler(hJob1, @FALSE)
      Call DSSetJobQueue(ErrCode, hJob1, "MediumPriorityJobs")
      ErrCode = DSRunJob(hJob1, DSJ.RUNNORMAL)
      ErrCode = DSWaitForJob(hJob1)
      Status = DSGetJobInfo(hJob1, DSJ.JOBSTATUS)
      If Status = DSJS.RUNFAILED Or Status = DSJS.CRASHED Then
         * Fatal Error - No Return
         DEFFUN test("JOB_2") CALLING "DSU.test"
         Call DSLogFatal("Job Failed: job_2", "JobControl")
      End

All lines are auto-generated when added the job job_2 except the DEFFUN line at the end. If I replaced the server routine code with the DEFFUN line and sending mail directly, it'd work too.
But I want to call one line each time per job added if there's a lot of jobs, so I go with the server routine for not having to copy/ paste the MailArg paragraph for each job code.
It seems to me that the calling line is having some trouble. Do you guys have any idea how I can do it?


Solution

  • DEFFUN is a compiler declaration, and should appear before any executable statement. It is simply setting up the calling name and argument list for the actual function. For example

    DEFFUN test(Arg1) Calling "DSU.test"
    

    Somewhere you need to call your "test" function. For example

    ErrCode = test("JOB_2")