86 lines
1.6 KiB
Go
86 lines
1.6 KiB
Go
|
package main
|
||
|
|
||
|
/*
|
||
|
This is scrapped for later use.
|
||
|
Now make a simple, one task runner with params
|
||
|
defined as env vars.
|
||
|
*/
|
||
|
import (
|
||
|
"os/exec"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
|
||
|
type JobStatus uint64
|
||
|
|
||
|
const (
|
||
|
NEW = iota
|
||
|
RUNNING
|
||
|
FINISHED
|
||
|
FAILED
|
||
|
)
|
||
|
|
||
|
type Job interface {
|
||
|
Start()
|
||
|
Result() string
|
||
|
}
|
||
|
|
||
|
// Tasks are defined via config and are used to run jobs
|
||
|
type Task struct {
|
||
|
RepoFullName string
|
||
|
Owner string
|
||
|
Secret string
|
||
|
command exec.Cmd
|
||
|
}
|
||
|
|
||
|
type ShellCommandJob struct {
|
||
|
JobStatus
|
||
|
Task
|
||
|
UUID string
|
||
|
ExitCode uint8
|
||
|
stdout string
|
||
|
err *error
|
||
|
TimeCreated time.Time
|
||
|
TimeStarted time.Time
|
||
|
TimeFinished time.Time // This is redundant but keep it for now
|
||
|
TimeConsumed time.Duration
|
||
|
}
|
||
|
|
||
|
// TODO: Replace with task method to generate jobs
|
||
|
func NewShellJob(t Task, uuid string) *ShellCommandJob {
|
||
|
|
||
|
timeNow := time.Now().UTC()
|
||
|
job := ShellCommandJob{
|
||
|
JobStatus: NEW,
|
||
|
Task: t,
|
||
|
UUID: uuid,
|
||
|
TimeCreated: timeNow,
|
||
|
}
|
||
|
return &job
|
||
|
}
|
||
|
|
||
|
func (j *ShellCommandJob) Start() {
|
||
|
j.TimeStarted = time.Now().UTC()
|
||
|
stdout, err := j.command.Output()
|
||
|
if err != nil {
|
||
|
j.JobStatus = FAILED
|
||
|
}
|
||
|
j.TimeFinished = time.Now().UTC()
|
||
|
j.TimeConsumed = time.Since(j.TimeStarted)
|
||
|
j.stdout = string(stdout)
|
||
|
}
|
||
|
|
||
|
func (j *ShellCommandJob) Result() string {
|
||
|
return j.stdout
|
||
|
}
|
||
|
|
||
|
func (j *ShellCommandJob) String() string {
|
||
|
switch j.JobStatus {
|
||
|
case 0: return "NEW"
|
||
|
case 1: return "RUNNING"
|
||
|
case 2: return "FINISHED"
|
||
|
case 3: return "FAILD"
|
||
|
default: return "UNDEFINED"
|
||
|
}
|
||
|
}
|