diff options
author | Kyle Meyer <kyle@kyleam.com> | 2020-08-27 22:56:05 -0400 |
---|---|---|
committer | Kyle Meyer <kyle@kyleam.com> | 2020-08-27 23:09:39 -0400 |
commit | 5cada85c7571956345f04e326c8af045cdb62131 (patch) | |
tree | a6312dbf2bd11968b873279fd6dc745396207e37 | |
parent | f028f0c1b225f1522924e052e23f88f125a4ce4a (diff) | |
download | piem-5cada85c7571956345f04e326c8af045cdb62131.tar.gz |
Switch downloads to url-retrieve-synchronously
piem-download-and-decompress uses url-retrieve and a callback, setting
url-asynchronous to nil. This approach doesn't seem to be sufficient
because I'm getting intermittent failures in piem-b4--get-am-files
related to unfinished processes. And taking a peek at
url-retrieve-synchronously suggests that indeed a good amount more is
needed to do a synchronous call with url-retrieve.
Switch piem-download-and-decompress over to
url-retrieve-synchronously. I haven't noticed any issues with the
url-retrieve call in piem-inject-thread-into-maildir, but switch that
over too for simplicity and consistency.
Message-Id: <20200828025605.1106-1-kyle@kyleam.com>
-rw-r--r-- | piem-b4.el | 11 | ||||
-rw-r--r-- | piem.el | 75 |
2 files changed, 40 insertions, 46 deletions
@@ -68,12 +68,11 @@ (buffer (condition-case nil (piem-download-and-decompress (concat url mid "/t.mbox.gz")) - (user-error nil)))) - (when (buffer-live-p buffer) - (with-current-buffer buffer - (write-region nil nil mbox-thread)) - (kill-buffer buffer) - (setq local-mbox-p t)))) + (error nil)))) + (with-current-buffer buffer + (write-region nil nil mbox-thread)) + (kill-buffer buffer) + (setq local-mbox-p t))) ;; Move to the coderepo so that we pick up any b4 configuration ;; from there. (apply #'piem-process-call coderepo piem-b4-b4-executable "am" @@ -34,6 +34,7 @@ (require 'url) (defvar url-http-end-of-headers) +(defvar url-http-response-status) ;;;; Options @@ -403,20 +404,19 @@ buffer." (delete-region (point) (point-max)) (goto-char (point-min))) -(defun piem--decompress-callback (status) - (if (plist-get status :error) - (kill-buffer (current-buffer)) - (piem--url-remove-header) - (piem--url-decompress))) - (defun piem-download-and-decompress (url) "Retrieve gzipped content at URL and decompress it. -A buffer with the decompressed content is returned. A live -buffer indicates that the request did not result in an error." +A buffer with the decompressed content is returned." (unless (piem-check-gunzip) (user-error "gunzip executable not found")) - (let ((url-asynchronous nil)) - (url-retrieve url #'piem--decompress-callback))) + (when-let ((buffer (url-retrieve-synchronously url 'silent))) + (with-current-buffer buffer + (if (/= url-http-response-status 200) + (progn (kill-buffer buffer) + (error "Download of %s failed" url)) + (piem--url-remove-header) + (piem--url-decompress)) + buffer))) ;;;; Maildir injection @@ -460,29 +460,6 @@ buffer indicates that the request did not result in an error." (goto-char end))) (cons n-added n-skipped))) -(defun piem--inject-thread-callback (status mid message-only) - (let ((buffer (current-buffer))) - (unwind-protect - (let ((error-status (plist-get status :error))) - (if error-status - (signal (car error-status) (cdr error-status)) - (piem--url-remove-header) - (unless message-only - (piem--url-decompress)) - (pcase-let ((`(,added-count . ,skipped-count) - (piem--write-mbox-to-maildir))) - (message "Added %d message%s%s for %s to %s" - added-count - (if (= added-count 1) "" "s") - (if (> skipped-count 0) - (format " (skipping %d)" skipped-count) - "") - mid - (abbreviate-file-name piem-maildir-directory))) - (run-hook-with-args 'piem-after-mail-injection-functions mid))) - (and (buffer-live-p buffer) - (kill-buffer buffer))))) - ;;;###autoload (defun piem-inject-thread-into-maildir (mid &optional message-only) "Inject thread containing MID into `piem-maildir-directory'. @@ -504,13 +481,31 @@ This function depends on :url being configured for entries in "`piem-maildir-directory' does not look like a Maildir directory")) ((not (or message-only (piem-check-gunzip))) (user-error "gunzip executable not found"))) - (url-retrieve (concat (or (piem-inbox-url) - (user-error - "Could not find inbox URL for current buffer")) - mid - (if message-only "/raw" "/t.mbox.gz")) - #'piem--inject-thread-callback - (list mid message-only))) + (when-let ((url (concat (or (piem-inbox-url) + (user-error + "Could not find inbox URL for current buffer")) + mid + (if message-only "/raw" "/t.mbox.gz"))) + (buffer (url-retrieve-synchronously url 'silent))) + (unwind-protect + (with-current-buffer buffer + (if (/= url-http-response-status 200) + (error "Download of %s failed" url) + (piem--url-remove-header) + (unless message-only + (piem--url-decompress)) + (pcase-let ((`(,added-count . ,skipped-count) + (piem--write-mbox-to-maildir))) + (message "Added %d message%s%s for %s to %s" + added-count + (if (= added-count 1) "" "s") + (if (> skipped-count 0) + (format " (skipping %d)" skipped-count) + "") + mid + (abbreviate-file-name piem-maildir-directory))) + (run-hook-with-args 'piem-after-mail-injection-functions mid))) + (kill-buffer buffer)))) ;;;; Patch handling |