Added background jobs UI

This commit is contained in:
2018-04-28 21:17:44 +02:00
parent 59633707c1
commit 262b6018d1
7 changed files with 122 additions and 11 deletions

View File

@ -1,6 +1,13 @@
package utils
import "time"
import (
"os/exec"
"path"
"time"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
// JobInfo gives you information about a background job.
type JobInfo struct {
@ -11,6 +18,30 @@ type JobInfo struct {
// IsRunning tells you whether the given job is running or not.
func (job *JobInfo) IsRunning() bool {
now := time.Now()
return job.LastStarted.After(job.LastFinished) && !now.After(job.LastFinished)
return job.LastStarted.After(job.LastFinished)
}
// Start will start the job.
func (job *JobInfo) Start() error {
cmd := exec.Command(path.Join(arn.Root, "jobs", job.Name, job.Name))
err := cmd.Start()
if err != nil {
return err
}
job.LastStarted = time.Now()
// Wait for job finish in another goroutine
go func() {
err := cmd.Wait()
if err != nil {
color.Red("Job '%s' encountered an error: %s", job.Name, err.Error())
}
job.LastFinished = time.Now()
}()
return nil
}