Loading...
Searching...
No Matches
mpe_execute.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Runs a DC service the way a test needs it run
4 @details Wraps mx_execute so that every call is measurable, using only
5 documented mx_execute outputs:
6
7 @li the child's log is kept when mdebug is on, through outlogds, and printed
8 as CHILD: lines. A normal run asks for no log, so it stays quiet - flip
9 sasjs_mdebug for an investigation run instead of editing tests;
10 @li the child's outcome is written to outstatus (STATE, URI, ERRMSG,
11 PLATFORM). state=canceled means the child aborted, so a test can assert
12 what happened rather than inferring it from an outlib that may still hold
13 the previous call's tables;
14 @li failfast aborts the caller as soon as the child is not completed, so the
15 first failure is the real one and the log names it. Tests that expect an
16 abort pass failfast=0 and check the status row. SAS 9 reports no job state
17 at all (PROC STP), which core records as state=unknown; that is treated as
18 unmeasured rather than failed, and noted in the log.
19
20 Usage:
21
22 %mpe_execute(&appLoc/services/editors/getdata,
23 viyacontext=&defaultcontext,
24 inputfiles=&f1:sascontroltable,
25 outlib=web1
26 )
27 %put reaching here means the child completed;
28 data _null_;
29 set work.dc_childstatus;
30 putlog "child said: " state= errmsg=;
31 run;
32
33 @param [in] program The _PROGRAM endpoint to run
34 @param [in] inputfiles=(0) fileref:filename pairs, as mx_execute
35 @param [in] inputdatasets=(0) datasets to send as CSV, as mx_execute
36 @param [in] inputparams=(0) name/value dataset, as mx_execute
37 @param [out] outlib=(0) Output libref for the response JSON
38 @param [out] outref=(0) Output fileref for the raw response text
39 @param [in] debug= (131) The _debug value sent to the child. Numeric, as the
40 server requires; 131 asks for the log.
41 @param [in] mdebug= (&sasjs_mdebug) 1 keeps the child's log and prints it
42 @param [in] viyacontext= (&defaultcontext) Viya compute context
43 @param [out] outstatus=(work.dc_childstatus) One row per child run
44 @param [in] failfast= (1) Abort the caller when the child did not complete
45
46 <h4> SAS Macros </h4>
47 @li mp_abort.sas
48 @li mx_execute.sas
49
50 @sasjslint {"maxLineLength": 101}
51
52**/
53
54%macro mpe_execute(program
55 ,inputfiles=0
56 ,inputdatasets=0
57 ,inputparams=0
58 ,outlib=0
59 ,outref=0
60 ,debug=
61 ,mdebug=&sasjs_mdebug
62 ,viyacontext=&defaultcontext
63 ,outstatus=work.dc_childstatus
64 ,viyaresult=WEBOUT_JSON
65 ,failfast=1
66 );
67%local logds state msg;
68/* Aliases: passing &program / &inputfiles ... straight into another macro that
69 * declares the same parameter names makes SAS resolve them against the callee
70 * and null them ("recursive reference to the macro variable"). Unique names
71 * on this side keep the values intact. */
72%local a_viyaresult a_pgm a_inds a_indatasets a_inparams a_dbg a_mdbg
73 a_outlib a_outref a_logds a_status a_ctx;
74/* numeric: the server validates _debug as a number, so anything else never asks
75 * for the log and outlogds comes back empty */
76%if %length(&debug)=0 %then %let debug=131;
77%if %length(&mdebug)=0 %then %let mdebug=0;
78%let a_pgm=&program;
79%let a_viyaresult=&viyaresult;
80%let a_inds=&inputfiles;
81%let a_indatasets=&inputdatasets;
82%let a_inparams=&inputparams;
83%let a_dbg=&debug;
84%let a_mdbg=&mdebug;
85%let a_outlib=&outlib;
86%let a_outref=&outref;
87%let a_status=&outstatus;
88%let a_ctx=&viyacontext;
89
90%if &mdebug>0 %then %let logds=work.dc_childlog;
91%else %let logds=_null_;
92%let a_logds=&logds;
93
94/* a status row from an earlier call must not be read as this call's outcome:
95 a child that dies before core writes one would otherwise look completed */
96%if %index(&a_status,.)>0 and %sysfunc(exist(&a_status)) %then %do;
97 proc datasets library=%scan(&a_status,1,.) nolist;
98 delete %scan(&a_status,2,.);
99 quit;
100%end;
101
102%mx_execute(&a_pgm
103 ,inputfiles=&a_inds
104 ,inputdatasets=&a_indatasets
105 ,inputparams=&a_inparams
106 ,debug=&a_dbg
107 ,mdebug=&a_mdbg
108 ,viyacontext=&a_ctx
109 ,outlib=&a_outlib
110 ,outref=&a_outref
111 ,outlogds=&a_logds
112 ,outstatus=&a_status
113 ,viyaresult=&a_viyaresult
114)
115
116/* the child's own log, for an investigation run */
117%if &mdebug>0 %then %do;
118 data _null_;
119 set &a_status;
120 putlog "DC_EXECUTE: " state= errmsg=;
121 run;
122 %if %sysfunc(exist(work.dc_childlog)) %then %do;
123 data _null_;
124 set work.dc_childlog;
125 putlog "CHILD: " line;
126 run;
127 %end;
128%end;
129
130/* fail fast - a child that did not complete must not leave the test running
131 * against whatever the previous call left in outlib. A missing status table
132 * and an empty one are both failures: state would stay blank and the caller
133 * would carry on inferring, which is the thing this macro exists to stop. */
134%global _mpe_abortmsg;
135%let _mpe_abortmsg=;
136%let state=;
137%let msg=;
138%if %sysfunc(exist(&a_status))=1 %then %do;
139 data _null_;
140 length _prog $32767 _state $32767 _prefix $32767 _childmsg $32767
141 _out $32767 _room 8;
142 set &a_status;
143 call symputx('state',state,'l');
144 call symputx('msg',errmsg,'l');
145 /* the text the caller sees is assembled here rather than with %let: the
146 child's message is arbitrary text, and a %, an & or a quote in it would
147 be macro resolved on the way into a symbol. symget/symputx hand it over
148 untouched, and a message already at the 32767 limit is cut to the room
149 left rather than overflowed by the join. */
150 if upcase(state) ne 'COMPLETED' then do;
151 _prog=symget('a_pgm');
152 _state=symget('state');
153 _childmsg=symget('msg');
154 _prefix=cat(trim(_prog),' did not complete: state=',trim(_state));
155 _room=32767-length(trim(_prefix))-2;
156 if _room<1 then _room=0;
157 if length(_childmsg)>_room then _childmsg=substr(_childmsg,1,_room);
158 if missing(_childmsg) then _out=trim(_prefix);
159 else _out=trim(_prefix)||' '||_childmsg;
160 call symputx('_mpe_abortmsg',_out,'g');
161 end;
162 run;
163%end;
164%else %do;
165 %let state=missing(&a_status);
166 %let _mpe_abortmsg=&a_pgm did not complete: state=&state;
167%end;
168
169%if %length(&failfast)=0 %then %let failfast=1;
170%if &failfast=1 %then %do;
171 /* SAS 9 (PROC STP) reports no job status at all: core writes state='unknown'
172 * with that explanation on a call that worked, so it cannot be read as a
173 * failure - only as unmeasured. Everything else that is not completed is a
174 * failure, a blank state included. */
175 %if "%upcase(&state)"="UNKNOWN" %then
176 %put NOTE: &sysmacroname: &program returned no job status (SAS 9) - the outcome was not measured;
177 %else %if "%upcase(&state)" ne "COMPLETED" %then %do;
178 %mp_abort(
179 iftrue=(1=1)
180 ,mac=&sysmacroname
181 ,msg=%superq(_mpe_abortmsg)
182 )
183 %end;
184%end;
185
186%mend mpe_execute;