2018-04-28 17:44:44 +00:00
|
|
|
package utils
|
|
|
|
|
2018-04-28 19:17:44 +00:00
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/animenotifier/arn"
|
2019-03-30 06:39:40 +00:00
|
|
|
"github.com/blitzprog/color"
|
2018-04-28 19:17:44 +00:00
|
|
|
)
|
2018-04-28 17:44:44 +00:00
|
|
|
|
|
|
|
// JobInfo gives you information about a background job.
|
|
|
|
type JobInfo struct {
|
|
|
|
Name string
|
|
|
|
LastStarted time.Time
|
|
|
|
LastFinished time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRunning tells you whether the given job is running or not.
|
|
|
|
func (job *JobInfo) IsRunning() bool {
|
2018-04-28 19:17:44 +00:00
|
|
|
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
|
2018-04-28 17:44:44 +00:00
|
|
|
}
|