Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15 from hughrun/scheduling
Browse files Browse the repository at this point in the history
Scheduling
  • Loading branch information
hughrun authored Feb 3, 2019
2 parents 8042672 + 29a2027 commit 2f02a2d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ You should now have a line at the bottom of settings.py saying something like `p

## Usage

For most users you simply need to run `install.sh` and then forget about it. The install script will ask a series of questions, authorise your app, and optionally set up a daily task to refresh your list. On MacOS this is done via **launchd** and on Linux via **cron**. If you want to use cron instead of launchd on Mac, just choose 'Linux' when asked - but I wouldn't recommend it. In future you will have the option to do run the `refresh` task weekly instead of daily, and choose minutes as well as an hour of the day.
For most users you simply need to run `install.sh` and then forget about it. The install script will ask a series of questions, authorise your app, and optionally set up a daily or weekly task to refresh your list. On MacOS this is done via **launchd** and on Linux via **cron**. If you want to use cron instead of launchd on Mac, just choose 'Linux' when asked - but I wouldn't recommend it (read on for why).

In both cases, `pocketsnack refresh` will run at the specified time. In the case of **cron**, this will only happen if the machine is up and logged on - e.g. a server. In the case of **launchd**, the script will be associated with your user account. If your machine is sleeping or your account logged out at the scheduled time, it will run immediately when you wake the machine up or log in. This allows you to set it for, e.g. 5am and be confident that when you open your Macbook at 7am the script will run and your Pocket account will refresh.

If you use launchd, log files for stdout and errors will be created wherever you saved _pocketsnack_.

To run commands manually, use `pocketsnack [command]`.

Expand Down
31 changes: 31 additions & 0 deletions daily.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.getpocket.pocketsnack</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/bin:/usr/bin:/usr/local/bin</string> <!-- this allows us to run scripts from /usr/local/bin -->
</dict>
<key>StandardOutPath</key>
<string>PATH</string>
<key>StandardErrorPath</key>
<string>PATH</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/pocketsnack</string>
<string>refresh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>0</integer>
<key>Hour</key>
<integer>6</integer> <!-- this line is changed by install.sh -->
<key>Weekday</key>
<integer>1</integer> <!-- this line is changed by install.sh -->
</dict>
</dict>
</plist>
6 changes: 5 additions & 1 deletion hourly.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<key>PATH</key>
<string>/bin:/usr/bin:/usr/local/bin</string> <!-- this allows us to run scripts from /usr/local/bin -->
</dict>
<key>StandardOutPath</key>
<string>/var/log/pocketsnack.log</string>
<key>StandardErrorPath</key>
<string>/var/log/pocketsnack.error.log</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/pocketsnack</string>
Expand All @@ -17,7 +21,7 @@
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>0</integer> <!-- this line will be changed by install.sh soon -->
<integer>0</integer>
<key>Hour</key>
<integer>6</integer> <!-- this line is changed by install.sh -->
</dict>
Expand Down
87 changes: 79 additions & 8 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,71 @@ if [ $(which python3) ]; then
sleep 5s
pocketsnack authorise
# here we should ask if they want to set up a regular job
echo 'Do you want to automatically refresh your Pocket List? This will run "stash" and then "lucky_dip" once per day.'
select daily in Yes No
echo 'Do you want to automatically refresh your Pocket List? This will run "stash" and then "lucky_dip" once per day, or once per week.'
auto=("No Thanks" "Yes Daily" "Yes Weekly")
select automated in "${auto[@]}"
do
case $daily in
Yes)
case $REPLY in
3)
# which day do they want to refresh their list? Must be between 1 (Monday) and 7 (Sunday)
echo "Which day do you want to refresh your Pocket list?"
select day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday;
do
if [[ -z $day ]]; then
echo "Please enter a number between 1 and 7."
else
daynum=$REPLY
# what hour do they want to refresh their list? Must be a number between 0 and 23. Is 6 by default.
echo 'Enter an hour between 0 and 23 for when you want to run your script.'
read hour
isnum='^2[0-3]$|^1?[0-9]$'
while ! [[ $hour =~ $isnum ]]
do
echo 'Invalid choice - please enter a whole number between 0 and 23'
read hour
done
echo "You chose $hour"
# are they running MacOS or Linux
echo "Ok last question: are you running (1) MacOS or (2) Linux?"
select operating_system in MacOS Linux;
do
case $operating_system in
MacOS)
# create a new plist file with the hour number and put it in the right place
sed "26 s/[0-9][0-9]*/$hour/" <daily.plist >~/Library/LaunchAgents/com.getpocket.pocketsnack.plist
# edit the file in place to update the day
sed -i '' -e "28 s/[0-9][0-9]*/$daynum/" ~/Library/LaunchAgents/com.getpocket.pocketsnack.plist
# edit the file in place to update the logfiles
logpath="<string>$(pwd)/pocketsnack.log</string>"
errorpath="<string>$(pwd)/pocketsnack-error.log</string>"
sed -i '' -e "13c\\
$logpath
" ~/Library/LaunchAgents/com.getpocket.pocketsnack.plist
sed -i '' -e "15c\\
$errorpath
" ~/Library/LaunchAgents/com.getpocket.pocketsnack.plist
# unload launchd file in case we're re-loading it
launchctl unload ~/Library/Launchagents/com.getpocket.pocketsnack.plist
# load launchd file
launchctl load ~/Library/Launchagents/com.getpocket.pocketsnack.plist
echo "You're all set up! Enjoy your new Pocket experience."
exit 0
;;
Linux)
# edit crontab
(crontab -l ; echo "0 $hour * * $day /usr/local/bin/pocketsnack refresh") 2> /dev/null | sort | uniq | crontab -
echo "You're all set up! Enjoy your new Pocket experience."
exit 0
;;
*)
echo "Please select 1 for MacOS or 2 for Linux"
;;
esac
done
fi
done
;;
2)
# what hour do they want to refresh their list? Must be a number between 0 and 23. Is 6 by default.
echo 'Enter an hour between 0 and 23 for when you want to run your script.'
read hour
Expand All @@ -32,7 +92,7 @@ if [ $(which python3) ]; then
echo 'Invalid choice - please enter a whole number between 0 and 23'
read hour
done
echo "you chose $hour"
echo "You chose $hour"
# are they running MacOS or Linux
echo "Ok last question: are you running (1) MacOS or (2) Linux?"
select operating_system in MacOS Linux
Expand All @@ -41,6 +101,17 @@ if [ $(which python3) ]; then
MacOS)
# create a new plist file with the hour number and put it in the right place
sed "22 s/[0-9][0-9]*/$hour/" <hourly.plist >~/Library/LaunchAgents/com.getpocket.pocketsnack.plist
# edit the file in place to update the logfiles
logpath="<string>$(pwd)/pocketsnack.log</string>"
errorpath="<string>$(pwd)/pocketsnack-error.log</string>"
sed -i '' -e "13c\\
$logpath
" ~/Library/LaunchAgents/com.getpocket.pocketsnack.plist
sed -i '' -e "15c\\
$errorpath
" ~/Library/LaunchAgents/com.getpocket.pocketsnack.plist
# unload launchd file in case we're re-loading it
launchctl unload ~/Library/Launchagents/com.getpocket.pocketsnack.plist
# load launchd file
launchctl load ~/Library/Launchagents/com.getpocket.pocketsnack.plist
echo "You're all set up! Enjoy your new Pocket experience."
Expand All @@ -58,12 +129,12 @@ if [ $(which python3) ]; then
esac
done
;;
No)
echo 'No script scheduled. You can use pocket-snack manually at any time using the pocketsnack command.'
1)
echo 'No script scheduled. You can use pocket-snack manually at any time using the `pocketsnack` command.'
exit 0
;;
*)
echo 'Please select 1 to schedule daily refreshes or 2 to exit.'
echo 'Please select 1 to exit, 2 to schedule daily refreshes or 3 to schedule weekly refreshes.'
;;
esac
done
Expand Down

0 comments on commit 2f02a2d

Please sign in to comment.