Skip to content

Commit

Permalink
Merge pull request #6 from open-oni/feature/fake-job-success
Browse files Browse the repository at this point in the history
Feature/fake job success
  • Loading branch information
jechols authored Nov 7, 2024
2 parents c5f839c + 92b4747 commit 368b74f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func main() {
var srv = &gliderssh.Server{Addr: BABind}
srv.AddHostKey(HostKeySigner)

var sessionID atomic.Uint64
var sessionID atomic.Int64
srv.Handle(func(_s gliderssh.Session) {
var s = session{Session: _s, id: sessionID.Add(1)}

Expand Down
15 changes: 11 additions & 4 deletions cmd/agent/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
"github.com/open-oni/oni-agent/internal/version"
)

var sessionID atomic.Uint64
var sessionID atomic.Int64

type session struct {
ssh.Session
id uint64
id int64
}

// Status is a string type the handler's "status" JSON may return
Expand Down Expand Up @@ -157,12 +157,19 @@ func (s session) purgeBatch(name string) {
}

func (s session) getJob(arg string) (job *queue.Job, found bool) {
var id, _ = strconv.ParseUint(arg, 10, 64)
var id, _ = strconv.ParseInt(arg, 10, 64)
if id == 0 {
s.respond(StatusError, fmt.Sprintf("%q is not a valid job id", arg), nil)
return nil, false
}

// Allow fake jobs to get a response instead of an error so that automations
// that haven't accounted for "no job needed" responses don't fail
var noop = queue.NoOpJob()
if id == noop.ID() {
return noop, true
}

var j = JobRunner.GetJob(id)
if j == nil {
s.respond(StatusError, "Job not found", H{"job": H{"id": id}})
Expand Down Expand Up @@ -220,7 +227,7 @@ func (s session) getJobLogs(arg string) {
}

func (s session) respondNoJob() {
s.respond(StatusSuccess, "No-op: job is redundant or already completed", H{"job": H{"id": -1}})
s.respond(StatusSuccess, "No-op: job is redundant or already completed", H{"job": H{"id": queue.NoOpJob().ID()}})
}

func (s session) queueJob(command string, args ...string) {
Expand Down
23 changes: 21 additions & 2 deletions internal/queue/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (

// Job represents a single ONI management job to be run
type Job struct {
id uint64
id int64
status JobStatus
cmd *exec.Cmd
args []string
Expand All @@ -36,6 +36,25 @@ type Job struct {
pid int
}

var noopJob = &Job{
id: -1,
status: StatusSuccessful,
cmd: nil,
args: nil,
queuedAt: time.Now(),
startedAt: time.Now(),
completedAt: time.Now(),
err: nil,
stdout: logstream.Stream{},
stderr: logstream.Stream{},
pid: -1,
}

// NoOpJob returns a job that does nothing and has a success status
func NoOpJob() *Job {
return noopJob
}

// start creates the command with the given context, starting the command and
// storing its pid and start time. After calling start, wait must then be
// called to let the command finish and release resources.
Expand Down Expand Up @@ -79,7 +98,7 @@ func (j *Job) wait() error {
}

// ID returns the job's assigned ID number
func (j *Job) ID() uint64 {
func (j *Job) ID() int64 {
return j.id
}

Expand Down
10 changes: 5 additions & 5 deletions internal/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
// Queue holds the list of ONI jobs we need to run
type Queue struct {
m sync.Mutex
seq uint64
lookup map[uint64]*Job
seq int64
lookup map[int64]*Job
binpath string
env []string
queue chan *Job
Expand All @@ -25,7 +25,7 @@ type Queue struct {
// New provides a new job queue
func New(oniPath string) *Queue {
var binpath = filepath.Join(oniPath, "manage.py")
var q = &Queue{lookup: make(map[uint64]*Job), queue: make(chan *Job, 1000), binpath: binpath}
var q = &Queue{lookup: make(map[int64]*Job), queue: make(chan *Job, 1000), binpath: binpath}

// We store the env vars needed to emulate Python's virtual environment,
// which essentially operates by setting three env vars. There's other stuff
Expand Down Expand Up @@ -59,7 +59,7 @@ func New(oniPath string) *Queue {

// NewJob queues up a new ONI management command from the given args, and
// returns the queued job's id
func (q *Queue) NewJob(args ...string) uint64 {
func (q *Queue) NewJob(args ...string) int64 {
q.m.Lock()
defer q.m.Unlock()

Expand All @@ -77,7 +77,7 @@ func (q *Queue) NewJob(args ...string) uint64 {
}

// GetJob returns a job by its id
func (q *Queue) GetJob(id uint64) *Job {
func (q *Queue) GetJob(id int64) *Job {
return q.lookup[id]
}

Expand Down

0 comments on commit 368b74f

Please sign in to comment.