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 option to notify on certain days or hours. #66

Open
ghost opened this issue Dec 27, 2016 · 5 comments
Open

Add option to notify on certain days or hours. #66

ghost opened this issue Dec 27, 2016 · 5 comments
Assignees

Comments

@ghost
Copy link

ghost commented Dec 27, 2016

Hello,

When arch is used on a workstation it is important to keep it updated, but you also need to be able to work on your computer without being distracted by a notification all the time.

With the current update checking interval system, you almost get a notification every single day.

It would be awesome to be able to set days on which the extension would check update or not.

For exemple, I like to update my computer on Friday, so that I have the weekend to fix any problem that would occur during an update. But I don't want to get any notification during the week as I'm often busy working.

What do you think about that ?

@RaphaelRochet
Copy link
Owner

I love that idea. I think I'll try to make this soon.

@ghost
Copy link
Author

ghost commented Jul 12, 2017

Hey Raphaël, did you get some time looking at this feature ? I wish I could help but my javascript skill are quite low...

@RaphaelRochet
Copy link
Owner

Not yet .. still on the todo list. That's something I'd like to use too, I guess it'll be the next feature I'll add.

@ghost
Copy link

ghost commented Oct 3, 2017

It would be nice to be able to automatically check for updates at a certain time each n days. And if the computer missed the last check because it was turned off or asleep, then check immediately when it's woken up or a new session starts, but still stick to the schedule for the next update

@chrisjbillington
Copy link

chrisjbillington commented Dec 5, 2018

A workaround for this is to put the functionality in your checking script. For example I use checkupdates+aur, but to only check once a week, I've made this script, saved as ~/bin/suppress_check_until:

#!/usr/bin/python

""""This script is prefixed to the  "Command to check for package updates" setting of
the arch update indicator gnome shell extension like so:

    /home/bilbo/suppress_check_until 4 17 checkupdates+aur

Where 4 17 means to schedule checks for Friday at 5pm, and checkupdates+aur is the
command to run to actually check for updates. If this script is called before the
scheduled time, it will return nothing, and the arch update indicator will appear as if
there are no pending updates. Once the scheduled time has arrived, subsequent calls to
this script will actually check for pending updates and print them. Further checks will
run until there are no pending updates remaining, at which point it is assumed the user
installed all the updates, and the next check is scheduled.

The logic of the scheduling means that if the computer is off for has no network at the
scheduled time, a check will still be run next time the computer is on and has a
connection, so updates will not be missed. Also, if the user takes more than a week to
respond to the indicator and install updates, the scheduled checks will not 'pile up'.
After the user updates, the next check will be scheduled for the next Friday at 5pm, for
example, regardless of how long it has been since the last check.

This script stores a file /var/tmp/arch_indicator_time_of_last_update to remember when
the last update was so that it can schedule the next.

This script requires the python-tzlocal package """

import sys
import subprocess
from datetime import datetime, timedelta, timezone
from tzlocal import get_localzone

DAY_TO_CHECK = int(sys.argv[1])  # Day of the week 0 = Mon, 1 = Tues etc
HOUR_TO_CHECK = int(sys.argv[2])  # Hour of the day, i.e. 15 = 3pm
UPDATE_CMD = sys.argv[3:]

LOCALZONE = get_localzone()
UTC = timezone.utc
LAST_UPDATE_FILE = '/var/tmp/arch_indicator_time_of_last_update'

# Get an aware datetime for when we last did a check that resulted in no updates.
try:
    with open(LAST_UPDATE_FILE, 'r') as f:
        last_update = datetime.fromisoformat(f.read().strip())
except FileNotFoundError:
    last_update = datetime.utcnow().replace(tzinfo=UTC) - timedelta(28)

# Convert to localtime:
last_update = last_update.astimezone(LOCALZONE)

# Find the next DAY_TO_CHECK and TIME_TO_CHECK after thatm as a naive datetime:
next_check = datetime(
    last_update.year, last_update.month, last_update.day, last_update.hour
)
next_check += timedelta(hours=(HOUR_TO_CHECK - next_check.hour) % 24)
next_check += timedelta(days=(DAY_TO_CHECK - next_check.weekday()) % 7)

# If it's past that time, run whatever command follows the other args in sys.argv
if datetime.now() >= next_check:
    result = subprocess.run(UPDATE_CMD, stdout=subprocess.PIPE)
    updates = result.stdout.decode()
    print(updates)
    # Every time this script gets called, keep printing these available updates until
    # the user installs them and there are none left.
    if result.returncode == 0 and not updates:
        # Save that this update round is over:
        with open(LAST_UPDATE_FILE, 'w') as f:
            f.write(datetime.now(LOCALZONE).isoformat() + '\n')
    sys.exit(result.returncode)

and then in the extension settings I put for the "command to check for package updates":

/home/bilbo/bin/suppress_check_until 4 17 checkupdates+aur

Which means to check for updates only if it's later than 5pm (17th hour of the day) on the Friday (4th day of the week) after the last time a check was done. The script is calling checkupdates+aur only if that condition is met, otherwise it's just returning, and the extension thinks there are no updates. But if there are updates, it shows them until they are installed and there are none left (after which it won't show any more until the next week).

So then you can set the extension to check for updates every 15 minutes or an hour or whatever, but checks will actually only be done if enough time has passed since the last round of updates. The script is saving a file to disk so it remembers last time updates were all done so that if it misses one due to the computer being off or having no internet or whatever, it will keep trying rather than waiting a whole week.

I'm sure you could do something with a systemd timer instead, rather than the script doing the date/time arithmetic itself. You could make the timer create a certain file every Friday at 5pm, or whatever. Then in your "command to check for package updates" line you'd put a call to bash with an if statement to only check for updates if the file exists, then to delete the file if the update check returned that there were no updates, or something like that.

But the Python script idea occurred to me first, and I've just set this up so it's not tested - I'll wait a week or two now and see if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants