Factor out task and handler into their own modules

This commit is contained in:
Ray Miller 2025-06-21 15:55:28 +01:00
parent 49571984c2
commit c69a9e2007
Signed by: ray
GPG key ID: 043F786C4CD681B8
3 changed files with 52 additions and 41 deletions

22
ordo/handler.scm Normal file
View file

@ -0,0 +1,22 @@
(define-module (ordo handler)
#:use-module (srfi srfi-9)
#:use-module (ordo logger)
#:export (make-handler
handler?
handler-name
handler-action
handler
run-handler))
(define-record-type <handler>
(make-handler name action)
handler?
(name handler-name)
(action handler-action))
(define* (handler #:key name action)
(make-handler name action))
(define (run-handler h conn)
(log-msg 'NOTICE "Running handler: " (handler-name h))
((handler-action h) conn))

View file

@ -18,8 +18,10 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(define-module (ordo play)
#:use-module (ordo connection)
#:use-module (ordo context)
#:use-module (ordo handler)
#:use-module (ordo inventory)
#:use-module (ordo logger)
#:use-module (ordo task)
#:use-module (ordo util flatten)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-26)
@ -33,17 +35,7 @@ this program. If not, see <https://www.gnu.org/licenses/>.
play-vars
play-tasks
play-handlers
run-play
task
task?
task-name
task-pre-condition
task-action
run-task
handler
handler?
handler-name
handler-action))
run-play))
(define-record-type <play>
(make-play name host sudo? sudo-user sudo-password vars tasks handlers)
@ -60,36 +52,6 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(define* (play #:key name host (sudo? #f) (sudo-user #f) (sudo-password #f) (vars '()) (tasks '()) (handlers '()))
(make-play name host sudo? sudo-user sudo-password (alist->hash-table vars) tasks handlers))
(define-record-type <task>
(make-task name action pre-condition)
task?
(name task-name)
(pre-condition task-pre-condition)
(action task-action))
(define* (task #:key name action (pre-condition (const #t)))
(make-task name action pre-condition))
(define (run-task t conn)
(if ((task-pre-condition t) conn)
(begin
(log-msg 'NOTICE "Running task " (task-name t))
((task-action t) conn))
(log-msg 'NOTICE "Skipping task " (task-name t) ": pre-condition not met")))
(define-record-type <handler>
(make-handler name action)
handler?
(name handler-name)
(action handler-action))
(define* (handler #:key name action)
(make-handler name action))
(define (run-handler h conn)
(log-msg 'NOTICE "Running handler: " (handler-name h))
((handler-action h) conn))
(define (run-play p)
(log-msg 'NOTICE "Running play: " (play-name p))
(parameterize ((*play-handlers* (map handler-name (play-handlers p)))
@ -102,6 +64,7 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(define (run-host-play p h)
(log-msg 'NOTICE "Running play on host: " (host-name h))
(parameterize ((*host-vars* (host-vars h))
(*play-handlers* (play-handlers p))
(*play-triggers* (make-bitvector (length (play-handlers p)) #f)))
(call-with-connection
(host-connection h)

26
ordo/task.scm Normal file
View file

@ -0,0 +1,26 @@
(define-module (ordo task)
#:use-module (ordo logger)
#:use-module (srfi srfi-9)
#:export (task
task?
task-name
task-pre-condition
task-action
run-task))
(define-record-type <task>
(make-task name action pre-condition)
task?
(name task-name)
(pre-condition task-pre-condition)
(action task-action))
(define* (task #:key name action (pre-condition (const #t)))
(make-task name action pre-condition))
(define (run-task t conn)
(if ((task-pre-condition t) conn)
(begin
(log-msg 'NOTICE "Running task " (task-name t))
((task-action t) conn))
(log-msg 'NOTICE "Skipping task " (task-name t) ": pre-condition not met")))