emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/detached a19d0cd1c1 4/4: Improve handling of remote ses


From: ELPA Syncer
Subject: [elpa] externals/detached a19d0cd1c1 4/4: Improve handling of remote sessions
Date: Thu, 8 Sep 2022 02:57:40 -0400 (EDT)

branch: externals/detached
commit a19d0cd1c1a5f96284c2a30abe7d1832a19d582f
Author: Niklas Eklund <niklas.eklund@posteo.net>
Commit: Niklas Eklund <niklas.eklund@posteo.net>

    Improve handling of remote sessions
    
    This patch updates the initialization of remote sessions. The package
    will now only watch sessions that are local, or currently
    accessible. This should mean that if detached is loaded directly when
    Emacs starts it won't try to access remote sessions. Instead it will
    wait until the user either switch to the remote host, or access the
    session willingly through detached-open-session.
---
 CHANGELOG.org         |  2 ++
 detached.el           | 66 +++++++++++++++++++++++++++++++++++++++++++--------
 test/detached-test.el | 28 ++++++++++++++++++----
 3 files changed, 81 insertions(+), 15 deletions(-)

diff --git a/CHANGELOG.org b/CHANGELOG.org
index 28781ba478..14d477e3ec 100644
--- a/CHANGELOG.org
+++ b/CHANGELOG.org
@@ -4,6 +4,8 @@
 
 * Development
 
+- Improved handling of remote sessions. The package will only try to 
initialize sessions that are accessible when package loads. Other active 
sessions it will wait until a remote connection has been established before 
they are being watched.
+
 * Version 0.8.1 (2022-09-08)
 
 - Adjust default location for sessions, from a directory under 
=temporary-file-directory= to =user-emacs-directory=. This change is for 
security reasons in case the user shares the computer with other users. By 
default sessions will not be removed on reboots, the user can use =M-x 
detached-delete-sessions= to clear old sessions manually.
diff --git a/detached.el b/detached.el
index 05acc44b4f..45d7bbc6b7 100644
--- a/detached.el
+++ b/detached.el
@@ -692,6 +692,7 @@ Optionally SUPPRESS-OUTPUT."
     ;; Update transitioned sessions
     (thread-last (detached--db-get-sessions)
                  (seq-filter #'detached--active-session-p)
+                 (seq-filter #'detached--session-accessible-p)
                  (seq-remove (lambda (it) (when (detached--session-missing-p 
it)
                                        (detached--db-remove-entry it)
                                        t)))
@@ -701,6 +702,7 @@ Optionally SUPPRESS-OUTPUT."
     ;; Watch session directories with active sessions
     (thread-last (detached--db-get-sessions)
                  (seq-filter #'detached--active-session-p)
+                 (seq-filter #'detached--session-accessible-p)
                  (seq-map #'detached--session-directory)
                  (seq-uniq)
                  (seq-do #'detached--watch-session-directory))))
@@ -766,6 +768,7 @@ This function uses the `notifications' library."
 (defun detached-get-sessions ()
   "Return validated sessions."
   (detached--validate-unknown-sessions)
+  (detached--initialize-remote-sessions)
   (detached--db-get-sessions))
 
 (defun detached-shell-command-attach-session (session)
@@ -983,6 +986,17 @@ Optionally CONCAT the command return command into a 
string."
   (eq 'local
       (cdr (detached--session-host session))))
 
+(defun detached--session-accessible-p (session)
+  "Return t if SESSION is accessible."
+  (or (detached--local-session-p session)
+      (file-remote-p (detached--session-directory session) nil t)))
+
+(defun detached--watched-session-directory-p (directory)
+  "Return t if DIRECTORY is being watched."
+  (alist-get directory
+             detached--watched-session-directories
+             nil nil #'string=))
+
 (defun detached--session-missing-p (session)
   "Return t if SESSION is missing."
   (not
@@ -1034,6 +1048,7 @@ Optionally CONCAT the command return command into a 
string."
   "Validate `detached' sessions with state unknown."
   (thread-last (detached--db-get-sessions)
                (seq-filter (lambda (it) (eq 'unknown (detached--session-state 
it))))
+               (seq-filter #'detached--session-accessible-p)
                (seq-do (lambda (it)
                          (if (detached--session-missing-p it)
                              (detached--db-remove-entry it)
@@ -1058,7 +1073,7 @@ Optionally make the path LOCAL to host."
       full-path)))
 
 (defun detached--cleanup-host-sessions (host)
-  "Run cleanuup on HOST sessions."
+  "Run cleanup on HOST sessions."
   (let ((host-name (car host)))
     (thread-last (detached--db-get-sessions)
                  (seq-filter (lambda (it) (string= host-name (car 
(detached--session-host it)))))
@@ -1071,6 +1086,7 @@ Optionally make the path LOCAL to host."
          (detached-message (rx (regexp "\n.detached-exit-code:.*"))))
     (with-temp-buffer
       (insert-file-contents filename)
+      (detached--maybe-watch-session session)
       (goto-char (point-min))
       (let ((beginning (point))
             (end (if (search-forward-regexp detached-message nil t)
@@ -1078,6 +1094,13 @@ Optionally make the path LOCAL to host."
                    (point-max))))
         (buffer-substring beginning end)))))
 
+(defun detached--maybe-watch-session (session)
+  "Maybe watch SESSION."
+  (let ((session-directory (detached--session-directory session)))
+    (and (detached--active-session-p session)
+         (not (detached--watched-session-directory-p session-directory))
+         (detached--watch-session-directory session-directory))))
+
 (defun detached--create-session-directory ()
   "Create session directory if it doesn't exist."
   (let ((directory (detached--get-session-directory)))
@@ -1098,6 +1121,27 @@ Optionally make the path LOCAL to host."
       detached-session-directory
     (concat (file-remote-p default-directory) detached-session-directory)))
 
+(defun detached--initialize-remote-sessions ()
+  "Initialize accessible remote sessions."
+  (let ((remote-sessions
+         (thread-last (detached--db-get-sessions)
+                      (seq-filter #'detached--remote-session-p)
+                      (seq-filter #'detached--session-accessible-p))))
+
+    ;; Update transitioned sessions
+    (thread-last remote-sessions
+                 (seq-remove (lambda (it) (when (detached--session-missing-p 
it)
+                                       (detached--db-remove-entry it)
+                                       t)))
+                 (seq-filter #'detached--state-transition-p)
+                 (seq-do #'detached--session-state-transition-update))
+
+    ;; Watch session directories
+    (thread-last remote-sessions
+                 (seq-map #'detached--session-directory)
+                 (seq-uniq)
+                 (seq-do #'detached--watch-session-directory))))
+
 ;;;;; Database
 
 (defun detached--db-initialize ()
@@ -1318,8 +1362,7 @@ log to deduce the end time."
 
 (defun detached--watch-session-directory (session-directory)
   "Watch for events in SESSION-DIRECTORY."
-  (unless (alist-get session-directory detached--watched-session-directories
-                     nil nil #'string=)
+  (unless (detached--watched-session-directory-p session-directory)
     (push
      `(,session-directory . ,(file-notify-add-watch
                               session-directory
@@ -1415,10 +1458,10 @@ If event is cased by an update to the `detached' 
database, re-initialize
 
 (defun detached--duration-str (session)
   "Return SESSION's duration time."
-  (let* ((duration (if (eq 'active (detached--session-state session))
-                       (- (time-to-seconds) (plist-get (detached--session-time 
session) :start))
-                     (plist-get
-                      (detached--session-time session) :duration)))
+  (let* ((duration (if (eq (detached--session-state session) 'inactive)
+                       (plist-get
+                        (detached--session-time session) :duration)
+                     (- (time-to-seconds) (plist-get (detached--session-time 
session) :start))))
          (time (round duration))
          (hours (/ time 3600))
          (minutes (/ (mod time 3600) 60))
@@ -1450,9 +1493,12 @@ If event is cased by an update to the `detached' 
database, re-initialize
 
 (defun detached--state-str (session)
   "Return string based on SESSION state."
-  (if (eq 'active (detached--session-state session))
-      "*"
-    ""))
+  (pcase (detached--session-state session)
+    ('active (if (detached--session-accessible-p session)
+                 "*"
+               "?"))
+    ('inactive "")
+    ('unknown "?")))
 
 (defun detached--working-dir-str (session)
   "Return working directory of SESSION."
diff --git a/test/detached-test.el b/test/detached-test.el
index 7a4be364f9..9771e70ac6 100644
--- a/test/detached-test.el
+++ b/test/detached-test.el
@@ -250,9 +250,12 @@
 ;;;;; String representations
 
 (ert-deftest detached-test-duration-str ()
-  (should (string= "1s" (detached--duration-str (detached--session-create 
:time '(:duration 1)))))
-  (should (string= "1m 1s" (detached--duration-str (detached--session-create 
:time '(:duration 61)))))
-  (should (string= "1h 1m 1s" (detached--duration-str 
(detached--session-create :time '(:duration 3661))))))
+  (should (string= "1s" (detached--duration-str
+                         (detached--session-create :time '(:duration 1) :state 
'inactive))))
+  (should (string= "1m 1s" (detached--duration-str
+                            (detached--session-create :time '(:duration 61) 
:state 'inactive))))
+  (should (string= "1h 1m 1s" (detached--duration-str
+                               (detached--session-create :time '(:duration 
3661) :state 'inactive)))))
 
 (ert-deftest detached-test-creation-str ()
   ;; Make sure to set the TIMEZONE before executing the test to avoid
@@ -271,8 +274,23 @@
   (should (string= "" (detached--status-str (detached--session-create :status 
'(unknown . 0))))))
 
 (ert-deftest detached-test-state-str ()
-  (should (string= "*" (detached--state-str (detached--session-create :state 
'active))))
-  (should (string= "" (detached--state-str (detached--session-create :state 
'inactive)))))
+  ;; Accessible sessions
+  (cl-letf (((symbol-function #'detached--session-accessible-p) (lambda (_) 
t)))
+    (should (string= "*" (detached--state-str
+                          (detached--session-create :state 'active))))
+    (should (string= "" (detached--state-str
+                         (detached--session-create :state 'inactive))))
+    (should (string= "?" (detached--state-str
+                          (detached--session-create :state 'unknown)))))
+
+  ;; Inaccessible sessions
+  (cl-letf (((symbol-function #'detached--session-accessible-p) (lambda (_) 
nil)))
+    (should (string= "?" (detached--state-str
+                          (detached--session-create :state 'active))))
+    (should (string= "" (detached--state-str
+                         (detached--session-create :state 'inactive))))
+    (should (string= "?" (detached--state-str
+                          (detached--session-create :state 'unknown))))))
 
 (ert-deftest detached-test-working-dir-str ()
   (should



reply via email to

[Prev in Thread] Current Thread [Next in Thread]