1616package org .openworkflow .sdk .impl .scripts ;
1717
1818import java .io .IOException ;
19- import java .io .UncheckedIOException ;
19+ import java .io .OutputStream ;
20+ import java .lang .ProcessBuilder .Redirect ;
21+ import java .nio .charset .StandardCharsets ;
2022import java .util .Map ;
2123import org .openworkflow .sdk .api .types .RunTaskConfiguration ;
2224import org .openworkflow .sdk .api .types .RunTaskConfiguration .ProcessReturnType ;
23- import org .openworkflow .sdk .impl .WorkflowDefinition ;
25+ import org .openworkflow .sdk .impl .TaskContext ;
26+ import org .openworkflow .sdk .impl .WorkflowApplication ;
27+ import org .openworkflow .sdk .impl .WorkflowContext ;
28+ import org .openworkflow .sdk .impl .WorkflowError ;
29+ import org .openworkflow .sdk .impl .WorkflowException ;
2430import org .openworkflow .sdk .impl .WorkflowModel ;
2531import org .openworkflow .sdk .impl .WorkflowModelFactory ;
32+ import org .openworkflow .sdk .impl .WorkflowUtils ;
2633import org .openworkflow .sdk .impl .executors .ProcessResult ;
2734import org .slf4j .Logger ;
2835import org .slf4j .LoggerFactory ;
@@ -40,10 +47,14 @@ public static WorkflowModel modelFromOutput(
4047 if (code != 0 ) {
4148 logger .warn ("Process call failed with code {}" , code );
4249 if (logger .isDebugEnabled ()) {
43- logger .debug (streamSuppliers .errorStream ().get ());
50+ String str = streamSuppliers .errorStream ().get ();
51+ if (WorkflowUtils .isValid (str )) {
52+ logger .debug (str );
53+ }
4454 }
4555 }
4656 return switch (returnType ) {
57+ case null -> model ;
4758 case ALL ->
4859 modelFactory .fromAny (
4960 new ProcessResult (
@@ -55,38 +66,81 @@ public static WorkflowModel modelFromOutput(
5566 };
5667 }
5768
58- public static Process uncheckedStart (ProcessBuilder builder ) {
59- try {
60- return builder .start ();
61- } catch (IOException e ) {
62- throw new UncheckedIOException (e );
63- }
64- }
65-
6669 public static void addEnviromment (ProcessBuilder builder , Map <String , Object > env ) {
6770 for (Map .Entry <String , Object > entry : env .entrySet ()) {
6871 builder .environment ().put (entry .getKey (), (String ) entry .getValue ());
6972 }
7073 }
7174
75+ private static boolean keepOut (ProcessReturnType type , boolean dumpOutput ) {
76+ return type != null
77+ && (dumpOutput || type == ProcessReturnType .ALL || type == ProcessReturnType .STDOUT );
78+ }
79+
80+ private static boolean keepError (ProcessReturnType type ) {
81+ return type != null
82+ && (logger .isDebugEnabled ()
83+ || type == ProcessReturnType .ALL
84+ || type == ProcessReturnType .STDERR );
85+ }
86+
7287 public static WorkflowModel buildResultFromProcess (
73- WorkflowDefinition definition , Process process , ProcessReturnType type , WorkflowModel model ) {
74- try {
75- int code = process .waitFor ();
76- StreamSuppliers suppliers = StreamSuppliers .from (process );
77- if (definition
78- .application ()
79- .configManager ()
80- .config ("org.openworkflow.sdk.impl.scripts.dumpOutput" , Boolean .class )
81- .orElse (true )) {
82- System .out .println (suppliers .outputStream ().get ());
88+ ProcessBuilder builder ,
89+ ProcessReturnType type ,
90+ String stdin ,
91+ WorkflowContext workflowContext ,
92+ TaskContext taskContext ,
93+ WorkflowModel model ) {
94+ WorkflowApplication app = workflowContext .definition ().application ();
95+ boolean dumpOutput = app .configManager ().scriptOutput2SystemOutput ();
96+ boolean keepOut = keepOut (type , dumpOutput );
97+ boolean keepError = keepError (type );
98+ if (!keepOut ) {
99+ builder .redirectOutput (Redirect .DISCARD );
100+ }
101+ if (!keepError ) {
102+ builder .redirectError (Redirect .DISCARD );
103+ }
104+ Process process = uncheckedStart (builder , taskContext );
105+ try (StreamSuppliers suppliers =
106+ StreamSuppliers .from (process , app .executorService (), keepOut , keepError )) {
107+ addStdin (process , stdin , taskContext );
108+ // do not wait if await was false (type is null)
109+ int code = type == null ? 0 : process .waitFor ();
110+ if (dumpOutput ) {
111+ String str = suppliers .outputStream ().get ();
112+ if (WorkflowUtils .isValid (str )) {
113+ System .out .println (str );
114+ }
83115 }
84- return modelFromOutput (code , suppliers , type , definition . application () .modelFactory (), model );
116+ return modelFromOutput (code , suppliers , type , app .modelFactory (), model );
85117 } catch (InterruptedException e ) {
86118 Thread .currentThread ().interrupt ();
87119 return model ;
88120 }
89121 }
90122
123+ private static Process uncheckedStart (ProcessBuilder builder , TaskContext taskContext ) {
124+ try {
125+ return builder .start ();
126+ } catch (IOException e ) {
127+ throw new WorkflowException (WorkflowError .runtime (taskContext , e ).build ());
128+ }
129+ }
130+
131+ private static void addStdin (Process process , String stdin , TaskContext taskContext ) {
132+ if (stdin != null ) {
133+ try (OutputStream out = process .getOutputStream ()) {
134+ out .write (stdin .getBytes (StandardCharsets .UTF_8 ));
135+ } catch (IOException e ) {
136+ if (process .toHandle ().isAlive ()) {
137+ logger .warn (
138+ "Unexpected IO exception while writing to process input while the process is still alive, does the process close the standard input before ending?" ,
139+ e );
140+ }
141+ }
142+ }
143+ }
144+
91145 private ScriptUtils () {}
92146}
0 commit comments