Browse code
Make more robust
Ed Langley authored on 11/03/2019 00:16:51
Showing 4 changed files
Showing 4 changed files
... | ... |
@@ -7,7 +7,7 @@ |
7 | 7 |
(defclass event-pump () |
8 | 8 |
((%tick-pause :initform 0.01) |
9 | 9 |
(%running :accessor running :initform nil) |
10 |
- (%finish-cb :reader finish-cb :writer fwoar.event-loop:register-finish-cb :initform nil) |
|
10 |
+ (%finish-cb :accessor fwoar.event-loop:finish-cb :initform nil) |
|
11 | 11 |
(%ws-client :accessor ws-client :initarg :ws-client :initform nil) |
12 | 12 |
(%waiting-pings :accessor waiting-pings :initform 0) |
13 | 13 |
(%modules :accessor modules :initform (make-hash-table)) |
... | ... |
@@ -30,11 +30,11 @@ |
30 | 30 |
(setf (waiting-pings event-pump) 0)) |
31 | 31 |
|
32 | 32 |
(defmethod fwoar.event-loop:prepare-loop ((event-pump event-pump)) |
33 |
- (declare (optimize (debug 3))) |
|
34 |
- (let ((client (funcall (client-factory event-pump) event-pump))) |
|
35 |
- #+nil (websocket-driver:start-connection client))) |
|
33 |
+ (funcall (client-factory event-pump) |
|
34 |
+ event-pump)) |
|
36 | 35 |
|
37 | 36 |
(defmethod fwoar.event-loop:cleanup ((event-pump event-pump)) |
37 |
+ (format t "~&cleaning up...~&") |
|
38 | 38 |
(setf (running event-pump) nil) |
39 | 39 |
(do-hash-table (_ v (modules event-pump)) |
40 | 40 |
(declare (ignore _)) |
... | ... |
@@ -78,7 +78,7 @@ |
78 | 78 |
(modules event-pump)))) |
79 | 79 |
|
80 | 80 |
(defun stop-slacker (event-pump) |
81 |
- (funcall (finish-cb event-pump))) |
|
81 |
+ (funcall (fwoar.event-loop:finish-cb event-pump))) |
|
82 | 82 |
|
83 | 83 |
(defun throttle-continue (num) |
84 | 84 |
(let ((continue-count 0) |
... | ... |
@@ -3,7 +3,7 @@ |
3 | 3 |
|
4 | 4 |
(defpackage :quote-server |
5 | 5 |
(:use :cl :ningle :araneus :serapeum :alexandria :fw.lu) |
6 |
- (:export #:start)) |
|
6 |
+ (:export #:start)) |
|
7 | 7 |
|
8 | 8 |
(cl:in-package :hhgbot-2) |
9 | 9 |
|
... | ... |
@@ -108,13 +108,13 @@ |
108 | 108 |
(define-command "latina" (event-pump message channel word &rest a) |
109 | 109 |
(declare (ignore a)) |
110 | 110 |
(let* ((results (words-coprocess::get-word-results word)) |
111 |
- (json-objs (split-sequence #\newline results :remove-empty-subseqs t)) |
|
112 |
- (parsed (mapcar 'yason:parse json-objs))) |
|
111 |
+ (json-objs (split-sequence #\newline results :remove-empty-subseqs t)) |
|
112 |
+ (parsed (mapcar 'yason:parse json-objs))) |
|
113 | 113 |
(queue-message event-pump channel |
114 |
- (format nil "~{> ~a~2%~}" |
|
115 |
- (remove-if-not #'identity |
|
116 |
- (mapcan (op (gethash "meanings" _)) |
|
117 |
- parsed)))))) |
|
114 |
+ (format nil "~{> ~a~2%~}" |
|
115 |
+ (remove-if-not #'identity |
|
116 |
+ (mapcan (op (gethash "meanings" _)) |
|
117 |
+ parsed)))))) |
|
118 | 118 |
|
119 | 119 |
(define-message-command "random-quote" (event-pump message channel &rest args) |
120 | 120 |
args |
... | ... |
@@ -131,26 +131,26 @@ |
131 | 131 |
|
132 | 132 |
(define-message-command "arc" (event-pump message channel &rest args) |
133 | 133 |
(let ((r (with-output-to-string (s) |
134 |
- (multiple-value-bind (results idx) |
|
135 |
- (slacker.montezuma-store:search-index *client* "message" (string-join args " ")) |
|
136 |
- (montezuma:each results |
|
137 |
- (lambda (h) |
|
138 |
- (format s "> ~a: ~a~%" |
|
139 |
- (local-time:format-timestring |
|
140 |
- nil |
|
141 |
- (local-time:unix-to-timestamp |
|
142 |
- (floor |
|
143 |
- (parse-number |
|
144 |
- (montezuma:document-value (montezuma:get-document idx (montezuma:doc h)) |
|
145 |
- "ts"))) |
|
146 |
- ) |
|
147 |
- :format local-time:+rfc3339-format+) |
|
148 |
- |
|
149 |
- (montezuma:document-value (montezuma:get-document idx (montezuma:doc h)) |
|
150 |
- "text")))))))) |
|
134 |
+ (multiple-value-bind (results idx) |
|
135 |
+ (slacker.montezuma-store:search-index *client* "message" (string-join args " ")) |
|
136 |
+ (montezuma:each results |
|
137 |
+ (lambda (h) |
|
138 |
+ (format s "> ~a: ~a~%" |
|
139 |
+ (local-time:format-timestring |
|
140 |
+ nil |
|
141 |
+ (local-time:unix-to-timestamp |
|
142 |
+ (floor |
|
143 |
+ (parse-number |
|
144 |
+ (montezuma:document-value (montezuma:get-document idx (montezuma:doc h)) |
|
145 |
+ "ts"))) |
|
146 |
+ ) |
|
147 |
+ :format local-time:+rfc3339-format+) |
|
148 |
+ |
|
149 |
+ (montezuma:document-value (montezuma:get-document idx (montezuma:doc h)) |
|
150 |
+ "text")))))))) |
|
151 | 151 |
(if (= 0 (length r)) |
152 | 152 |
(format nil "No results found for: `~a`" (string-join args " ")) |
153 |
- r))) |
|
153 |
+ r))) |
|
154 | 154 |
|
155 | 155 |
(defun extract-channel-info (channels) |
156 | 156 |
(funcall (data-lens:pick |
... | ... |
@@ -175,7 +175,7 @@ |
175 | 175 |
(data-lens:juxt (op (gethash "name" _)) |
176 | 176 |
(op (gethash "id" _)))) |
177 | 177 |
(gethash "channels" r)))) |
178 |
-#+nil (fw.su:log-json channels) |
|
178 |
+ #+nil (fw.su:log-json channels) |
|
179 | 179 |
(assoc name channels :test 'equal))) |
180 | 180 |
|
181 | 181 |
(defmacro with-output-to-json-string ((s &rest args &key indent) &body body) |
... | ... |
@@ -193,7 +193,7 @@ Return a string with the generated JSON output." |
193 | 193 |
|
194 | 194 |
(defmethod slacker:handle-message :before (type (event-pump hhgbot-event-pump) ts channel message) |
195 | 195 |
(declare (ignore type ts channel)) |
196 |
-#+nil |
|
196 |
+ #+nil |
|
197 | 197 |
(index-message message) |
198 | 198 |
(values)) |
199 | 199 |
|
... | ... |
@@ -202,22 +202,22 @@ Return a string with the generated JSON output." |
202 | 202 |
&rest args) |
203 | 203 |
(format *xxx* "~&~a: ~{~a~^ ~}~%" target args) |
204 | 204 |
(dbind* (&optional target-name target-id) (find-channel target) |
205 |
- (if target-name |
|
206 |
- (progn |
|
207 |
- (with (message (string-join args #\space)) |
|
208 |
- (when-let* ((start-link (position #\< message)) |
|
209 |
- (stop-link (position #\> message :start start-link)) |
|
210 |
- (_ (> stop-link (+ 4 start-link)))) |
|
211 |
- (setf message (concat (subseq message 0 start-link) |
|
212 |
- (subseq message (1+ start-link) stop-link) |
|
213 |
- (subseq message (1+ stop-link))))) |
|
214 |
- (queue-message event-pump target-id |
|
215 |
- message)) |
|
216 |
- (queue-message event-pump target-id |
|
217 |
- (format nil "Notifying channel ~a" target-name) |
|
218 |
- :thread (ensure-thread message))) |
|
219 |
- (queue-message event-pump target-id (format nil "Can't find channel `~a`" target) |
|
220 |
- :thread (ensure-thread message))))) |
|
205 |
+ (if target-name |
|
206 |
+ (progn |
|
207 |
+ (with (message (string-join args #\space)) |
|
208 |
+ (when-let* ((start-link (position #\< message)) |
|
209 |
+ (stop-link (position #\> message :start start-link)) |
|
210 |
+ (_ (> stop-link (+ 4 start-link)))) |
|
211 |
+ (setf message (concat (subseq message 0 start-link) |
|
212 |
+ (subseq message (1+ start-link) stop-link) |
|
213 |
+ (subseq message (1+ stop-link))))) |
|
214 |
+ (queue-message event-pump target-id |
|
215 |
+ message)) |
|
216 |
+ (queue-message event-pump target-id |
|
217 |
+ (format nil "Notifying channel ~a" target-name) |
|
218 |
+ :thread (ensure-thread message))) |
|
219 |
+ (queue-message event-pump target-id (format nil "Can't find channel `~a`" target) |
|
220 |
+ :thread (ensure-thread message))))) |
|
221 | 221 |
|
222 | 222 |
(defparameter *reaction-store* (make-hash-table :test 'equalp :synchronized t)) |
223 | 223 |
|
... | ... |
@@ -257,7 +257,7 @@ Return a string with the generated JSON output." |
257 | 257 |
(initialize-quotes) |
258 | 258 |
(quote-server:start) |
259 | 259 |
(setf (values *client* slacker::*api-token*) |
260 |
- (start-in-repl))) |
|
260 |
+ (start-in-repl))) |
|
261 | 261 |
|
262 | 262 |
(in-package :quote-server) |
263 | 263 |
|
... | ... |
@@ -15,22 +15,25 @@ |
15 | 15 |
|
16 | 16 |
(defmethod slacker:start-module ((event-pump event-pump) (exe js-executor)) |
17 | 17 |
(declare (ignorable event-pump)) |
18 |
+ |
|
18 | 19 |
(values exe |
19 |
- (bt:make-thread |
|
20 |
- (lambda () |
|
21 |
- (loop |
|
22 |
- (multiple-value-bind (message message-p) (chanl:recv (work-queue exe)) |
|
23 |
- (when message-p |
|
24 |
- (destructuring-bind (promise script) message |
|
25 |
- (handler-case |
|
26 |
- (blackbird-base:finish promise |
|
27 |
- (cl-js:run-js script)) |
|
28 |
- (serious-condition (c) (blackbird:signal-error promise c))))) |
|
29 |
- (sleep 0.4)))) |
|
30 |
- :name "js-executor"))) |
|
20 |
+ (setf (thread exe) |
|
21 |
+ (bt:make-thread |
|
22 |
+ (lambda () |
|
23 |
+ (loop |
|
24 |
+ (multiple-value-bind (message message-p) (chanl:recv (work-queue exe)) |
|
25 |
+ (when message-p |
|
26 |
+ (destructuring-bind (promise script) message |
|
27 |
+ (handler-case |
|
28 |
+ (blackbird-base:finish promise |
|
29 |
+ (cl-js:run-js script)) |
|
30 |
+ (serious-condition (c) (blackbird:signal-error promise c))))) |
|
31 |
+ (sleep 0.4)))) |
|
32 |
+ :name "js-executor")))) |
|
31 | 33 |
|
32 | 34 |
(defmethod slacker:stop-module ((event-pump event-pump) (exe js-executor)) |
33 | 35 |
(declare (ignorable event-pump)) |
34 | 36 |
(with-accessors ((thread thread)) exe |
37 |
+ (format t "~&executor: ~s~&" thread) |
|
35 | 38 |
(when thread |
36 | 39 |
(bt:destroy-thread thread)))) |
... | ... |
@@ -103,7 +103,7 @@ |
103 | 103 |
(apply #'attach-module |
104 | 104 |
event-pump module args))) |
105 | 105 |
|
106 |
- (bt:make-thread (op (fwoar.event-loop:run-loop event-pump)))) |
|
106 |
+ (fwoar.event-loop:run-loop event-pump)) |
|
107 | 107 |
|
108 | 108 |
(defun start-client (implementation &key (queue-pair (make-instance 'queue-pair)) modules) |
109 | 109 |
(let* ((event-pump (make-instance implementation |
... | ... |
@@ -190,8 +190,11 @@ |
190 | 190 |
(let* ((event-pump (start-client implementation |
191 | 191 |
:queue-pair queue-pair |
192 | 192 |
:modules '((hhgbot-augmented-assistant::js-executor))))) |
193 |
- (bt:make-thread (lambda () (loop until (running event-pump) |
|
194 |
- finally (event-loop event-pump))) |
|
193 |
+ (bt:make-thread (lambda () |
|
194 |
+ (loop until (running event-pump) |
|
195 |
+ finally |
|
196 |
+ (unwind-protect (event-loop event-pump) |
|
197 |
+ (stop-slacker event-pump)))) |
|
195 | 198 |
:name "Event Loop") |
196 | 199 |
event-pump)) |
197 | 200 |
|