Skip to content

Instantly share code, notes, and snippets.

@marklap
Created May 14, 2014 21:38
Show Gist options
  • Save marklap/e11c33c317263aaed81d to your computer and use it in GitHub Desktop.
Save marklap/e11c33c317263aaed81d to your computer and use it in GitHub Desktop.
Tinkering with creating structs, interfaces for Jobs, Tasks, Executions, etc
package main
import (
"fmt"
"time"
"github.com/twinj/uuid"
)
const (
TASK_EXEC uint = iota
)
const (
JOB_REMOTE uint = iota
JOB_LOCAL
)
const (
EXECUTION_SUCCESS uint = iota
EXECUTION_FAILURE
EXECUTION_ABORT
EXECUTION_CANCEL
)
type Task struct {
typ uint
uuid uuid.UUID
cmd string
}
func (self Task) Type() string {
switch self.typ {
case TASK_EXEC:
return "exec"
}
return "unknown"
}
type Job struct {
uuid uuid.UUID
typ uint
tasks []Task
executions []Execution
}
func (self Job) String() string {
return fmt.Sprintf("<Job [type:%s, uuid:%s]>", self.Type(), self.uuid)
}
func (self Job) Type() string {
switch self.typ {
case JOB_REMOTE:
return "remote"
case JOB_LOCAL:
return "local"
}
return "unknown"
}
func (self Job) Exec() Execution {
exec := Execution{
ts: time.Now(),
uuid: uuid.NewV4(),
success: true,
result: ExecutionResult{typ: EXECUTION_SUCCESS, val: "well done"},
}
self.executions = append(self.executions, exec)
return exec
}
type ExecutionResult struct {
typ uint
val string
}
func (self ExecutionResult) Type() string {
switch self.typ {
case EXECUTION_ABORT:
return "abort"
case EXECUTION_CANCEL:
return "cancel"
case EXECUTION_FAILURE:
return "failure"
case EXECUTION_SUCCESS:
return "success"
}
return "unknown"
}
type Execution struct {
ts time.Time
uuid uuid.UUID
success bool
result ExecutionResult
}
func (self Execution) String() string {
return fmt.Sprintf("<Execution [ts: %s, result:%s, uuid:%s]>", self.ts, self.result.Type(), self.uuid)
}
func NewJob(typ uint) *Job {
return &Job{typ: typ, uuid: uuid.NewV4()}
}
func main() {
for i := 0; i < 10000; i++ {
job := NewJob(JOB_LOCAL)
exec := job.Exec()
fmt.Println(job)
fmt.Println(exec)
}
fmt.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment