speakd
Check-in [e14ae52c5e]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:As-yet-untested support for event expiry times
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e14ae52c5e3a4c29766014ab9d3b6d5330572ece
User & Date: alaric 2014-07-27 01:45:21
Context
2014-08-10
23:17
Event expiry and retry check-in: 66921e6c83 user: alaric tags: trunk
2014-07-27
01:45
As-yet-untested support for event expiry times check-in: e14ae52c5e user: alaric tags: trunk
2014-04-08
09:54
wiki markup errors check-in: e9708c8686 user: alaric tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to speakd-event.scm.

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
(use sql-de-lite)
(use args)
(use data-structures)
(use ports)

(define (check-schema-is-ok db)
  (when (null? (schema db))
        (exec (sql db "CREATE TABLE queue (id INTEGER PRIMARY KEY AUTOINCREMENT, ts INTEGER, priority INTEGER, event TEXT, retries INTEGER NOT NULL DEFAULT 0, error TEXT);"))))

(define (queue-event db-path priority event)
  (call-with-database
   db-path
   (lambda (db)
     (check-schema-is-ok db)

     (exec (sql db "INSERT INTO queue (ts, priority, event) VALUES (?,?,?);")
           (current-seconds)

           priority
           (with-output-to-string (lambda () (write event)))))))

(define opts
 (list (args:make-option (p priority) (#:required "priority") "event priority (higher numbers are more urgent)")

       (args:make-option (d db-path) (#:optional "database path") "database path")
       (args:make-option (h help)      #:none     "Display this text"
         (usage))))

(define (usage)
 (with-output-to-port (current-error-port)
   (lambda ()
     (print "Usage: " (car (argv)) " [options...] event")
     (newline)
     (print (args:usage opts))))
 (exit 1))

(receive (options operands)
    (args:parse (command-line-arguments) opts)
    (let ((priority (alist-ref 'priority options))

          (db-path (alist-ref 'db-path options)))
      (for-each (lambda (ev-string)
                  (unless (list? (with-input-from-string ev-string read))
                          (printf "Invalid event: ~S\n" ev-string)
                          (exit 1))) operands)
      (map
       (lambda (ev-string)
         (queue-event
          (if db-path db-path "/tmp/speakd.sqlite")
          (if priority (string->number priority) 0)

          (with-input-from-string ev-string read)))
       operands)))







|

|




>
|
|
>
|
|



>















>










>


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
(use sql-de-lite)
(use args)
(use data-structures)
(use ports)

(define (check-schema-is-ok db)
  (when (null? (schema db))
        (exec (sql db "CREATE TABLE queue (id INTEGER PRIMARY KEY AUTOINCREMENT, ts INTEGER, expiry INTEGER, priority INTEGER, event TEXT, retries INTEGER NOT NULL DEFAULT 0, error TEXT);"))))

(define (queue-event db-path priority ttl event)
  (call-with-database
   db-path
   (lambda (db)
     (check-schema-is-ok db)
     (let ((now (current-seconds)))
       (exec (sql db "INSERT INTO queue (ts, expiry, priority, event) VALUES (?,?,?,?);")
             now
             (if ttl (+ now ttl) #f)
             priority
             (with-output-to-string (lambda () (write event))))))))

(define opts
 (list (args:make-option (p priority) (#:required "priority") "event priority (higher numbers are more urgent)")
       (args:make-option (t ttl) (#:optional "time to live") "event expiry time (seconds)")
       (args:make-option (d db-path) (#:optional "database path") "database path")
       (args:make-option (h help)      #:none     "Display this text"
         (usage))))

(define (usage)
 (with-output-to-port (current-error-port)
   (lambda ()
     (print "Usage: " (car (argv)) " [options...] event")
     (newline)
     (print (args:usage opts))))
 (exit 1))

(receive (options operands)
    (args:parse (command-line-arguments) opts)
    (let ((priority (alist-ref 'priority options))
          (ttl (alist-ref 'ttl options))
          (db-path (alist-ref 'db-path options)))
      (for-each (lambda (ev-string)
                  (unless (list? (with-input-from-string ev-string read))
                          (printf "Invalid event: ~S\n" ev-string)
                          (exit 1))) operands)
      (map
       (lambda (ev-string)
         (queue-event
          (if db-path db-path "/tmp/speakd.sqlite")
          (if priority (string->number priority) 0)
          ttl
          (with-input-from-string ev-string read)))
       operands)))

Changes to speakd.scm.

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        (display text)
        (close-output-port out)
        (close-input-port in)))))

(define (text-jbo . text)
  (let-values (((fd temp-path) (file-mkstemp "/tmp/speakd.XXXXXX")))
    (file-close fd)
    (text-to-wav '("-v" "jbo+f3" "-s" "120") (apply string-append text) temp-path)
    temp-path))

(define (text-en . text)
  (let-values (((fd temp-path) (file-mkstemp "/tmp/speakd.XXXXXX")))
    (file-close fd)
    (text-to-wav '("-v" "en+f3" "-s" "150") (apply string-append text) temp-path)
    temp-path))

(define (jingle-wav name)  (list 'nodelete name))

(define (jingle-mp3 name)
  (let-values (((fd temp-path) (file-mkstemp "/tmp/speakd.XXXXXX")))
    (file-close fd)







|





|







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        (display text)
        (close-output-port out)
        (close-input-port in)))))

(define (text-jbo . text)
  (let-values (((fd temp-path) (file-mkstemp "/tmp/speakd.XXXXXX")))
    (file-close fd)
    (text-to-wav '("-v" "jbo+f1" "-s" "120") (apply string-append text) temp-path)
    temp-path))

(define (text-en . text)
  (let-values (((fd temp-path) (file-mkstemp "/tmp/speakd.XXXXXX")))
    (file-close fd)
    (text-to-wav '("-v" "en+f3" "-s" "100") (apply string-append text) temp-path)
    temp-path))

(define (jingle-wav name)  (list 'nodelete name))

(define (jingle-mp3 name)
  (let-values (((fd temp-path) (file-mkstemp "/tmp/speakd.XXXXXX")))
    (file-close fd)
136
137
138
139
140
141
142
143



144
145
146
147
148
149
150
                             #f)))       ; Unknown action
                     ev-responses)))
          (for-each execute-event-step!
                    ev-response-steps)))))

(define (check-schema-is-ok db)
  (when (null? (schema db))
        (exec (sql db "CREATE TABLE queue (id INTEGER PRIMARY KEY AUTOINCREMENT, ts INTEGER, priority INTEGER, event TEXT, retries INTEGER NOT NULL DEFAULT 0, error TEXT);"))))




(define (run-event! db ev)
  (unless ev
          (thread-sleep! 1))
  (when ev
        (let ((handlers
               (eval (with-input-from-file (handlers-path) read))))







|
>
>
>







136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
                             #f)))       ; Unknown action
                     ev-responses)))
          (for-each execute-event-step!
                    ev-response-steps)))))

(define (check-schema-is-ok db)
  (when (null? (schema db))
        (exec (sql db "CREATE TABLE queue (id INTEGER PRIMARY KEY AUTOINCREMENT, ts INTEGER, expiry INTEGER, priority INTEGER, event TEXT, retries INTEGER NOT NULL DEFAULT 0, error TEXT);"))))

(define (delete-expired-events! db)
  (exec (sql db "DELETE FROM queue WHERE expiry < ?;") (current-seconds)))

(define (run-event! db ev)
  (unless ev
          (thread-sleep! 1))
  (when ev
        (let ((handlers
               (eval (with-input-from-file (handlers-path) read))))
162
163
164
165
166
167
168

169
170
171
172
173
174
175
    (exec s (exn->string exn) (event-id ev))))

(define (handle-one-event!)
  (call-with-database
   (db-path)
   (lambda (db)
     (check-schema-is-ok db)

     (let ((ev (poll-for-event db)))
       (handle-exceptions
        exn (if ((condition-predicate 'user-interrupt) exn)
                (raise exn)
                (log-failure! db ev exn))
        (run-event! db ev))))))








>







165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    (exec s (exn->string exn) (event-id ev))))

(define (handle-one-event!)
  (call-with-database
   (db-path)
   (lambda (db)
     (check-schema-is-ok db)
     (delete-expired-events! db)
     (let ((ev (poll-for-event db)))
       (handle-exceptions
        exn (if ((condition-predicate 'user-interrupt) exn)
                (raise exn)
                (log-failure! db ev exn))
        (run-event! db ev))))))