tasks now run in goroutines

main
simonkellet 2 years ago
parent acff443905
commit 84ab0bf369
  1. 39
      run.go

@ -4,30 +4,52 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"sync"
"time"
"github.com/gen2brain/beeep" "github.com/gen2brain/beeep"
) )
// Run will take the config file and for each task, run a go routine // Run will take the config file and for each task, run a go routine
func TaskRun(c Config) { func TaskRun(c Config) {
var wg sync.WaitGroup
for i := range c.Tasks { for i := range c.Tasks {
title := c.Tasks[i].TaskName title := c.Tasks[i].TaskName
desc := c.Tasks[i].TaskDesc interval := c.Tasks[i].Interval
err := beeep.Notify(title, desc, "")
var desc string
// check if desc is empty and use the cmd output, if not just get the desc
if c.Tasks[i].TaskDesc == "" {
desc = TaskCMD(c.Tasks[i])
} else {
desc = c.Tasks[i].TaskDesc
}
asset := c.Tasks[i].Icon
//run the commands with go func()!
wg.Add(1)
go func() {
fmt.Printf("%s has time interval %d\n", title, interval)
for range time.Tick(time.Second * interval) {
err := beeep.Notify(title, desc, asset)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not display \"%s\":\n\t%s\n", title, err) fmt.Fprintf(os.Stderr, "ERROR: could not display \"%s\":\n\t%s\n", title, err)
} }
} }
wg.Done()
}()
}
wg.Wait()
} }
// TaskCMD will take the config struct and run all the "CMD" fields with "bash -c [command]" and return the output // TaskCMD will take in a Task, run the command with bash and return the string output
func TaskCMD(c Config) (output string) { func TaskCMD(t Task) (output string) {
for i := range c.Tasks { command := t.CMD
command := c.Tasks[i].CMD
if command == " " { if command == " " {
fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", c.Tasks[i].TaskName) fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", t.TaskName)
return return
} }
@ -35,8 +57,5 @@ func TaskCMD(c Config) (output string) {
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not run command \"%s\":\n\t%s\n", command, err) fmt.Fprintf(os.Stderr, "ERROR: could not run command \"%s\":\n\t%s\n", command, err)
} }
//fmt.Printf("Output:\n%s\n", out)
return string(out) return string(out)
} }
return
}

Loading…
Cancel
Save