Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic backup of the queue. Also, signal handling #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cfsmp3
Copy link

@cfsmp3 cfsmp3 commented Nov 28, 2021

Currently the queue is only saved when the program ends cleanly. But on crash, system reboot, etc, that never happens, and then system comes up with whatever we had on the queue when it was last saved.

This PR does two things:

  • Capture signals, so for example SIGTERM is properly handled - we exit the GTK loop, we save the queue, we write a message.
  • Make a backup of the queue every minute if needed. This could be made configurable but I don't think it's worth the trouble.

Tested by running the daemon from terminal, sending it SIGTERM, also keyboard interrupt, send notifications every few seconds to make the queue dirty and force a save, then don't send any additional notification to verify that it won't save unless needed.

Bonus: Also run black, which is why there's a few format changes in the files.

@cfsmp3 cfsmp3 requested a review from kgilmer November 28, 2021 17:27
Copy link
Member

@kgilmer kgilmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think some users will welcome the function of periodic persistence of notifications I would like to error on the side of doing less by default and have it be an opt-in feature.

The code reformatting looks good to me, makes things more readable, although I do not work with Python very often.

self._object = RoficationDbusObject(queue)
# create GLib mainloop, this is needed to make D-Bus work and takes care of catching signals.
self._loop = MainLoop()

def run(self) -> None:
timeout_add_seconds(60, self.backup_queue)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer it if this behavior is opt-in. It's a matter of opinion either way but I reckon that most users would not want this functionality as it comes at the cost of periodic disk writes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with those odds :-) I definitely prefer some I/O to notification loss. Note that the write only happens if there are unsaved notifications.
Anyway, I'm made it configurable with default to no save. I know that I will keep my notifications safe, but well :-)


from ._metadata import ROFICATION_VERSION, ROFICATION_NAME, ROFICATION_URL
from ._notification import Notification, Urgency
from ._queue import NotificationQueue
from datetime import datetime

NOTIFICATIONS_DBUS_INTERFACE = 'org.freedesktop.Notifications'
NOTIFICATIONS_DBUS_OBJECT_PATH = '/org/freedesktop/Notifications'
NOTIFICATIONS_DBUS_INTERFACE = "org.freedesktop.Notifications"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the single quote vs double. Does this change make a syntactic difference or simply style?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's style, but it comes from here:

https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html

Black is an opinionated Python formatter - quite in use these days, as it solves all the religious format wars by imposing one style :-)

So just running black on a set of files will give you consistency across the code.

filename = self._queue_file

if not filename:
warn("Unable to save notification queue, no file specified")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this emit in syslog or somewhere else user visible?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using warn there as that's what _queue.py already had for other messages. In any case, its output just go to stderr.

try:
print('Saving notification queue to file')
with open(filename, 'w') as f:
print(f"Saving notification queue to file: {filename}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@cfsmp3 cfsmp3 requested a review from kgilmer November 29, 2021 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants