tasks now run in goroutines

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

@ -4,39 +4,58 @@ import (
"fmt"
"os"
"os/exec"
"sync"
"time"
"github.com/gen2brain/beeep"
)
// Run will take the config file and for each task, run a go routine
func TaskRun(c Config) {
var wg sync.WaitGroup
for i := range c.Tasks {
title := c.Tasks[i].TaskName
desc := c.Tasks[i].TaskDesc
err := beeep.Notify(title, desc, "")
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not display \"%s\":\n\t%s\n", title, err)
interval := c.Tasks[i].Interval
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 {
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
func TaskCMD(c Config) (output string) {
for i := range c.Tasks {
command := c.Tasks[i].CMD
// TaskCMD will take in a Task, run the command with bash and return the string output
func TaskCMD(t Task) (output string) {
command := t.CMD
if command == " " {
fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", c.Tasks[i].TaskName)
return
}
if command == " " {
fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", t.TaskName)
return
}
out, err := exec.Command("bash", "-c", command).Output()
if err != nil {
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)
out, err := exec.Command("bash", "-c", command).Output()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not run command \"%s\":\n\t%s\n", command, err)
}
return
return string(out)
}

Loading…
Cancel
Save