csys: basic dispatching mechanism working

This commit is contained in:
Helmut Merz 2025-06-11 19:33:05 +02:00
parent 0eb98e357e
commit 6cef655707
4 changed files with 21 additions and 6 deletions

View file

@ -10,7 +10,7 @@
(:util :scopes/util)
(:alx :alexandria))
(:export #:action-spec #:define-actions
#:*root* #:make-setup #:actions
#:*root* #:make-setup #:actions #:select
#:find-service #:run-services #:setup-services #:shutdown
#:base-context #:context #:add-action #:config #:mailbox #:name
#:handle-message

View file

@ -10,7 +10,7 @@
(:shape :scopes/shape)
(:util :scopes/util)
(:alx :alexandria))
(:export #:*dispatcher* #:setup #:start
(:export #:*dispatcher* #:setup #:start #:shutdown
#:printer
))
@ -31,9 +31,22 @@
:foreground t))
(defun handle-message (msg)
(print msg))
(let ((hdlrs (core:select msg (core:actions *dispatcher*))))
(if hdlrs
(mapcar #'(lambda (hdlr) (run-action hdlr msg)) hdlrs)
(util:lgw "no action selected" msg)))
nil)
(defun run-action (job msg)
(let ((ch (async:make-ch)))
(async:submit-task ch (lambda () (funcall job msg)))))
(defun shutdown (msg &optional (delay 0.1))
(sleep delay)
(format t "~&*** shutdown ~a~%" msg)
(actor:stop (core:mailbox *dispatcher*)))
;;;; example behaviors / actions
(defun printer (msg)
(print msg))
(format t "~&*** message: ~a~%" msg))

View file

@ -4,7 +4,8 @@
(config:root)
(config:add-action '(test) #'csys:printer)
(config:add-action '(:test) #'csys:printer)
(config:add-action '(:stop) #'csys:shutdown)
(config:add :logger :class 'logging:config
:loglevel (config:from-env :loglevel :info)

View file

@ -33,6 +33,7 @@
(csys:setup)
(!= csys:*dispatcher* nil)
(actor:send (core:mailbox csys:*dispatcher*) (message:create '(:test)))
(actor:stop (core:mailbox csys:*dispatcher*))
(actor:send (core:mailbox csys:*dispatcher*) (message:create '(:stop)))
;(actor:stop (core:mailbox csys:*dispatcher*))
(csys:start)
)