-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-wp-codebox-agent-process-runner.php
More file actions
186 lines (159 loc) · 6.23 KB
/
Copy pathclass-wp-codebox-agent-process-runner.php
File metadata and controls
186 lines (159 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Host-side WP Codebox agent process execution helpers.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
final class WP_Codebox_Agent_Process_Runner {
/** @var array<string, callable> */
private array $callbacks;
/**
* @param array<string, callable> $callbacks Test seams for pure-PHP smoke coverage.
*/
public function __construct( array $callbacks = array() ) {
$this->callbacks = $callbacks;
}
public function shell_available(): bool {
if ( isset( $this->callbacks['shell_available'] ) ) {
return (bool) ( $this->callbacks['shell_available'] )();
}
return function_exists( 'exec' ) && function_exists( 'shell_exec' );
}
/** @param array<string,string> $secret_env Secret env values for the child process. @return array{exit_code:int,output:string,timed_out?:bool,timeout_seconds?:int} */
public function run_command( string $command, array $secret_env = array(), int $timeout_seconds = 0 ): array {
if ( isset( $this->callbacks['command_runner'] ) ) {
return ( $this->callbacks['command_runner'] )( $command, $secret_env, $timeout_seconds );
}
if ( ( ! empty( $secret_env ) || $timeout_seconds > 0 ) && ! function_exists( 'proc_open' ) ) {
return array(
'exit_code' => 1,
'output' => 'WP Codebox inherited secret environment or timeout requires proc_open support.',
);
}
if ( ! empty( $secret_env ) || $timeout_seconds > 0 ) {
$process = $this->open_process( $command, $secret_env, $pipes );
if ( is_resource( $process ) ) {
$output = '';
$error = '';
$started = time();
$timed_out = false;
while ( true ) {
$output .= (string) stream_get_contents( $pipes[1] );
$error .= (string) stream_get_contents( $pipes[2] );
$status = proc_get_status( $process );
if ( ! (bool) ( $status['running'] ?? false ) ) {
break;
}
if ( $timeout_seconds > 0 && time() - $started >= $timeout_seconds ) {
$timed_out = true;
proc_terminate( $process );
break;
}
usleep( 100000 );
}
$output .= (string) stream_get_contents( $pipes[1] );
$error .= (string) stream_get_contents( $pipes[2] );
fclose( $pipes[1] );
fclose( $pipes[2] );
$exit_code = proc_close( $process );
if ( $timed_out ) {
return array(
'exit_code' => 124,
'output' => trim( (string) $output . "\n" . (string) $error . "\nWP Codebox task timed out after {$timeout_seconds} seconds." ),
'timed_out' => true,
'timeout_seconds' => $timeout_seconds,
);
}
return array(
'exit_code' => $exit_code,
'output' => trim( (string) $output . "\n" . (string) $error ),
);
}
}
$output = array();
$exit = 0;
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec -- Required host-side WP Codebox execution primitive.
exec( $command . ' 2>&1', $output, $exit );
return array(
'exit_code' => $exit,
'output' => implode( "\n", $output ),
);
}
/** @param array<string,mixed> $item Prepared worker item. @return array<string,mixed>|WP_Error */
public function start_fanout_worker_process( array $item ): array|WP_Error {
if ( ! function_exists( 'proc_open' ) ) {
return new WP_Error( 'wp_codebox_proc_open_unavailable', 'Fanout execution requires proc_open support.', array( 'status' => 500 ) );
}
$prepared = is_array( $item['prepared'] ?? null ) ? $item['prepared'] : array();
$secret_env = is_array( $prepared['process_secret_env'] ?? null ) ? $prepared['process_secret_env'] : array();
$process = $this->open_process( (string) $prepared['command'], $secret_env, $pipes );
if ( ! is_resource( $process ) ) {
return new WP_Error( 'wp_codebox_fanout_worker_start_failed', 'Could not start fanout worker process.', array( 'status' => 500, 'worker_id' => (string) $item['id'] ) );
}
return array_merge(
$item,
array(
'process' => $process,
'pipes' => $pipes,
'started_at' => microtime( true ),
'output' => '',
'error_output' => '',
)
);
}
/** @param array<string,mixed> $worker Active worker. @return array{worker:array<string,mixed>,result:array<string,mixed>|null} */
public function capture_fanout_worker_process_result( array $worker ): array {
$worker['output'] .= (string) stream_get_contents( $worker['pipes'][1] );
$worker['error_output'] .= (string) stream_get_contents( $worker['pipes'][2] );
$status = proc_get_status( $worker['process'] );
$running = (bool) ( $status['running'] ?? false );
$elapsed = microtime( true ) - (float) $worker['started_at'];
$timeout = (int) ( $worker['prepared']['timeout_seconds'] ?? 0 );
if ( $running && $timeout > 0 && $elapsed >= $timeout ) {
proc_terminate( $worker['process'] );
$worker['timed_out'] = true;
$running = false;
}
if ( $running ) {
return array(
'worker' => $worker,
'result' => null,
);
}
$worker['output'] .= (string) stream_get_contents( $worker['pipes'][1] );
$worker['error_output'] .= (string) stream_get_contents( $worker['pipes'][2] );
fclose( $worker['pipes'][1] );
fclose( $worker['pipes'][2] );
$exit_code = proc_close( $worker['process'] );
if ( true === ( $worker['timed_out'] ?? false ) ) {
$exit_code = 124;
}
$result = array(
'exit_code' => $exit_code,
'output' => trim( (string) $worker['output'] . "\n" . (string) $worker['error_output'] ),
);
if ( true === ( $worker['timed_out'] ?? false ) ) {
$result['timed_out'] = true;
$result['timeout_seconds'] = $timeout;
}
return array(
'worker' => $worker,
'result' => $result,
);
}
/** @param array<string,string> $secret_env Secret env values for the child process. */
private function open_process( string $command, array $secret_env, mixed &$pipes ): mixed {
$descriptor_spec = array(
1 => array( 'pipe', 'w' ),
2 => array( 'pipe', 'w' ),
);
$current_env = getenv();
$process = proc_open( $command, $descriptor_spec, $pipes, null, array_merge( is_array( $current_env ) ? $current_env : array(), $_ENV, $secret_env ) );
if ( is_resource( $process ) ) {
stream_set_blocking( $pipes[1], false );
stream_set_blocking( $pipes[2], false );
}
return $process;
}
}