From: Christopher Allan Webber Date: Mon, 2 Jan 2017 17:40:42 +0000 (-0600) Subject: actors: Add cleanup facility to run-hive. X-Git-Tag: v0.4.0~37 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=a5c16530d2e253c0d56143c5337740b1f7a403ed;p=8sync.git actors: Add cleanup facility to run-hive. * 8sync/actors.scm (build-actions): Moved location to be before for compiler reasons. (): Add *cleanup* default action to all actors, which does nothing by default. (, hive-handle-cleanup-all): New cleanup method, which gives all actors still in the registry a chance to clean up. (make-hive): Have the hive register itself to itself. (run-hive): Now run in a dynamic-wind, and call run-hive-cleanup when we're done. (run-hive-cleanup): New variable. * tests/test-actors.scm: New test to make sure cleanup is run. --- diff --git a/8sync/actors.scm b/8sync/actors.scm index 333d779..045a43a 100644 --- a/8sync/actors.scm +++ b/8sync/actors.scm @@ -283,6 +283,14 @@ raise an exception if an error." (call/ec find-message-handler)) (apply method actor message (message-body message))) +(define-syntax-rule (build-actions (symbol method) ...) + "Construct an alist of (symbol . method), where the method is wrapped +with wrap-apply to facilitate live hacking and allow the method definition +to come after class definition." + (list + (cons (quote symbol) + (wrap-apply method)) ...)) + (define-class () ;; An address object (id #:init-keyword #:id @@ -300,7 +308,9 @@ raise an exception if an error." #:getter actor-message-handler) ;; This is the default, "simple" way to inherit and process messages. - (actions #:init-value '() + (actions #:init-value (build-actions + ;; Default cleanup method is to do nothing. + (*cleanup* (const #f))) #:allocation #:each-subclass)) ;;; So these are the nicer representations of addresses. @@ -356,14 +366,6 @@ raise an exception if an error." ;;; Actor utilities ;;; =============== -(define-syntax-rule (build-actions (symbol method) ...) - "Construct an alist of (symbol . method), where the method is wrapped -with wrap-apply to facilitate live hacking and allow the method definition -to come after class definition." - (list - (cons (quote symbol) - (wrap-apply method)) ...)) - (define-syntax-rule (define-simple-actor class action ...) (define-class class () (actions #:init-value (build-actions action ...) @@ -403,12 +405,25 @@ to come after class definition." (build-actions ;; This is in the case of an ambassador failing to forward a ;; message... it reports it back to the hive - (*failed-forward* hive-handle-failed-forward)))) + (*failed-forward* hive-handle-failed-forward) + (*cleanup-all* hive-handle-cleanup-all)))) (define-method (hive-handle-failed-forward (hive ) message) "Handle an ambassador failing to forward a message" 'TODO) +(define-method (hive-handle-cleanup-all (hive ) message) + "Send a message to all actors in our registry to clean themselves up." + ;; Unfortunately we have to do this hack and run over the list + ;; twice, because hash-for-each would result in an unrewindable + ;; continuation. + (define actor-ids + (hash-map->list (lambda (actor-id actor) actor-id) + (hive-actor-registry hive))) + (for-each (lambda (actor-id) + (<- hive actor-id '*cleanup*)) + actor-ids)) + (define* (make-hive #:key hive-id) (let ((hive (make #:id (make-address @@ -416,6 +431,8 @@ to come after class definition." (big-random-number-string)))))) ;; Set the hive's actor reference to itself (set! (actor-hive hive) hive) + ;; Register the actor with itself + (hive-register-actor! hive hive) hive)) (define-method (hive-id (hive )) @@ -700,10 +717,22 @@ Like create-actor, but permits supplying an id-cookie." (define* (run-hive hive initial-tasks) "Start up an agenda and run HIVE in it with INITIAL-TASKS." - (let* ((queue (list->q initial-tasks)) - (agenda (make-agenda #:pre-unwind-handler print-error-and-continue - #:queue queue))) - (start-agenda agenda))) + (dynamic-wind + (const #f) + (lambda () + (let* ((queue (list->q initial-tasks)) + (agenda (make-agenda #:pre-unwind-handler print-error-and-continue + #:queue queue))) + (start-agenda agenda))) + ;; Run cleanup + (lambda () + (run-hive-cleanup hive)))) + +(define (run-hive-cleanup hive) + (let ((queue (list->q (list (bootstrap-message hive (actor-id hive) + '*cleanup-all*))))) + (start-agenda + (make-agenda #:queue queue)))) (define (bootstrap-message hive to-id action . message-body-args) (wrap diff --git a/tests/test-actors.scm b/tests/test-actors.scm index 68a05bf..0de8961 100644 --- a/tests/test-actors.scm +++ b/tests/test-actors.scm @@ -139,5 +139,18 @@ lazy-rep> I'm not answering that. customer> Whaaaaat? I can't believe I got voice mail!\n" displayed-text)))) +(define-simple-actor + (*cleanup* test-call-cleanup)) + +(define (test-call-cleanup actor message) + (speak "Hey, I'm cleanin' up here!\n")) + +(with-fresh-speaker + (let ((hive (make-hive))) + (hive-create-actor hive ) + (run-hive hive '())) + (test-equal '("Hey, I'm cleanin' up here!\n") + (get-spoken))) + (test-end "test-actors") (test-exit)