Macros for SAS Application Developers
https://github.com/sasjs/core
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 **/
91 
92 %macro mv_getjobstate(uri=0,outds=work.mv_getjobstate
93  ,contextName=SAS Job Execution compute context
94  ,access_token_var=ACCESS_TOKEN
95  ,grant_type=sas_services
96  );
97 %local oauth_bearer;
98 %if &grant_type=detect %then %do;
99  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
100  %else %let grant_type=sas_services;
101 %end;
102 %if &grant_type=sas_services %then %do;
103  %let oauth_bearer=oauth_bearer=sas_services;
104  %let &access_token_var=;
105 %end;
106 
107 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
108  and &grant_type ne sas_services
109  )
110  ,mac=&sysmacroname
111  ,msg=%str(Invalid value for grant_type: &grant_type)
112 )
113 
114 /* validation in datastep for better character safety */
115 %local errmsg errflg;
116 data _null_;
117  uri=symget('uri');
118  if length(uri)<12 then do;
119  call symputx('errflg',1);
120  call symputx('errmsg',"URI is invalid (too short) - '&uri'",'l');
121  end;
122  if scan(uri,-1) ne 'state' or scan(uri,1) ne 'jobExecution' then do;
123 
124  call symputx('errflg',1);
125  call symputx('errmsg',
126  "URI should be in format /jobExecution/jobs/$$$$UUID$$$$/state"
127  !!" but is actually like: &uri",'l');
128  end;
129 run;
130 
131 %mp_abort(iftrue=(&errflg=1)
132  ,mac=&sysmacroname
133  ,msg=%str(&errmsg)
134 )
135 
136 options noquotelenmax;
137 %local base_uri; /* location of rest apis */
138 %let base_uri=%mf_getplatform(VIYARESTAPI);
139 
140 %local fname0;
141 %let fname0=%mf_getuniquefileref();
142 
143 proc http method='GET' out=&fname0 &oauth_bearer url="&base_uri/&uri";
144  headers "Accept"="text/plain"
145  %if &grant_type=authorization_code %then %do;
146  "Authorization"="Bearer &&&access_token_var"
147  %end; ;
148 run;
149 %if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
150 %do;
151  data _null_;infile &fname0;input;putlog _infile_;run;
152  %mp_abort(mac=&sysmacroname
153  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
154  )
155 %end;
156 
157 data;
158  format uri $128. state $32. timestamp datetime19.;
159  infile &fname0;
160  uri="&uri";
161  timestamp=datetime();
162  input;
163  state=_infile_;
164 run;
165 
166 proc append base=&outds data=&syslast;
167 run;
168 
169 filename &fname0 clear;
170 
171 %mend mv_getjobstate;