I have a list of IP addresses to poll and want to catch the USOCKET:TIMEOUT-ERROR from Drakma when a machine isn't available, log a message, and continue on mapping over the list. Does anyone have an example of how to do this?
Thanks,
Patrick
This is not Drakma specific:
(defun try-request-from-multiple-urls (&rest urls) (loop (with-simple-restart (try-next "Try next URL") (handler-case (let ((url (first urls))) (format t "requesting ~A~%" url) (return (drakma:http-request url))) (usocket:timeout-error () (setf urls (rest urls)) (unless urls (format t "no more urls, returning~%") (return)) (format t "timeout, trying next url~%") (invoke-restart 'try-next))))))
Tune as desired. If you want to have the last timeout error percolate to the caller, you'll want handler-bind instead.
-Hans
On Wed, Jun 12, 2013 at 9:42 PM, Patrick May patrick.may@mac.com wrote:
I have a list of IP addresses to poll and want to catch the
USOCKET:TIMEOUT-ERROR from Drakma when a machine isn't available, log a message, and continue on mapping over the list. Does anyone have an example of how to do this?
Thanks,
Patrick
On Jun 12, 2013, at 3:58 PM, Hans Hübner hans.huebner@gmail.com wrote:
This is not Drakma specific:
(defun try-request-from-multiple-urls (&rest urls) (loop (with-simple-restart (try-next "Try next URL") (handler-case (let ((url (first urls))) (format t "requesting ~A~%" url) (return (drakma:http-request url))) (usocket:timeout-error () (setf urls (rest urls)) (unless urls (format t "no more urls, returning~%") (return)) (format t "timeout, trying next url~%") (invoke-restart 'try-next))))))
Tune as desired. If you want to have the last timeout error percolate to the caller, you'll want handler-bind instead.
Thanks, Hans (and Drew C). I figured out my problem on the train home (debugging by public embarrassment), but your use of with-simple-restart makes the code more understandable so I'll incorporate that.
Sorry for being too quick to post.
Patrick
On Wed, Jun 12, 2013 at 11:23 PM, Patrick May patrick.may@mac.com wrote:
(defun try-request-from-multiple-urls (&rest urls) (loop (with-simple-restart (try-next "Try next URL") (handler-case (let ((url (first urls))) (format t "requesting ~A~%" url) (return (drakma:http-request url))) (usocket:timeout-error () (setf urls (rest urls)) (unless urls (format t "no more urls, returning~%") (return)) (format t "timeout, trying next url~%") (invoke-restart 'try-next))))))
Tune as desired. If you want to have the last timeout error percolate to the caller, you'll want handler-bind instead.
Thanks, Hans (and Drew C). I figured out my problem on the train home (debugging by public embarrassment), but your use of with-simple-restart makes the code more understandable so I'll incorporate that.
Restarts are nice, but usually for communicating with other call sites. Seems a bit overkill here. If I understand your problem, all you need is handler-case really.
(defun poll-urls (urls) (dolist (url urls) (handler-case (drakma:http-request url) (usocket:timeout-error () (format t "got timeout getting ~A~%" url)))))
Cheers,
-- Luís Oliveira http://r42.eu/~luis/
On Jun 12, 2013, at 7:19 PM, Luís Oliveira luismbo@gmail.com wrote:
On Wed, Jun 12, 2013 at 11:23 PM, Patrick May patrick.may@mac.com wrote:
(defun try-request-from-multiple-urls (&rest urls) (loop (with-simple-restart (try-next "Try next URL") (handler-case (let ((url (first urls))) (format t "requesting ~A~%" url) (return (drakma:http-request url))) (usocket:timeout-error () (setf urls (rest urls)) (unless urls (format t "no more urls, returning~%") (return)) (format t "timeout, trying next url~%") (invoke-restart 'try-next))))))
Tune as desired. If you want to have the last timeout error percolate to the caller, you'll want handler-bind instead.
Thanks, Hans (and Drew C). I figured out my problem on the train home (debugging by public embarrassment), but your use of with-simple-restart makes the code more understandable so I'll incorporate that.
Restarts are nice, but usually for communicating with other call sites. Seems a bit overkill here. If I understand your problem, all you need is handler-case really.
(defun poll-urls (urls) (dolist (url urls) (handler-case (drakma:http-request url) (usocket:timeout-error () (format t "got timeout getting ~A~%" url)))))
I ultimately went completely underkill and used ignore-error. I need a shower now.
Thanks,
Patrick