ement.el icon indicating copy to clipboard operation
ement.el copied to clipboard

Add minor-mode to run sync-watchdog timer

Open phil-s opened this issue 2 years ago • 6 comments

I don't have much info about this, but sometimes ement simply stops updating rooms on its own.

Today I've been using ement.el for many hours, and I'm running the stand-alone Element client as well (with a different auth token), and I just noticed that Element had a message in a room which ement.el wasn't showing me. As soon as I typed g in that room, ement.el fetched the missing message (which had been sent more than an hour earlier) as well as a bunch of others (my desktop notifications showed me a heap of messages from other rooms in rapid succession).

All I could find in the messages buffer was ement-room-sync: Wrong type argument: arrayp, nil which I've noticed happens when typing g in the Notifications or Mentions buffers. I don't know whether that could have made the auto-sync fail? (Maybe if the timer only runs upon successful completion of the previous attempt?)

I've also seen the sync failing overnight, such that I've taken to typing g in some room when I start using my laptop in the morning to ensure that the sync is happening.

Maybe some kind of fail-safe system could be triggered periodically to make sure that the sync system gets restarted after a while if necessary?

phil-s avatar Jun 27 '23 03:06 phil-s

You can see the code that handles sync failures and retries here: https://github.com/alphapapa/ement.el/blob/909abd44427ba1d3b9e83eda422c520dbe55880b/ement.el#L506-L530 Only certain types of failures are automatically retried (e.g. DNS lookup failure isn't retried).

When syncing is interrupted, a message is printed, and the mode line shows "Not-syncing".

All I could find in the messages buffer was ement-room-sync: Wrong type argument: arrayp, nil which I've noticed happens when typing g in the Notifications or Mentions buffers. I don't know whether that could have made the auto-sync fail?

No, that merely indicates that that command doesn't handle being called in notifications or mentions buffers.

Maybe some kind of fail-safe system could be triggered periodically to make sure that the sync system gets restarted after a while if necessary?

Yes, some more code could be written to retry in other cases. I've tried to keep it very simple so far, especially because plz is also being developed in parallel, and it would be easy for a bug in one package or the other, combined with automatic retries, to retry too often or too quickly, etc. There's room for more maturation of this code, for sure.

It could be as simple as something like this:

(run-at-time t 300 (lambda ()
                     (cl-loop for (_id . session) in ement-sessions
                              do (ignore-errors
                                   (ement--sync session)))))

alphapapa avatar Jun 27 '23 11:06 alphapapa

A quick elaboration on that code in case it's helpful to others:

(defvar my-ement-auto-sync-timer nil)
(defvar my-ement-auto-sync-interval 300
  "Number of seconds between calls to `my-ement-auto-sync-sessions'.")

(defun my-ement-auto-sync-sessions ()
  "Re-sync every session in `ement-sessions'."
  (cl-loop for (_id . session) in ement-sessions
           do (ignore-errors
                (ement--sync session))))

(define-minor-mode my-ement-auto-sync-mode
  "Periodically sync Ement sessions as a failsafe.

Calls `my-ement-auto-sync-sessions' every `my-ement-auto-sync-interval' seconds."
  :global t
  :init-value nil
  :lighter ""
  (when (and my-ement-auto-sync-timer
	     (timerp my-ement-auto-sync-timer))
    (cancel-timer my-ement-auto-sync-timer)
    (setq my-ement-auto-sync-timer nil))
  (when my-ement-auto-sync-mode
    (setq my-ement-auto-sync-timer
	  (run-at-time t my-ement-auto-sync-interval
		       #'my-ement-auto-sync-sessions))))

And then either toggle the mode interactively, or enable it in lisp with something like:

(with-eval-after-load "ement"
  (my-ement-auto-sync-mode 1))

phil-s avatar Jun 27 '23 12:06 phil-s

I suppose we could add such a minor mode to a future version. Do you think it should be enabled by default?

alphapapa avatar Jun 28 '23 07:06 alphapapa

I suppose we could add such a minor mode to a future version. Do you think it should be enabled by default?

I'm inclined to say yes just because I've experienced the issue, and it feels like a pretty lightweight workaround.

phil-s avatar Jun 28 '23 13:06 phil-s

Status update? This feature has been something that I've been missing out on for quite some time, but forgot to create an issue for. Glad that someone has already done that and shared a solution with us. Thanks a lot @phil-s!

Icy-Thought avatar Aug 26 '24 11:08 Icy-Thought

@Icy-Thought Someone could send a pull request implementing this feature. It hasn't been a high-priority issue to work on.

alphapapa avatar Aug 26 '24 12:08 alphapapa