1
0
Pārlūkot izejas kodu

More timeout fixes

Innokenty Enikev 10 gadi atpakaļ
vecāks
revīzija
da96d00b63
3 mainītis faili ar 25 papildinājumiem un 16 dzēšanām
  1. 10 6
      chatikbot.lisp
  2. 1 1
      eliza.lisp
  3. 14 9
      forecast.lisp

+ 10 - 6
chatikbot.lisp

@@ -159,7 +159,7 @@
 	      (error (e) (log:error e))))))
     (error (e)
       (log:error e)
-      (telegram-send-message chat-id "some shit happened"))))
+      (telegram-send-message chat-id "Ошибочка вышла"))))
 
 
 (defun handle-cmd-rates (chat-id message-id args)
@@ -174,11 +174,15 @@
     (telegram-send-message
      chat-id
      (if location
-         (forecast-format (forecast
-                           (aget "latitude" location)
-                           (aget "longitude" location)
-                           :hourly (find "hourly" args :key #'string-downcase :test #'equal)
-                           :daily (find "daily" args :key #'string-downcase :test #'equal)))
+	 (handler-case
+	     (forecast-format (forecast
+			       (aget "latitude" location)
+			       (aget "longitude" location)
+			       :hourly (find "hourly" args :key #'string-downcase :test #'equal)
+			       :daily (find "daily" args :key #'string-downcase :test #'equal)))
+	   (error (e)
+	     (log:error e)
+	     "Ошибочка вышла"))
          "Так а ты чьих будешь?"))))
 
 

+ 1 - 1
eliza.lisp

@@ -81,7 +81,7 @@
   (let ((r (use-eliza-rules
             (read-from-string-no-punct input))))
     (cond
-      ((null r))
+      ((null r) nil)
       ((and (consp (car r)) (eq 'function (caar r)))
        (apply (cadar r) (cdr r)))
       ((keywordp (car r)) r)

+ 14 - 9
forecast.lisp

@@ -4,15 +4,20 @@
 (defparameter +forecast-api-url+ "https://api.forecast.io/forecast" "forecast.io API endpoint")
 
 (defun forecast (lat lon &key time (currently t) minutely hourly daily alerts)
-  (yason:parse
-   (flexi-streams:octets-to-string
-    (drakma:http-request (format
-                          nil
-                          "~A/~A/~A,~A~@[,~A~]?units=si&exclude=~:[currently,~;~]~:[minutely,~;~]~:[hourly,~;~]~:[daily,~;~]~:[alerts,~;~]flags&lang=ru"
-                          +forecast-api-url+ *forecast-api-key* lat lon time
-                          currently minutely hourly daily alerts))
-    :external-format :utf8)
-   :object-as :alist))
+  (handler-case
+      (bordeaux-threads:with-timeout (5)
+	(yason:parse
+	 (flexi-streams:octets-to-string
+	  (drakma:http-request (format
+				nil
+				"~A/~A/~A,~A~@[,~A~]?units=si&exclude=~:[currently,~;~]~:[minutely,~;~]~:[hourly,~;~]~:[daily,~;~]~:[alerts,~;~]flags&lang=ru"
+				+forecast-api-url+ *forecast-api-key* lat lon time
+				currently minutely hourly daily alerts))
+	  :external-format :utf8)
+	 :object-as :alist))
+    (bordeaux-threads:timeout (e)
+      (declare (ignore e))
+      (error "Timeout"))))
 
 (local-time:reread-timezone-repository :timezone-repository "/usr/share/zoneinfo/")