discussion and development of piem
 help / color / mirror / code / Atom feed
* [PATCH] Switch downloads to url-retrieve-synchronously
@ 2020-08-28  2:56 Kyle Meyer
  0 siblings, 0 replies; only message in thread
From: Kyle Meyer @ 2020-08-28  2:56 UTC (permalink / raw)
  To: piem

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.
---
 piem-b4.el | 11 ++++----
 piem.el    | 75 +++++++++++++++++++++++++-----------------------------
 2 files changed, 40 insertions(+), 46 deletions(-)

diff --git a/piem-b4.el b/piem-b4.el
index 39d32be..6de0d2b 100644
--- a/piem-b4.el
+++ b/piem-b4.el
@@ -68,12 +68,11 @@ (defun piem-b4--get-am-files (mid coderepo args)
                  (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"
diff --git a/piem.el b/piem.el
index ef92e73..42646a0 100644
--- a/piem.el
+++ b/piem.el
@@ -34,6 +34,7 @@ (require 'subr-x)
 (require 'url)
 
 (defvar url-http-end-of-headers)
+(defvar url-http-response-status)
 
 \f
 ;;;; Options
@@ -403,20 +404,19 @@ (defun piem--url-decompress ()
   (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)))
 
 \f
 ;;;; Maildir injection
@@ -460,29 +460,6 @@ (defun piem--write-mbox-to-maildir ()
         (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 @@ (defun piem-inject-thread-into-maildir (mid &optional message-only)
      "`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))))
 
 \f
 ;;;; Patch handling

base-commit: f028f0c1b225f1522924e052e23f88f125a4ce4a
-- 
2.28.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-08-28  2:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-08-28  2:56 [PATCH] Switch downloads to url-retrieve-synchronously Kyle Meyer

Code repositories for project(s) associated with this public inbox

	https://git.kyleam.com/piem/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).