-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.lisp
executable file
·339 lines (301 loc) · 12 KB
/
pipeline.lisp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
(in-package :pipeline)
(defgeneric ensure-stream (stream direction output-if-error)
(:method ((s stream) d o) s)
(:method ((s (eql nil)) d o)
(ecase d
(:input (register-resource
(make-concatenated-stream)))
((:error :output) (register-resource
(make-broadcast-stream)))))
(:method ((s (eql t)) direction o)
(ecase direction
(:input *standard-input*)
(:output *standard-output*)
(:error *error-output*)))
(:method ((s (eql :output)) (d (eql :error)) output)
output))
(defvar *handler*)
(defgeneric exit-handler (handler))
(defgeneric handler-register-resource (handler resource))
(defun register-resource (resource)
(handler-register-resource *handler* resource))
(defmacro with-resource-handler ((&optional handler) &body body)
(unless handler
(setf handler '(make-instance 'simple-handler-mixin)))
(alexandria:once-only (handler)
`(let ((*handler* ,handler))
(unwind-protect (progn ,@body)
(exit-handler ,handler)))))
(defgeneric cleanup-resource (handler resource))
(defclass simple-handler-mixin ()
((resources :initform nil :accessor .resources)))
(defmethod handler-register-resource ((h simple-handler-mixin) resource)
(prog1 resource
(pushnew resource (.resources h))))
(defmethod exit-handler ((handler simple-handler-mixin))
(with-simple-restart (ignore "Ignore all remaining resources")
(loop
while (.resources handler)
for resource = (pop (.resources handler))
do (with-simple-restart (accept "Treat current resource as cleaned up")
(cleanup-resource handler resource)))))
(defclass pipeline ()
((is :initarg :input :accessor .input)
(os :initarg :output :accessor .output)
(es :initarg :error :accessor .error)
(mapping :initarg :mapping :accessor .mapping)
(channel :initarg :channel :accessor channel-of :initform nil)
(filters :initarg :filters :reader .filters)))
(defun fresh-string-stream ()
(let ((stream (make-string-output-stream)))
(prog1 stream
(handler-bind ((error (lambda (c)
(declare (ignore c))
(close stream))))
(register-resource `(:stream ,stream))))))
(defun %pipe-arg-input (input)
(etypecase input
((eql nil) (make-concatenated-stream))
((eql t) *standard-input*)
(stream input)))
(defun %pipe-arg-output (output)
(etypecase output
((eql :string) (fresh-string-stream))
((eql nil) (register-resource
(make-broadcast-stream)))
((eql t) *standard-output*)
(stream output)))
(defun %pipe-arg-error (error-stream output-stream)
(etypecase error-stream
((eql :string) (fresh-string-stream))
((eql :output) output-stream)
((eql nil) (register-resource
(make-broadcast-stream)))
((eql t) *error-output*)
(stream error-stream)))
(defun make-pipeline (input output error redirect mapping filters)
(make-instance 'pipeline
:input input
:output output
:mapping mapping
:error error
:channel redirect
:filters (coerce filters 'simple-vector)))
(defclass active-pipeline (simple-handler-mixin pipeline)
((pipeline :reader .pipeline :initarg :pipeline)
(open :initform nil :accessor .open)
(foldenv :initform nil :accessor foldenv)
(named-results :initform (make-hash-table :test #'equalp)
:accessor named-results)
(results :initform nil :accessor results)
(id :initform (gensym (string '#:PIPELINE)) :reader pipeline-id)))
(defmethod print-object ((p active-pipeline) stream)
(print-unreadable-object (p stream :type t :identity t)
(format stream
"~A (~:[closed~;open~]) ~d result~:p"
(pipeline-id p)
(.open p)
(length (results p)))))
(defgeneric open-pipeline (pipeline)
(:method-combination progn :most-specific-last)
(:method progn (pipeline) pipeline)
(:method :around ((pipeline active-pipeline))
(prog2 (assert (not (.open pipeline)))
(call-next-method)
(setf (.open pipeline) t))))
(defgeneric close-pipeline (pipeline)
(:method-combination progn :most-specific-first)
(:method progn (pipeline) t)
(:method :around ((pipeline active-pipeline))
(prog2 (assert (.open pipeline))
(call-next-method)
(setf (.open pipeline) nil)
(setf (channel-of pipeline) nil))))
(defun get-output-string (pipeline)
(get-output-stream-string (.output pipeline)))
(defun %execute-pipeline (pipeline)
(check-type pipeline pipeline)
(let ((active (make-instance 'active-pipeline
:channel (channel-of pipeline)
:mapping (.mapping pipeline)
:pipeline pipeline
:input (.input pipeline)
:output (.output pipeline)
:error (.error pipeline)
:filters (.filters pipeline))))
(prog1 active
(with-resource-handler (active)
(open-pipeline active)
(close-pipeline active)))))
(defmethod open-pipeline progn ((pipeline active-pipeline)
&aux (length (length (.filters pipeline))))
(make-channel-collector pipeline)
(setf (.input pipeline)
(%pipe-arg-input (.input pipeline)))
(setf (.output pipeline)
(%pipe-arg-output (.output pipeline)))
(setf (.error pipeline)
(%pipe-arg-error (.error pipeline)
(.output pipeline)))
(case length
(0)
(1 (spawn (aref (.filters pipeline) 0)
:pipeline pipeline
:input (.input pipeline)
:output (.output pipeline)
:error (register-resource
(make-broadcast-stream
(.error pipeline)))
:last t
:first t
:wait t))
(t
(let* ((size (1- length))
(pipes (make-pipes size)))
(declare (type simple-vector pipes))
(map () #'register-resource pipes)
(loop
:for filter :across (.filters pipeline)
:for p-in = (.input pipeline)
:then (pipe-in (svref pipes index))
:for index :upfrom 0
:for first = t :then nil
:for last = (= index size)
:for wait = last
:for p-out = (if last
(.output pipeline)
(pipe-out (svref pipes index)))
;; may block until done
:for spawned = (spawn filter
:pipeline pipeline
:input p-in
:output p-out
:error (register-resource
(make-broadcast-stream
(.error pipeline)))
:last last
:first first
:wait wait)
:do (register-resource spawned))))))
(defmethod cleanup-resource (_ (p pipeline.pipes:pipe))
(pipeline.pipes:clean-pipe p))
(defmethod cleanup-resource (_ (s stream))
(handler-case (close s :abort t)
(error (e) (warn "error while closing stream ~s: ~a" s e))))
(defgeneric cleanup-resource-tag (pipeline tag value))
(defmethod cleanup-resource (pipeline (tagged cons))
(destructuring-bind (tag value) tagged
(cleanup-resource-tag pipeline tag value)))
(defmethod cleanup-resource-tag (_p (_t (eql :thread)) v)
(sb-thread:join-thread v :default nil))
(defmethod cleanup-resource-tag (_p (_t (eql :process)) v)
(when (sb-ext:process-alive-p v)
(sb-ext:process-wait v))
(sb-ext:process-close v))
(defmethod cleanup-resource-tag (_p (_t (eql :funcall)) v))
(defmethod cleanup-resource-tag (p (_t (eql :stream)) v)
(close v :abort t))
(defmethod cleanup-resource-tag (p (_t (eql :channel-thread)) v)
(destructuring-bind (&key channel thread) v
(trivial-channels:sendmsg channel :done)
(cleanup-resource-tag p :thread thread)))
(defun make-channel-collector (pipeline
&aux
(p pipeline)
(channel (channel-of p)))
(when channel
(flet ((event-loop ()
(loop
for m = (trivial-channels:recvmsg channel)
while m
do (typecase m
((eql :done)
(return))
(pipeline-fold
(setf (foldenv p)
(foldenv:combine
(foldenv p)
(list (.name m) (.result m))
(.mapping p))))
(pipeline-result
(if (.name m)
(push (.result m) (gethash (.name m) (named-results p)))
(push (.result m) (results p))))
(t (push m (results p)))))))
(let ((name (format nil "channel-collector-~a" (pipeline-id pipeline))))
(register-resource
`(:channel-thread (:thread ,(bt:make-thread #'event-loop :name name)
:channel ,channel)))))))
(defmacro with-pipeline ((&key input (output t) (error :output)
(channel t) (trace nil) (env nil)
(mapping nil))
&body filters)
"#<WITH-PIPELINE DOCUMENTATION>
Set CHANNEL to T to capture results reported by SIGNAL-RESULT and return them
eventually as a list.
"
(flet ((common-code (&key (redirect nil rp))
(assert rp () "explicit call only")
`(%execute-pipeline
(make-pipeline ,input
,output
,error
,redirect
,mapping
(list ,@filters))))
(maybe-trace (expr)
(if trace
(let ((traced (if (consp trace)
trace
'(spawn cleanup-resource-tag cleanup-resource
register-resource make-pipe open-pipeline close-pipeline
ensure-stream %pipe-arg-error %pipe-arg-output
%pipe-arg-input))))
`(prog2
(trace ,@traced)
(unwind-protect ,expr
(untrace ,@traced))))
expr)))
(maybe-trace
`(block nil
(with-augmented-environment ,env
,(etypecase channel
(null
(common-code :redirect nil))
((eql t)
(with-gensyms (channel)
`(let ((,channel (trivial-channels:make-channel)))
,(common-code :redirect channel))))
(symbol
(common-code :redirect channel))))))))
;; (trace cleanup-resource register-resource open-pipeline close-pipeline)
(defun redirecting-result-to (channel function)
(lambda ()
(handler-bind ((pipeline-result
(lambda (c)
(trivial-channels:sendmsg channel c)
(invoke-restart 'continue))))
(funcall function))))
(defvar *%channel%*)
(defun all-channel-values (channel &aux (h (make-hash-table)) (anonymous nil))
(flet ((visit (name result)
(if name
(push result (gethash name h))
(push result anonymous))))
(loop
for m = (trivial-channels:getmsg channel)
while m
do (typecase m
(pipeline-result
(visit (.name m) (.result m)))
(t (visit nil m)))
finally
(return
(list* t (nreverse anonymous)
(alexandria:hash-table-plist h))))))
(defun execute (&rest args)
(destructuring-bind (keyword process)
(with-pipeline (:error *error-output*)
(apply #'program args))
(assert (eq :process keyword))
(zerop (sb-ext:process-exit-code process))))