1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright © 2016, 2017 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of 8sync.
6 ;;; 8sync is free software: you can redistribute it and/or modify it
7 ;;; under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation, either version 3 of the
9 ;;; License, or (at your option) any later version.
11 ;;; 8sync is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU Lesser General Public License for more details.
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with 8sync. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (8sync actors)
20 #:use-module (oop goops)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-9 gnu)
23 #:use-module (ice-9 control)
24 #:use-module (ice-9 format)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 pretty-print)
27 #:use-module (8sync agenda)
28 #:use-module (8sync rmeta-slot)
29 #:export (;; utilities... ought to go in their own module
31 big-random-number-string
32 simple-message-id-generator
40 ;;; Commenting out the <address> type for now;
41 ;;; it may be back when we have better serializers
44 address-actor-id address-hive-id
59 ;; There are more methods for the hive, but there's
60 ;; no reason for the outside world to look at them maybe?
62 bootstrap-actor bootstrap-actor*
64 create-actor create-actor*
69 message-to message-action message-from
70 message-id message-body message-in-reply-to
75 <- <-* <-wait <-wait* <-reply <-reply* <-reply-wait <-reply-wait*
77 call-with-message mbody-receive mbody-val
82 serialize-message write-message
83 serialize-message-pretty pprint-message
84 read-message read-message-from-string))
88 (make-parameter (random-state-from-platform)))
90 ;; Same size as a uuid4 I think...
91 (define random-number-size (expt 2 128))
93 (define (big-random-number)
94 (random random-number-size (%random-state)))
96 ;; Would be great to get this base64 encoded instead.
97 (define (big-random-number-string)
98 ;; @@: This is slow. Using format here is wasteful.
99 (format #f "~x" (big-random-number)))
101 ;; @@: This is slow. A mere ~275k / second on my (old) machine.
102 ;; The main cost seems to be in number->string.
103 (define (simple-message-id-generator)
104 ;; Prepending this cookie makes message ids unique per hive
105 (let ((prefix (format #f "~x:" (big-random-number)))
108 (set! counter (1+ counter))
109 (string-append prefix (number->string counter)))))
117 ;; @@: We may want to add a deferred-reply to the below, similar to
118 ;; what we had in XUDD, for actors which do their own response
119 ;; queueing.... ie, that might receive messages but need to shelve
120 ;; them to be acted upon after something else is taken care of.
122 (define-record-type <message>
123 (make-message-intern id to from action
124 body in-reply-to wants-reply
127 (id message-id) ; id of this message
128 (to message-to) ; actor id this is going to
129 (from message-from) ; actor id of sender
130 (action message-action) ; action (a symbol) to be handled
131 (body message-body) ; argument list "body" of message
132 (in-reply-to message-in-reply-to) ; message id this is in reply to, if any
133 (wants-reply message-wants-reply) ; whether caller is waiting for reply
134 (replied message-replied ; was this message replied to?
135 set-message-replied!))
138 (define* (make-message id to from action body
139 #:key in-reply-to wants-reply
141 (make-message-intern id to from action body
142 in-reply-to wants-reply replied))
144 (define (message-auto-reply? message)
145 (eq? (message-action message) '*auto-reply*))
147 (define (message-needs-reply? message)
148 "See if this message needs a reply still"
149 (and (message-wants-reply message)
150 (not (message-replied message))))
153 (define (kwarg-list-to-alist args)
154 (let loop ((remaining args)
157 (((? keyword? key) val rest ...)
159 (cons (cons (keyword->symbol key) val)
162 (_ (throw 'invalid-kwarg-list
163 "Invalid keyword argument list"
167 ;;; See: https://web.archive.org/web/20081223021934/http://mumble.net/~jar/articles/oo-moon-weinreb.html
168 ;;; (also worth seeing: http://mumble.net/~jar/articles/oo.html )
170 ;; This is the internal, generalized message sending method.
171 ;; Users shouldn't use it! Use the <-foo forms instead.
173 ;; @@: Could we get rid of some of the conditional checks through
176 (define-inlinable (send-message send-options from-actor to-id action
177 replying-to-message wants-reply?
179 (if replying-to-message
180 (set-message-replied! replying-to-message #t))
181 (let* ((hive (actor-hive from-actor))
183 (make-message (hive-gen-message-id hive) to-id
184 (actor-id from-actor) action
186 #:wants-reply wants-reply?
188 (if replying-to-message
189 (message-id replying-to-message)
192 (abort-to-prompt (hive-prompt (actor-hive from-actor))
193 from-actor new-message send-options)
194 ;; @@: It might be that eventually we pass in send-options
195 ;; here too. Since <-wait and <-reply-wait are the only ones
196 ;; that use it yet, for now it kind of just makes things
198 (8sync (hive-process-message hive new-message)))))
200 (define (<- to-id action . message-body-args)
201 "Send a message from an actor to another actor"
202 (send-message '() (%current-actor) to-id action
203 #f #f message-body-args))
205 (define (<-* send-options to-id action . message-body-args)
206 "Like <-*, but allows extra parameters via send-options"
207 (define* (really-send #:key (actor (%current-actor))
209 (send-message send-options actor to-id action
210 #f #f message-body-args))
211 (apply really-send send-options))
213 (define (<-wait to-id action . message-body-args)
214 "Send a message from an actor to another, but wait until we get a response"
215 (wait-maybe-handle-errors
216 (send-message '() (%current-actor) to-id action
217 #f #t message-body-args)))
219 (define (<-wait* send-options to-id action . message-body-args)
220 "Like <-wait, but allows extra parameters, for example whether to
222 (define* (really-send #:key (actor (%current-actor))
224 (apply wait-maybe-handle-errors
225 (send-message send-options actor to-id action
226 #f #t message-body-args)
228 (apply really-send send-options))
230 ;; TODO: Intelligently ~propagate(ish) errors on -wait functions.
231 ;; We might have `send-message-wait-brazen' to allow callers to
232 ;; not have an exception thrown and instead just have a message with
233 ;; the appropriate '*error* message returned.
235 (define (<-reply original-message . message-body-args)
237 (when (message-needs-reply? original-message)
238 (send-message '() (%current-actor) (message-from original-message) '*reply*
239 original-message #f message-body-args)))
241 (define (<-reply* send-options original-message . message-body-args)
242 "Like <-reply, but allows extra parameters via send-options"
243 (define* (really-send #:key (actor (%current-actor))
245 (send-message send-options actor
246 (message-from original-message) '*reply*
247 original-message #f message-body-args))
248 (when (message-needs-reply? original-message)
249 (apply really-send send-options)))
251 (define (<-auto-reply actor original-message)
252 "Auto-reply to a message. Internal use only!"
253 (send-message '() actor (message-from original-message) '*auto-reply*
254 original-message #f '()))
256 (define (<-reply-wait original-message . message-body-args)
257 "Reply to a messsage, but wait until we get a response"
258 (if (message-needs-reply? original-message)
259 (wait-maybe-handle-errors
260 (send-message '() (%current-actor)
261 (message-from original-message) '*reply*
262 original-message #t message-body-args))
265 (define (<-reply-wait* send-options original-message
267 "Like <-reply-wait, but allows extra parameters via send-options"
268 (define* (really-send #:key (actor (%current-actor))
270 (apply wait-maybe-handle-errors
271 (send-message send-options actor
272 (message-from original-message) '*reply*
273 original-message #t message-body-args)
275 (when (message-needs-reply? original-message)
276 (apply really-send send-options)))
278 (define* (wait-maybe-handle-errors message
281 "Before returning a message to a waiting caller, see if we need to
282 raise an exception if an error."
283 (define action (message-action message))
284 (cond ((and (eq? action '*error*)
286 (throw 'hive-unresumable-coroutine
287 "Won't resume coroutine; got an *error* as a reply"
293 ;;; Main actor implementation
294 ;;; =========================
296 (define (actor-inheritable-message-handler actor message)
297 (define action (message-action message))
299 (class-rmeta-ref (class-of actor) 'actions action
300 #:equals? eq? #:cache-set! hashq-set!
301 #:cache-ref hashq-ref))
303 (throw 'action-not-found
304 "No appropriate action handler found for actor"
308 (apply method actor message (message-body message)))
310 (define-syntax-rule (build-actions (symbol method) ...)
311 "Construct an alist of (symbol . method), where the method is wrapped
312 with wrap-apply to facilitate live hacking and allow the method definition
313 to come after class definition."
315 (list (cons (quote symbol)
316 (wrap-apply method)) ...)))
318 (define-class <actor> ()
320 (id #:init-keyword #:id
322 ;; The hive we're connected to.
323 ;; We need this to be able to send messages.
324 (hive #:init-keyword #:hive
325 #:accessor actor-hive)
326 ;; How we receive and process new messages
327 (message-handler #:init-value actor-inheritable-message-handler
328 ;; @@: There's no reason not to use #:class instead of
329 ;; #:each-subclass anywhere in this file, except for
330 ;; Guile bug #25211 (#:class is broken in Guile 2.2)
331 #:allocation #:each-subclass
332 #:getter actor-message-handler)
335 ;; - #t as in, send the init message, but don't wait (default)
336 ;; - 'wait, as in wait on the init message
337 ;; - #f as in don't bother to init
338 (should-init #:init-value #t
339 #:allocation #:each-subclass)
341 ;; This is the default, "simple" way to inherit and process messages.
342 (actions #:init-thunk (build-actions
343 ;; Default init method is to do nothing.
345 ;; Default cleanup method is to do nothing.
346 (*cleanup* (const #f)))
347 #:allocation #:each-subclass))
349 ;;; Addresses are vectors where the first part is the actor-id and
350 ;;; the second part is the hive-id. This works well enough... they
351 ;;; look decent being pretty-printed.
353 (define (make-address actor-id hive-id)
354 (vector actor-id hive-id))
356 (define (address-actor-id address)
357 (vector-ref address 0))
359 (define (address-hive-id address)
360 (vector-ref address 1))
362 (define (address->string address)
363 (string-append (address-actor-id address) "@"
364 (address-hive-id address)))
366 (define-method (actor-id-actor (actor <actor>))
367 "Get the actor id component of the actor-id"
368 (address-actor-id (actor-id actor)))
370 (define-method (actor-id-hive (actor <actor>))
371 "Get the hive id component of the actor-id"
372 (address-hive-id (actor-id actor)))
374 (define-method (actor-id-string (actor <actor>))
375 "Render the full actor id as a human-readable string"
376 (address->string (actor-id actor)))
378 (define %current-actor
381 (define (actor-alive? actor)
382 (hive-resolve-local-actor (actor-hive actor) (actor-id actor)))
389 (define-syntax-rule (define-actor class inherits
392 (define-class class inherits
393 (actions #:init-thunk (build-actions action ...)
394 #:allocation #:each-subclass)
400 ;;; Every actor has a hive. The hive is a kind of "meta-actor"
401 ;;; which routes all the rest of the actors in a system.
403 (define-generic hive-handle-failed-forward)
405 (define-class <hive> (<actor>)
406 (actor-registry #:init-thunk make-hash-table
407 #:getter hive-actor-registry)
408 (msg-id-generator #:init-thunk simple-message-id-generator
409 #:getter hive-msg-id-generator)
410 ;; Ambassadors are used (or will be) for inter-hive communication.
411 ;; These are special actors that know how to route messages to other hives.
412 (ambassadors #:init-thunk make-weak-key-hash-table
413 #:getter hive-ambassadors)
414 ;; Waiting coroutines
415 ;; This is a map from cons cell of message-id
416 ;; to a cons cell of (actor-id . coroutine)
417 ;; @@: Should we have a <waiting-coroutine> record type?
418 ;; @@: Should there be any way to clear out "old" coroutines?
419 (waiting-coroutines #:init-thunk make-hash-table
420 #:getter hive-waiting-coroutines)
422 ;; When actors send messages to each other they abort to this prompt
423 ;; to send the message, then carry on their way
424 (prompt #:init-thunk make-prompt-tag
425 #:getter hive-prompt)
426 (actions #:allocation #:each-subclass
429 ;; This is in the case of an ambassador failing to forward a
430 ;; message... it reports it back to the hive
431 (*failed-forward* hive-handle-failed-forward)
432 ;; These are called at start and end of run-hive
433 (*init-all* hive-handle-init-all)
434 (*cleanup-all* hive-handle-cleanup-all))))
436 (define-method (hive-handle-init-all (hive <hive>) message)
437 "Run *init* method on all actors in registry"
438 ;; We have to do this hack and run over the list
439 ;; twice, because hash-for-each would result in an unrewindable
440 ;; continuation, and to avoid the hash-map changing during the
443 (hash-map->list (lambda (actor-id actor) actor-id)
444 (hive-actor-registry hive)))
445 (for-each (lambda (actor-id)
446 (let* ((actor (hash-ref (hive-actor-registry hive)
448 (match (slot-ref actor 'should-init)
451 (<-wait actor-id '*init*))
453 (<- actor-id '*init*)))))
456 (define-method (hive-handle-failed-forward (hive <hive>) message)
457 "Handle an ambassador failing to forward a message"
460 (define-method (hive-handle-cleanup-all (hive <hive>) message)
461 "Send a message to all actors in our registry to clean themselves up."
462 ;; We have to do this hack and run over the list
463 ;; twice, because hash-for-each would result in an unrewindable
464 ;; continuation, and to avoid the hash-map changing during the
467 (hash-map->list (lambda (actor-id actor) actor-id)
468 (hive-actor-registry hive)))
469 (for-each (lambda (actor-id)
470 (<- actor-id '*cleanup*))
473 (define* (make-hive #:key hive-id)
474 (let ((hive (make <hive>
477 (big-random-number-string))))))
478 ;; Set the hive's actor reference to itself
479 (set! (actor-hive hive) hive)
480 ;; Register the actor with itself
481 (hive-register-actor! hive hive)
484 (define-method (hive-id (hive <hive>))
485 (actor-id-hive hive))
487 (define-method (hive-gen-actor-id (hive <hive>) cookie)
488 (make-address (if cookie
489 (string-append cookie ":" (big-random-number-string))
490 (big-random-number-string))
493 (define-method (hive-gen-message-id (hive <hive>))
494 "Generate a message id using HIVE's message id generator"
495 ((hive-msg-id-generator hive)))
497 (define-method (hive-resolve-local-actor (hive <hive>) actor-address)
498 (hash-ref (hive-actor-registry hive) actor-address))
500 (define-method (hive-resolve-ambassador (hive <hive>) ambassador-address)
501 (hash-ref (hive-ambassadors hive) ambassador-address))
503 (define-method (make-forward-request (hive <hive>) (ambassador <actor>) message)
504 (make-message (hive-gen-message-id hive) (actor-id ambassador)
505 ;; If we make the hive not an actor, we could either switch this
506 ;; to #f or to the original actor...?
507 ;; Maybe some more thinking should be done on what should
508 ;; happen in case of failure to forward? Handling ambassador failures
509 ;; seems like the primary motivation for the hive remaining an actor.
512 `((original . ,message))))
514 (define-method (hive-reply-with-error (hive <hive>) original-message
515 error-key error-args)
516 ;; We only supply the error-args if the original sender is on the same hive
517 (define (orig-actor-on-same-hive?)
518 (equal? (hive-id hive)
519 (address-hive-id (message-from original-message))))
520 (set-message-replied! original-message #t)
521 (let* ((new-message-body
522 (if (orig-actor-on-same-hive?)
523 `(#:original-message ,original-message
524 #:error-key ,error-key
525 #:error-args ,error-args)
526 `(#:original-message ,original-message
527 #:error-key ,error-key)))
528 (new-message (make-message (hive-gen-message-id hive)
529 (message-from original-message)
530 (actor-id hive) '*error*
532 #:in-reply-to (message-id original-message))))
533 ;; We only return a thunk, rather than run 8sync here, because if
534 ;; we ran 8sync in the middle of a catch we'd end up with an
535 ;; unresumable continuation.
536 (lambda () (hive-process-message hive new-message))))
538 (define-record-type <waiting-on-reply>
539 (make-waiting-on-reply actor-id kont send-options)
541 (actor-id waiting-on-reply-actor-id)
542 (kont waiting-on-reply-kont)
543 (send-options waiting-on-reply-send-options))
546 (define-method (hive-process-message (hive <hive>) message)
547 "Handle one message, or forward it via an ambassador"
548 (define (maybe-autoreply actor)
549 ;; Possibly autoreply
550 (if (message-needs-reply? message)
551 (<-auto-reply actor message)))
553 (define (resolve-actor-to)
554 "Get the actor the message was aimed at"
555 (let ((actor (hive-resolve-local-actor hive (message-to message))))
557 (throw 'actor-not-found
558 (format #f "Message ~a from ~a directed to nonexistant actor ~a"
560 (address->string (message-from message))
561 (address->string (message-to message)))
565 ;; TODO: I'm pretty sure we're building up another stack of prompts here
566 ;; with the way we're doing this. That's a real problem.
567 (define* (call-catching-coroutine thunk #:optional (catch-errors #t))
568 (define queued-error-handling-thunk #f)
569 (define (call-catching-errors)
570 ;; TODO: maybe parameterize (or attach to hive) and use
571 ;; maybe-catch-all from agenda.scm
572 ;; @@: Why not just use with-throw-handler and let the catch
573 ;; happen at the agenda? That's what we used to do, but
574 ;; it ended up with a SIGABRT. See:
575 ;; http://lists.gnu.org/archive/html/bug-guile/2016-05/msg00003.html
578 ;; In the actor model, we don't totally crash on errors.
580 ;; If an error happens, we raise it
582 (if (message-needs-reply? message)
583 ;; If the message is waiting on a reply, let them know
584 ;; something went wrong.
585 ;; However, we have to do it outside of this catch
586 ;; routine, or we'll end up in an unrewindable continuation
588 (set! queued-error-handling-thunk
589 (hive-reply-with-error hive message key args)))
590 ;; print error message
591 (apply print-error-and-continue key args)))
592 ;; @@: This is a kludge. See above for why.
593 (if queued-error-handling-thunk
594 (8sync (queued-error-handling-thunk))))
595 (call-with-prompt (hive-prompt hive)
599 (lambda (kont actor message send-options)
600 ;; Register the coroutine
601 (hash-set! (hive-waiting-coroutines hive)
603 (make-waiting-on-reply
604 (actor-id actor) kont send-options))
605 ;; Send off the message
606 (8sync (hive-process-message hive message)))))
608 (define (process-local-message)
609 (let ((actor (resolve-actor-to)))
610 (call-catching-coroutine
612 (define message-handler (actor-message-handler actor))
613 ;; @@: Should a more general error handling happen here?
614 (parameterize ((%current-actor actor))
616 (message-handler actor message)))
617 (maybe-autoreply actor)
618 ;; Returning result allows actors to possibly make a run-request
619 ;; at the end of handling a message.
620 ;; ... We do want that, right?
623 (define (resume-waiting-coroutine)
624 (case (message-action message)
625 ;; standard reply / auto-reply
626 ((*reply* *auto-reply* *error*)
627 (call-catching-coroutine
629 (match (hash-remove! (hive-waiting-coroutines hive)
630 (message-in-reply-to message))
632 (if (not (equal? (message-to message)
633 (waiting-on-reply-actor-id waiting)))
634 (throw 'resuming-to-wrong-actor
635 "Attempted to resume a coroutine to the wrong actor!"
636 #:expected-actor-id (message-to message)
637 #:got-actor-id (waiting-on-reply-actor-id waiting)
639 (let* (;; @@: How should we resolve resuming coroutines to actors who are
641 (actor (resolve-actor-to))
642 (kont (waiting-on-reply-kont waiting))
643 (result (kont message)))
644 (maybe-autoreply actor)
646 (#f (throw 'no-waiting-coroutine
647 "message in-reply-to tries to resume nonexistent coroutine"
649 ;; no need to catch errors here, there's already an error handler
651 ;; Unhandled action for a reply!
653 (throw 'hive-unresumable-coroutine
654 "Won't resume coroutine, nonsense action on reply message"
655 #:action (message-action message)
656 #:message message))))
658 (define (process-remote-message)
659 ;; Find the ambassador
660 (let* ((remote-hive-id (hive-id (message-to message)))
661 (ambassador (hive-resolve-ambassador remote-hive-id))
662 (message-handler (actor-message-handler ambassador))
663 (forward-request (make-forward-request hive ambassador message)))
664 (message-handler ambassador forward-request)))
666 (let ((to (message-to message)))
667 ;; This seems to be an easy mistake to make, so check that addressing
670 (throw 'missing-addressee
671 "`to' field is missing on message"
673 (if (hive-actor-local? hive to)
674 (if (message-in-reply-to message)
675 (resume-waiting-coroutine)
676 (process-local-message))
677 (process-remote-message))))
679 (define-method (hive-actor-local? (hive <hive>) address)
680 (equal? (hive-id hive) (address-hive-id address)))
682 (define-method (hive-register-actor! (hive <hive>) (actor <actor>))
683 (hash-set! (hive-actor-registry hive) (actor-id actor) actor))
685 (define-method (%hive-create-actor (hive <hive>) actor-class
686 init-args id-cookie send-init?)
687 "Actual method called by bootstrap-actor / create-actor.
689 Since this is a define-method it can't accept fancy define* arguments,
690 so this gets called from the nicer bootstrap-actor interface. See
691 that method for documentation."
692 (let* ((actor-id (hive-gen-actor-id hive id-cookie))
693 (actor (apply make actor-class
697 (actor-should-init (slot-ref actor 'should-init)))
698 (hive-register-actor! hive actor)
699 ;; Maybe run actor init method
700 (when (and send-init? actor-should-init)
702 (if (eq? actor-should-init 'wait)
704 (send-method actor-id '*init*)))
705 ;; return the actor id
708 (define* (bootstrap-actor hive actor-class #:rest init-args)
709 "Create an actor on HIVE using ACTOR-CLASS passing in INIT-ARGS args"
710 (%hive-create-actor hive actor-class
711 init-args (symbol->string (class-name actor-class))
714 (define* (bootstrap-actor* hive actor-class id-cookie #:rest init-args)
715 "Create an actor, but also allow customizing a 'cookie' added to the id
717 (%hive-create-actor hive actor-class
721 (define (call-with-message message proc)
722 "Applies message body arguments into procedure, with message as first
723 argument. Similar to call-with-values in concept."
724 (apply proc message (message-body message)))
726 ;; (mbody-receive (<- bar baz)
730 ;; Emacs: (put 'mbody-receive 'scheme-indent-function 2)
732 ;; @@: Or receive-msg or receieve-message or??
733 (define-syntax-rule (mbody-receive arglist message body ...)
734 "Call body with arglist (which can accept arguments like lambda*)
735 applied from the message-body of message."
736 (call-with-message message
740 (define (mbody-val message)
741 "Retrieve the first value from the message-body of message.
742 Like single value return from a procedure call. Probably the most
743 common case when waiting on a reply from some action invocation."
744 (call-with-message message
745 (lambda (_ val) val)))
748 ;;; Various API methods for actors to interact with the system
749 ;;; ==========================================================
751 ;; TODO: move send-message and friends here...?
753 (define* (create-actor from-actor actor-class #:rest init-args)
754 "Create an instance of actor-class. Return the new actor's id.
756 This is the method actors should call directly (unless they want
757 to supply an id-cookie, in which case they should use
759 (%hive-create-actor (actor-hive from-actor) actor-class
763 (define* (create-actor* from-actor actor-class id-cookie #:rest init-args)
764 "Create an instance of actor-class. Return the new actor's id.
766 Like create-actor, but permits supplying an id-cookie."
767 (%hive-create-actor (actor-hive from-actor) actor-class
768 init-args id-cookie #t))
771 (define* (self-destruct actor #:key (cleanup #t))
772 "Remove an actor from the hive.
774 Unless #:cleanup is set to #f, this will first have the actor handle
775 its '*cleanup* action handler."
777 (<-wait (actor-id actor) '*cleanup*))
778 (hash-remove! (hive-actor-registry (actor-hive actor))
783 ;;; 8sync bootstrap utilities
784 ;;; =========================
786 (define* (run-hive hive initial-tasks
788 (handle-signals (list SIGINT SIGTERM)))
789 "Start up an agenda and run HIVE in it with INITIAL-TASKS.
792 - #:cleanup: Whether to run *cleanup* on all actors.
793 - #:handle-sigactions: a list of signals to set up interrupt
794 handlers for, so cleanup sill still happen as expected.
795 Defaults to a list of SIGINT and SIGTERM."
799 (define (run-it escape)
800 (define (handle-signal signum)
803 (for-each (lambda (signum)
804 (sigaction signum handle-signal))
806 (let* ((queue (list->q
807 (cons (bootstrap-message hive (actor-id hive) '*init-all*)
809 (agenda (make-agenda #:pre-unwind-handler print-error-and-continue
811 (run-agenda agenda)))
816 (run-hive-cleanup hive)))))
818 (define (run-hive-cleanup hive)
819 (let ((queue (list->q (list (bootstrap-message hive (actor-id hive)
822 (make-agenda #:queue queue))))
824 (define (bootstrap-message hive to-id action . message-body-args)
826 (apply <-* `(#:actor ,hive) to-id action message-body-args)))
830 ;;; Basic readers / writers
831 ;;; =======================
833 (define (serialize-message message)
834 "Serialize a message for read/write"
838 (message-from message)
839 (message-action message)
840 (message-body message)
841 (message-in-reply-to message)
842 (message-wants-reply message)
843 (message-replied message)))
845 (define* (write-message message #:optional (port (current-output-port)))
846 "Write out a message to a port for easy reading later.
848 Note that if a sub-value can't be easily written to something
849 Guile's `read' procedure knows how to read, this doesn't do anything
850 to improve that. You'll need a better serializer for that.."
851 (write (serialize-message message) port))
853 (define (serialize-message-pretty message)
854 "Serialize a message in a way that's easy for humans to read."
856 (id ,(message-id message))
857 (to ,(message-to message))
858 (from ,(message-from message))
859 (action ,(message-action message))
860 (body ,(message-body message))
861 (in-reply-to ,(message-in-reply-to message))
862 (wants-reply ,(message-wants-reply message))
863 (replied ,(message-replied message))))
865 (define (pprint-message message)
866 "Pretty print a message."
867 (pretty-print (serialize-message-pretty message)))
869 (define* (read-message #:optional (port (current-input-port)))
870 "Read a message serialized via serialize-message from PORT"
872 ((id to from action body in-reply-to wants-reply replied)
874 id to from action body
875 in-reply-to wants-reply replied))
877 (throw 'message-read-bad-structure
878 "Could not read message from structure"
881 (define (read-message-from-string message-str)
882 "Read message from MESSAGE-STR"
883 (with-input-from-string message-str
885 (read-message (current-input-port)))))