Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mv_jobexecute.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Executes a SAS Viya Job
4 @details Triggers a SAS Viya Job, with optional URL parameters, using
5 the JES web app.
6
7 First, compile the macros:
8
9 filename mc url
10 "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
11 %inc mc;
12
13 Then, execute the job!
14
15 %mv_jobexecute(path=/Public/folder
16 ,name=somejob
17 )
18
19 Example with parameters:
20
21 %mv_jobexecute(path=/Public/folder
22 ,name=somejob
23 ,paramstring=%str("macvarname":"macvarvalue","answer":42)
24 )
25
26 @param [in] access_token_var= The global macro variable to contain the access
27 token
28 @param [in] grant_type= valid values:
29 @li password
30 @li authorization_code
31 @li detect - will check if access_token exists, if not will use sas_services
32 if a SASStudioV session else authorization_code. Default option.
33 @li sas_services - will use oauth_bearer=sas_services
34
35 @param [in] path= The SAS Drive path to the job being executed
36 @param [in] name= The name of the job to execute
37 @param [in] paramstring= A JSON fragment with name:value pairs, eg:
38 `"name":"value"` or "name":"value","name2":42`. This will need to be
39 wrapped in `%str()`.
40
41 @param [in] contextName= Context name with which to run the job.
42 Default = `SAS Job Execution compute context`
43 @param [in] mdebug= set to 1 to enable DEBUG messages
44 @param [out] outds= (work.mv_jobexecute) The output dataset containing links
45
46
47 @version VIYA V.03.04
48 @author Allan Bowe, source: https://github.com/sasjs/core
49
50 <h4> SAS Macros </h4>
51 @li mp_abort.sas
52 @li mf_getplatform.sas
53 @li mf_getuniquefileref.sas
54 @li mf_getuniquelibref.sas
55 @li mv_getfoldermembers.sas
56
57**/
58
59%macro mv_jobexecute(path=0
60 ,name=0
61 ,contextName=SAS Job Execution compute context
62 ,access_token_var=ACCESS_TOKEN
63 ,grant_type=sas_services
64 ,paramstring=0
65 ,outds=work.mv_jobexecute
66 ,mdebug=0
67 );
68%local dbg;
69%if &mdebug=1 %then %do;
70 %put &sysmacroname entry vars:;
71 %put _local_;
72%end;
73%else %let dbg=*;
74
75%local oauth_bearer;
76%if &grant_type=detect %then %do;
77 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
78 %else %let grant_type=sas_services;
79%end;
80%if &grant_type=sas_services %then %do;
81 %let oauth_bearer=oauth_bearer=sas_services;
82 %let &access_token_var=;
83%end;
84
85%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
86 and &grant_type ne sas_services
87 )
88 ,mac=&sysmacroname
89 ,msg=%str(Invalid value for grant_type: &grant_type)
90)
91
92%mp_abort(iftrue=("&path"="0")
93 ,mac=&sysmacroname
94 ,msg=%str(Path not provided)
95)
96%mp_abort(iftrue=("&name"="0")
97 ,mac=&sysmacroname
98 ,msg=%str(Job Name not provided)
99)
100
101options noquotelenmax;
102
103%local base_uri; /* location of rest apis */
104%let base_uri=%mf_getplatform(VIYARESTAPI);
105
106data;run;
107%local foldermembers;
108%let foldermembers=&syslast;
109%mv_getfoldermembers(root=&path
110 ,access_token_var=&access_token_var
111 ,grant_type=&grant_type
112 ,outds=&foldermembers
113)
114
115%local joburi;
116%let joburi=0;
117data _null_;
118 length name uri $512;
119 call missing(name,uri);
120 set &foldermembers;
121 if name="&name" and uri=:'/jobDefinitions/definitions'
122 then call symputx('joburi',uri);
123run;
124
125%mp_abort(iftrue=("&joburi"="0")
126 ,mac=&sysmacroname
127 ,msg=%str(Job &path/&name not found)
128)
129
130/* prepare request*/
131%local fname0 fname1;
132%let fname0=%mf_getuniquefileref();
133%let fname1=%mf_getuniquefileref();
134
135data _null_;
136 file &fname0;
137 length joburi contextname $128 paramstring $32765;
138 joburi=quote(trim(symget('joburi')));
139 contextname=quote(trim(symget('contextname')));
140 _program=quote("&path/&name");
141 paramstring=symget('paramstring');
142 put '{"jobDefinitionUri":' joburi ;
143 put ' ,"arguments":{"_contextName":' contextname;
144 put ' ,"_program":' _program;
145 if paramstring ne "0" then do;
146 put ' ,' paramstring;
147 end;
148 put '}}';
149run;
150
151proc http method='POST' in=&fname0 out=&fname1 &oauth_bearer
152 url="&base_uri/jobExecution/jobs";
153 headers "Content-Type"="application/vnd.sas.job.execution.job.request+json"
154 "Accept"="application/vnd.sas.job.execution.job+json"
155 %if &grant_type=authorization_code %then %do;
156 "Authorization"="Bearer &&&access_token_var"
157 %end;
158 ;
159run;
160%if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
161%do;
162 data _null_;infile &fname0;input;putlog _infile_;run;
163 data _null_;infile &fname1;input;putlog _infile_;run;
164 %mp_abort(mac=&sysmacroname
165 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
166 )
167%end;
168
169%local libref;
170%let libref=%mf_getuniquelibref();
171libname &libref JSON fileref=&fname1;
172
173data &outds;
174 set &libref..links;
175 _program="&path/&name";
176run;
177
178%if &mdebug=1 %then %do;
179 %put &sysmacroname exit vars:;
180 %put _local_;
181%end;
182%else %do;
183 /* clear refs */
184 filename &fname0 clear;
185 filename &fname1 clear;
186 libname &libref;
187%end;
188%mend mv_jobexecute;