Skip to content

Commit

Permalink
Update daemon; some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
naspeh committed Apr 22, 2024
1 parent 775401a commit a0ac2cd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 56 deletions.
18 changes: 9 additions & 9 deletions testcmd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Available Commands:
completion Generate the autocompletion script for the specified shell
daemon Update the duration for current activity in a loop
daemon Update the duration for current activity and run hook if specified
db Execute sqlite3 with db file
finish Finish current activity
help Help about any command
Expand All @@ -27,26 +27,26 @@
- name: daemon-help
cmd: daemon -h
output: |
Update the duration for current activity in a loop
Update the duration for current activity and run hook if specified
Usage:
timefor daemon [flags]
Flags:
--break-time duration time for a break reminder (default 1h20m0s)
-h, --help help for daemon
--hook string a hook command template
--repeat-time duration time to repeat a break reminder (default 10m0s)
--sleep-time duration sleep time in the loop (default 30s)
--break-interval duration interval to show a break reminder (default 1h20m0s)
-h, --help help for daemon
--hook string a hook command template
--repeat-interval duration interval to repeat a break reminder (default 10m0s)
--update-interval duration interval to update activity time in db (default 30s)
- name: daemon--bad-hook-template
cmd: daemon --sleep-time 1s --hook 'echo "{{if}}"'
cmd: daemon --hook 'echo "{{if}}"'
code: 1
output: |
Error: cannot render hook command: failed to parse template: template: tpl:1: missing value for if
- name: daemon--err-in-hook-cmd
cmd: daemon --sleep-time 1s --hook 'exit 1'
cmd: daemon --hook 'exit 1'
code: 1
output: |
running hook command: exit 1
Expand Down
4 changes: 2 additions & 2 deletions timefor
Git LFS file not shown
87 changes: 45 additions & 42 deletions timefor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
)

const (
timeForExpire = 10 * time.Minute
sleepTimeForDaemon = 30 * time.Second
breakTimeForDaemon = 80 * time.Minute
repeatTimeForDaemon = 10 * time.Minute
defaultTpl = "{{if .Active}}☭{{else}}☯{{end}} {{.FormatLabel}}"
intervalToExpire = 10 * time.Minute
defaultIntervalToUpdateDb = 30 * time.Second
defaultIntervalToShowBreakReminder = 80 * time.Minute
defaultIntervalToRepeatBreakReminder = 10 * time.Minute
defaultTpl = "{{if .Active}}☭{{else}}☯{{end}} {{.FormatLabel}}"
)

var dbFile string
Expand Down Expand Up @@ -172,34 +172,34 @@ func newCmd(db *sqlx.DB) *cobra.Command {

var daemonCmd = &cobra.Command{
Use: "daemon",
Short: "Update the duration for current activity in a loop",
Short: "Update the duration for current activity and run hook if specified",
RunE: func(cmd *cobra.Command, args []string) error {
sleepTime, err := cmd.Flags().GetDuration("sleep-time")
intervalToUpdateDb, err := cmd.Flags().GetDuration("update-interval")
if err != nil {
return err
}
breakTime, err := cmd.Flags().GetDuration("break-time")
intervalToShowBreakReminder, err := cmd.Flags().GetDuration("break-interval")
if err != nil {
return err
}
repeatTime, err := cmd.Flags().GetDuration("repeat-time")
intervalToRepeatBreakReminder, err := cmd.Flags().GetDuration("repeat-interval")
if err != nil {
return err
}
hook, err := cmd.Flags().GetString("hook")
if err != nil {
return err
}
err = Daemon(db, sleepTime, breakTime, repeatTime, hook)
err = Daemon(db, intervalToUpdateDb, intervalToShowBreakReminder, intervalToRepeatBreakReminder, hook)
if err != nil {
return err
}
return nil
},
}
daemonCmd.Flags().Duration("sleep-time", sleepTimeForDaemon, "sleep time in the loop")
daemonCmd.Flags().Duration("break-time", breakTimeForDaemon, "time for a break reminder")
daemonCmd.Flags().Duration("repeat-time", repeatTimeForDaemon, "time to repeat a break reminder")
daemonCmd.Flags().Duration("update-interval", defaultIntervalToUpdateDb, "interval to update activity time in db")
daemonCmd.Flags().Duration("break-interval", defaultIntervalToShowBreakReminder, "interval to show a break reminder")
daemonCmd.Flags().Duration("repeat-interval", defaultIntervalToRepeatBreakReminder, "interval to repeat a break reminder")
daemonCmd.Flags().StringP("hook", "", "", "a hook command template")

var dbCmd = &cobra.Command{
Expand Down Expand Up @@ -434,40 +434,15 @@ func Show(db *sqlx.DB, tpl string) error {
return nil
}

// Daemon updates the duration of current activity then sleeps for a while
// Daemon updates the duration of current activity and runs the hook if specified
func Daemon(db *sqlx.DB, sleepTime time.Duration, breakTime time.Duration, repeatTime time.Duration, hook string) error {
var notified time.Time
var lastHook string
for {
_, err := UpdateIfExists(db, "", false)
if err != nil {
return err
}
activity, err := Latest(db)
if err != nil {
return err
}
duration, err := activeDuration(db)
if err != nil {
return err
}
if activity.Active() && duration > breakTime && time.Since(notified) > repeatTime {
args := []string{
"Take a break!",
fmt.Sprintf("Active for %v already", formatDuration(duration)),
}
if duration.Seconds() > breakTime.Seconds()*1.2 {
args = append(args, "-u", "critical")
} else {
// default timeout is too quick, so set it to 5s
args = append(args, "-t", "5000")
}
err := exec.Command("notify-send", args...).Run()
if err != nil {
fmt.Printf("cannot send notification: %v", err)
}
notified = time.Now()
}
if hook != "" {
cmd, err := activity.Format(hook)
if err != nil {
Expand All @@ -482,7 +457,35 @@ func Daemon(db *sqlx.DB, sleepTime time.Duration, breakTime time.Duration, repea
}
}
}
time.Sleep(sleepTime)
if activity.Active() && time.Since(activity.Updated()) > sleepTime {
fmt.Printf("updating time for %s\n", activity.Name)
_, err := UpdateIfExists(db, "", false)
if err != nil {
return err
}
duration, err := activeDuration(db)
if err != nil {
return err
}
if activity.Active() && duration > breakTime && time.Since(notified) > repeatTime {
args := []string{
"Take a break!",
fmt.Sprintf("Active for %v already", formatDuration(duration)),
}
if duration.Seconds() > breakTime.Seconds()*1.2 {
args = append(args, "-u", "critical")
} else {
// default timeout is too quick, so set it to 5s
args = append(args, "-t", "5000")
}
err := exec.Command("notify-send", args...).Run()
if err != nil {
fmt.Printf("cannot send notification: %v", err)
}
notified = time.Now()
}
}
time.Sleep(1)
}
}

Expand All @@ -503,7 +506,7 @@ func activeDuration(db *sqlx.DB) (time.Duration, error) {
}
if prev.ID == 0 && cur.Expired() {
break
} else if prev.Started().Sub(cur.Updated()) > timeForExpire {
} else if prev.Started().Sub(cur.Updated()) > intervalToExpire {
break
}
duration += cur.Duration()
Expand Down Expand Up @@ -690,7 +693,7 @@ func (a Activity) Updated() time.Time {
}

func (a Activity) Expired() bool {
return time.Since(a.Updated()) > timeForExpire
return time.Since(a.Updated()) > intervalToExpire
}

func (a Activity) Active() bool {
Expand Down
5 changes: 2 additions & 3 deletions timefor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -77,7 +76,7 @@ func TestSchema(t *testing.T) {
}

func TestCmd(t *testing.T) {
file, err := ioutil.TempFile("", "logtest")
file, err := os.CreateTemp("", "logtest")
if err != nil {
t.Fatal(err)
}
Expand All @@ -86,7 +85,7 @@ func TestCmd(t *testing.T) {
db = sqlx.MustOpen("sqlite3", file.Name())
defer db.Close()

data, err := ioutil.ReadFile("testcmd.yaml")
data, err := os.ReadFile("testcmd.yaml")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit a0ac2cd

Please sign in to comment.