[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dtache e2efe466e0 076/158: Improve error handling with
From: |
ELPA Syncer |
Subject: |
[elpa] externals/dtache e2efe466e0 076/158: Improve error handling with dtache-valid-session |
Date: |
Wed, 19 Jan 2022 18:57:59 -0500 (EST) |
branch: externals/dtache
commit e2efe466e090a40156692f569adae7b1a2119924
Author: Niklas Eklund <niklas.eklund@posteo.net>
Commit: Niklas Eklund <niklas.eklund@posteo.net>
Improve error handling with dtache-valid-session
Make commands use dtache-valid-session to make sure that the session
provided to the function actually is a session object, and if so that
it can be found. If it is not found it is indicative that the host
that the missing session belongs to is not up to date. So we trigger
an automatic cleanup for that session.
---
dtache-eshell.el | 35 ++++++-----
dtache-shell.el | 19 +++---
dtache.el | 189 +++++++++++++++++++++++++++++++------------------------
3 files changed, 134 insertions(+), 109 deletions(-)
diff --git a/dtache-eshell.el b/dtache-eshell.el
index 7077261659..00467cacba 100644
--- a/dtache-eshell.el
+++ b/dtache-eshell.el
@@ -89,23 +89,24 @@ If prefix-argument directly DETACH from the session."
"Attach to SESSION."
(interactive
(list (dtache-eshell-select-session)))
- (if (and (dtache--session-active-p session)
- (not (dtache--session-redirect-only session)))
- (cl-letf* ((dtache--dtach-mode 'attach)
- (input
- (dtache-dtach-command session t))
- ((symbol-function #'eshell-add-to-history) #'ignore))
- (eshell-kill-input)
- ;; Hide the input from the user
- (let ((begin (point))
- (end))
- (insert input)
- (setq end (point))
- (overlay-put (make-overlay begin end) 'invisible t)
- (insert " "))
- (setq dtache--buffer-session session)
- (call-interactively #'eshell-send-input))
- (dtache-open-session session)))
+ (when (dtache-valid-session session)
+ (if (and (dtache--session-active-p session)
+ (not (dtache--session-redirect-only session)))
+ (cl-letf* ((dtache--dtach-mode 'attach)
+ (input
+ (dtache-dtach-command session t))
+ ((symbol-function #'eshell-add-to-history) #'ignore))
+ (eshell-kill-input)
+ ;; Hide the input from the user
+ (let ((begin (point))
+ (end))
+ (insert input)
+ (setq end (point))
+ (overlay-put (make-overlay begin end) 'invisible t)
+ (insert " "))
+ (setq dtache--buffer-session session)
+ (call-interactively #'eshell-send-input))
+ (dtache-open-session session))))
;;;; Minor mode
diff --git a/dtache-shell.el b/dtache-shell.el
index cc56bae01f..664929cf4c 100644
--- a/dtache-shell.el
+++ b/dtache-shell.el
@@ -98,15 +98,16 @@ This function also makes sure that the HISTFILE is disabled
for local shells."
cluttering the comint-history with dtach commands."
(interactive
(list (dtache-shell-select-session)))
- (if (and (dtache--session-active-p session)
- (not (dtache--session-redirect-only session)))
- (cl-letf ((dtache-shell--current-session session)
- (comint-input-sender #'dtache-shell--attach-input-sender)
- ((symbol-function 'comint-add-to-input-history) (lambda (_)
t)))
- (setq dtache--buffer-session session)
- (comint-kill-input)
- (comint-send-input))
- (dtache-open-session session)))
+ (when (dtache-valid-session session)
+ (if (and (dtache--session-active-p session)
+ (not (dtache--session-redirect-only session)))
+ (cl-letf ((dtache-shell--current-session session)
+ (comint-input-sender #'dtache-shell--attach-input-sender)
+ ((symbol-function 'comint-add-to-input-history) (lambda (_)
t)))
+ (setq dtache--buffer-session session)
+ (comint-kill-input)
+ (comint-send-input))
+ (dtache-open-session session))))
;;;; Support functions
diff --git a/dtache.el b/dtache.el
index cd06c06bfa..af70718a43 100644
--- a/dtache.el
+++ b/dtache.el
@@ -227,126 +227,136 @@ If called with prefix-argument the output is
suppressed."
"Open a `dtache' SESSION."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (if-let ((open-function
- (dtache--session-open-function session)))
- (funcall open-function session)
- (dtache-open-dwim session)))
+ (when (dtache-valid-session session)
+ (if-let ((open-function
+ (dtache--session-open-function session)))
+ (funcall open-function session)
+ (dtache-open-dwim session))))
;;;###autoload
(defun dtache-compile-session (session)
"Open log of SESSION in `compilation-mode'."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (let ((buffer-name "*dtache-session-output*")
- (file
- (dtache-session-file session 'log))
- (tramp-verbose 1))
- (when (file-exists-p file)
- (with-current-buffer (get-buffer-create buffer-name)
- (setq-local buffer-read-only nil)
- (erase-buffer)
- (insert (dtache-session-output session))
- (setq-local default-directory
- (dtache--session-working-directory session))
- (run-hooks 'dtache-compile-hooks)
- (dtache-log-mode)
- (compilation-minor-mode)
- (setq-local font-lock-defaults '(compilation-mode-font-lock-keywords
t))
- (font-lock-mode)
- (read-only-mode))
- (pop-to-buffer buffer-name))))
+ (when (dtache-valid-session session)
+ (let ((buffer-name "*dtache-session-output*")
+ (file
+ (dtache-session-file session 'log))
+ (tramp-verbose 1))
+ (when (file-exists-p file)
+ (with-current-buffer (get-buffer-create buffer-name)
+ (setq-local buffer-read-only nil)
+ (erase-buffer)
+ (insert (dtache-session-output session))
+ (setq-local default-directory
+ (dtache--session-working-directory session))
+ (run-hooks 'dtache-compile-hooks)
+ (dtache-log-mode)
+ (compilation-minor-mode)
+ (setq-local font-lock-defaults '(compilation-mode-font-lock-keywords
t))
+ (font-lock-mode)
+ (read-only-mode))
+ (pop-to-buffer buffer-name)))))
;;;###autoload
(defun dtache-rerun-session (session)
"Rerun SESSION."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (let* ((default-directory
- (dtache--session-working-directory session))
- (dtache-open-session-function
- (dtache--session-open-function session))
- (dtache-session-callback-function
- (dtache--session-callback-function session))
- (dtache-session-status-function
- (dtache--session-status-function session)))
- (dtache-start-session (dtache--session-command session))))
+ (when (dtache-valid-session session)
+ (let* ((default-directory
+ (dtache--session-working-directory session))
+ (dtache-open-session-function
+ (dtache--session-open-function session))
+ (dtache-session-callback-function
+ (dtache--session-callback-function session))
+ (dtache-session-status-function
+ (dtache--session-status-function session)))
+ (dtache-start-session (dtache--session-command session)))))
;;;###autoload
(defun dtache-copy-session-output (session)
"Copy SESSION's log."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (with-temp-buffer
- (insert (dtache-session-output session))
- (kill-new (buffer-string))))
+ (when (dtache-valid-session session)
+ (with-temp-buffer
+ (insert (dtache-session-output session))
+ (kill-new (buffer-string)))))
;;;###autoload
(defun dtache-copy-session-command (session)
"Copy SESSION command."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (kill-new (dtache--session-command session)))
+ (when (dtache-valid-session session)
+ (kill-new (dtache--session-command session))))
;;;###autoload
(defun dtache-insert-session-command (session)
"Insert SESSION."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (insert (dtache--session-command session)))
+ (when (dtache-valid-session session)
+ (insert (dtache--session-command session))))
;;;###autoload
(defun dtache-delete-session (session)
"Delete SESSION."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (if (dtache--session-active-p session)
- (message "Kill session first before removing it.")
- (dtache--db-remove-entry session)))
+ (when (dtache-valid-session session)
+ (if (dtache--session-active-p session)
+ (message "Kill session first before removing it.")
+ (dtache--db-remove-entry session))))
;;;###autoload
(defun dtache-kill-session (session)
"Send a TERM signal to SESSION."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (let* ((pid (dtache--session-pid session)))
- (when pid
- (dtache--kill-processes pid))))
+ (when (dtache-valid-session session)
+ (let* ((pid (dtache--session-pid session)))
+ (when pid
+ (dtache--kill-processes pid)))))
;;;###autoload
(defun dtache-open-output (session)
"Open SESSION's output."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (let* ((buffer-name "*dtache-session-output*")
- (file-path
- (dtache-session-file session 'log))
- (tramp-verbose 1))
- (if (file-exists-p file-path)
- (progn
- (with-current-buffer (get-buffer-create buffer-name)
- (setq-local buffer-read-only nil)
- (erase-buffer)
- (insert (dtache-session-output session))
- (setq-local default-directory (dtache--session-working-directory
session))
- (dtache-log-mode)
- (goto-char (point-max)))
- (pop-to-buffer buffer-name))
- (message "Dtache can't find file: %s" file-path))))
+ (when (dtache-valid-session session)
+ (let* ((buffer-name "*dtache-session-output*")
+ (file-path
+ (dtache-session-file session 'log))
+ (tramp-verbose 1))
+ (if (file-exists-p file-path)
+ (progn
+ (with-current-buffer (get-buffer-create buffer-name)
+ (setq-local buffer-read-only nil)
+ (erase-buffer)
+ (insert (dtache-session-output session))
+ (setq-local default-directory (dtache--session-working-directory
session))
+ (dtache-log-mode)
+ (goto-char (point-max)))
+ (pop-to-buffer buffer-name))
+ (message "Dtache can't find file: %s" file-path)))))
;;;###autoload
(defun dtache-tail-output (session)
"Tail SESSION's output."
(interactive
(list (dtache-completing-read (dtache-get-sessions))))
- (if (dtache--session-active-p session)
- (let* ((file-path
- (dtache-session-file session 'log))
- (tramp-verbose 1))
- (when (file-exists-p file-path)
- (find-file-other-window file-path)
- (dtache-tail-mode)
- (goto-char (point-max))))
- (dtache-open-output session)))
+ (when (dtache-valid-session session)
+ (if (dtache--session-active-p session)
+ (let* ((file-path
+ (dtache-session-file session 'log))
+ (tramp-verbose 1))
+ (when (file-exists-p file-path)
+ (find-file-other-window file-path)
+ (dtache-tail-mode)
+ (goto-char (point-max))))
+ (dtache-open-output session))))
;;;###autoload
(defun dtache-diff-session (session1 session2)
@@ -355,17 +365,19 @@ If called with prefix-argument the output is suppressed."
(let ((sessions (dtache-get-sessions)))
`(,(dtache-completing-read sessions)
,(dtache-completing-read sessions))))
- (let ((buffer1 "*dtache-session-output-1*")
- (buffer2 "*dtache-session-output-2*"))
- (with-current-buffer (get-buffer-create buffer1)
- (erase-buffer)
- (insert (dtache--session-header session1))
- (insert (dtache-session-output session1)))
- (with-current-buffer (get-buffer-create buffer2)
- (erase-buffer)
- (insert (dtache--session-header session2))
- (insert (dtache-session-output session2)))
- (ediff-buffers buffer1 buffer2)))
+ (when (and (dtache-valid-session session1)
+ (dtache-valid-session session2))
+ (let ((buffer1 "*dtache-session-output-1*")
+ (buffer2 "*dtache-session-output-2*"))
+ (with-current-buffer (get-buffer-create buffer1)
+ (erase-buffer)
+ (insert (dtache--session-header session1))
+ (insert (dtache-session-output session1)))
+ (with-current-buffer (get-buffer-create buffer2)
+ (erase-buffer)
+ (insert (dtache--session-header session2))
+ (insert (dtache-session-output session2)))
+ (ediff-buffers buffer1 buffer2))))
;;;###autoload
(defun dtache-detach-session ()
@@ -439,6 +451,7 @@ nil before closing."
Optionally SUPPRESS-OUTPUT."
(if (and (not (eq dtache--dtach-mode 'attach))
(or suppress-output
+ (eq dtache--dtach-mode 'new)
(dtache-redirect-only-p command)))
(let* ((inhibit-message t)
(dtache--dtach-mode 'new)
@@ -554,14 +567,24 @@ Optionally make the path LOCAL to host."
;; Add `dtache-shell-mode'
(add-hook 'shell-mode-hook #'dtache-shell-mode)))
+(defun dtache-valid-session (session)
+ "Ensure that SESSION is valid.
+
+If session is not valid trigger an automatic cleanup on SESSION's host."
+ (when (dtache-session-p session)
+ (if (not (dtache--session-missing-p session))
+ t
+ (let ((host (dtache--session-host session)))
+ (message "Session does not exist. Initiate sesion cleanup on host %s"
host)
+ (dtache-cleanup-host-sessions host)
+ nil))))
+
(defun dtache-cleanup-host-sessions (host)
"Run cleanuup on HOST sessions."
- (seq-do
- (lambda (it)
- (when (and (string= host (dtache--session-host it))
- (dtache--session-missing-p it))
- (dtache--db-remove-entry it)))
- (dtache--db-get-sessions)))
+ (thread-last (dtache--db-get-sessions)
+ (seq-filter (lambda (it) (string= host (dtache--session-host
it))))
+ (seq-filter #'dtache--session-missing-p)
+ (seq-do #'dtache--db-remove-entry)))
(defun dtache-session-exit-code-status (session)
"Return status based on exit-code in SESSION."
- [elpa] externals/dtache 115de6c1a2 022/158: Add default value for dtache-max-command-length, (continued)
- [elpa] externals/dtache 115de6c1a2 022/158: Add default value for dtache-max-command-length, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 09d378e50a 024/158: Fix read-only problematic, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 4e99fc9daa 049/158: Deprecate dtache-list-sessions, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache cb1762ffc2 052/158: Add presentation about dtache version 0.2, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 5d9c8aa54f 053/158: Add macOS support, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 01a27b4dc2 056/158: Update README with reference to consult-dtache, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache b75af1b9a7 059/158: Update notification setup, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache b47bc3be09 061/158: Update copyright years, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 182ab7ccc6 064/158: Add integration with consult, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache ea6d8ae426 068/158: Add missing autoloads, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache e2efe466e0 076/158: Improve error handling with dtache-valid-session,
ELPA Syncer <=
- [elpa] externals/dtache 79f1d905a9 078/158: Add example of how to enhance a command with dtach, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache dae6db33db 083/158: Fix various minor problems, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 52e839ff4a 007/158: Update README with links, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 7e8727f3a2 020/158: Add TODO to investigate unknown inactive sessions, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache b94baaeecd 021/158: Fix bug in setup notification, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 763246bffe 026/158: Remove explicit dependency on dtache-env, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 087e8f4c73 019/158: Update README, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache d574161815 042/158: Simplify buffer handling, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 1878cefb2f 038/158: Remove embark dependency, ELPA Syncer, 2022/01/19
- [elpa] externals/dtache 3c61776c7f 014/158: Fix dtache initialization, ELPA Syncer, 2022/01/19