Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mv_getjobstate.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Extract the status from a running SAS Viya job
4 @details Extracts the status from a running job and appends it to an output
5 dataset with the following structure:
6
7 | uri | state | timestamp |
8 |---------------------------------------------------------------|---------|--------------------|
9 | /jobExecution/jobs/5cebd840-2063-42c1-be0c-421ec3e1c175/state | running | 15JAN2021:12:35:08 |
10
11 To query the running job, you need the URI. Sample code for achieving this
12 is provided below.
13
14 ## Example
15
16 First, compile the macros:
17
18 filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
19 %inc mc;
20
21 Next, create a long running job (in this case, a web service):
22
23 filename ft15f001 temp;
24 parmcards4;
25 data ;
26 rand=ranuni(0)*1000;
27 do x=1 to rand;
28 y=rand*4;
29 output;
30 end;
31 run;
32 data _null_;
33 call sleep(5,1);
34 run;
35 ;;;;
36 %mv_createwebservice(path=/Public/temp,name=demo)
37
38 Execute it, grab the uri, and finally, check the job status:
39
40 %mv_jobexecute(path=/Public/temp
41 ,name=demo
42 ,outds=work.info
43 )
44
45 data _null_;
46 set work.info;
47 if method='GET' and rel='state';
48 call symputx('uri',uri);
49 run;
50
51 %mv_getjobstate(uri=&uri,outds=results)
52
53 You can run this macro as part of a loop to await the final 'completed'
54 status.
55 The full list of status values is:
56
57 @li idle
58 @li pending
59 @li running
60 @li canceled
61 @li completed
62 @li failed
63
64 If you have one or more jobs that you'd like to wait for completion you can
65 also use the [mv_jobwaitfor](/mv__jobwaitfor_8sas.html) macro.
66
67 @param [in] access_token_var= (ACCESS_TOKEN)
68 The global macro variable to contain the access token
69 @param [in] grant_type= valid values:
70 @li password
71 @li authorization_code
72 @li detect - will check if access_token exists, if not will use sas_services
73 if a SASStudioV session else authorization_code.
74 @li sas_services - will use oauth_bearer=sas_services.
75 @param [in] uri= The uri of the running job for which to fetch the status,
76 in the format `/jobExecution/jobs/$UUID/state` (unquoted).
77 @param [out] outds= The output dataset in which to APPEND the status. Three
78 fields are appended: `CHECK_TM`, `URI` and `STATE`. If the dataset does not
79 exist, it is created.
80
81
82 @version VIYA V.03.04
83 @author Allan Bowe, source: https://github.com/sasjs/core
84
85 <h4> SAS Macros </h4>
86 @li mp_abort.sas
87 @li mf_getplatform.sas
88 @li mf_getuniquefileref.sas
89
90 @sasjslint {"maxLineLength": 102}
91**/
92
93%macro mv_getjobstate(uri=0,outds=work.mv_getjobstate
94 ,contextName=SAS Job Execution compute context
95 ,access_token_var=ACCESS_TOKEN
96 ,grant_type=sas_services
97 );
98%local oauth_bearer;
99%if &grant_type=detect %then %do;
100 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
101 %else %let grant_type=sas_services;
102%end;
103%if &grant_type=sas_services %then %do;
104 %let oauth_bearer=oauth_bearer=sas_services;
105 %let &access_token_var=;
106%end;
107
108%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
109 and &grant_type ne sas_services
110 )
111 ,mac=&sysmacroname
112 ,msg=%str(Invalid value for grant_type: &grant_type)
113)
114
115/* validation in datastep for better character safety */
116%local errmsg errflg;
117data _null_;
118 uri=symget('uri');
119 if length(uri)<12 then do;
120 call symputx('errflg',1);
121 call symputx('errmsg',"URI is invalid (too short) - '&uri'",'l');
122 end;
123 if scan(uri,-1) ne 'state' or scan(uri,1) ne 'jobExecution' then do;
124
125 call symputx('errflg',1);
126 call symputx('errmsg',
127 "URI should be in format /jobExecution/jobs/$$$$UUID$$$$/state"
128 !!" but is actually like: &uri",'l');
129 end;
130run;
131
132%mp_abort(iftrue=(&errflg=1)
133 ,mac=&sysmacroname
134 ,msg=%str(&errmsg)
135)
136
137options noquotelenmax;
138%local base_uri; /* location of rest apis */
139%let base_uri=%mf_getplatform(VIYARESTAPI);
140
141%local fname0;
142%let fname0=%mf_getuniquefileref();
143
144proc http method='GET' out=&fname0 &oauth_bearer url="&base_uri/&uri";
145 headers "Accept"="text/plain"
146 %if &grant_type=authorization_code %then %do;
147 "Authorization"="Bearer &&&access_token_var"
148 %end; ;
149run;
150%if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
151%do;
152 data _null_;infile &fname0;input;putlog _infile_;run;
153 %mp_abort(mac=&sysmacroname
154 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
155 )
156%end;
157
158data;
159 format uri $128. state $32. timestamp datetime19.;
160 infile &fname0;
161 uri="&uri";
162 timestamp=datetime();
163 input;
164 state=_infile_;
165run;
166
167proc append base=&outds data=&syslast;
168run;
169
170filename &fname0 clear;
171
172%mend mv_getjobstate;