Skip to content

Commit c9eb9fd

Browse files
authored
Merge pull request #23 from amfern/job-overview
jobliststubs command
2 parents 67f650e + 98a5ec0 commit c9eb9fd

File tree

5 files changed

+143
-36
lines changed

5 files changed

+143
-36
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ The output will be equal to the [Nomad Evaluation API structure](https://www.nom
152152

153153
`nomad-firehose jobs` will monitor all job changes in the Nomad cluster and emit a firehose event per change to the configured sink.
154154

155-
The output will be equal to the *full* [Nomad Job API structure](https://www.nomadproject.io/api/jobs.html)
155+
The output will be equal to the *full* [Nomad Job API structure](https://www.nomadproject.io/api/jobs.html#read-job)
156+
157+
### `jobliststubs`
158+
159+
`nomad-firehose jobliststubs` will monitor all job changes in the Nomad cluster and emit a firehose event per change to the configured sink.
160+
161+
The output will be equal to the job list [Nomad Job API structure](https://www.nomadproject.io/api/jobs.html#list-jobs)
156162

157163
### `deployments`
158164

command/jobs/app.go renamed to command/jobs/base.go

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package jobs
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"time"
76

@@ -10,8 +9,12 @@ import (
109
log "github.com/sirupsen/logrus"
1110
)
1211

12+
13+
type WatchJobListFunc func(job *nomad.JobListStub)
14+
15+
1316
// Firehose ...
14-
type Firehose struct {
17+
type FirehoseBase struct {
1518
lastChangeIndex uint64
1619
lastChangeTimeCh chan interface{}
1720
nomadClient *nomad.Client
@@ -20,7 +23,7 @@ type Firehose struct {
2023
}
2124

2225
// NewFirehose ...
23-
func NewFirehose() (*Firehose, error) {
26+
func NewFirehoseBase() (*FirehoseBase, error) {
2427
nomadClient, err := nomad.NewClient(nomad.DefaultConfig())
2528
if err != nil {
2629
return nil, err
@@ -31,23 +34,19 @@ func NewFirehose() (*Firehose, error) {
3134
return nil, err
3235
}
3336

34-
return &Firehose{
37+
return &FirehoseBase{
3538
nomadClient: nomadClient,
3639
sink: sink,
3740
stopCh: make(chan struct{}, 1),
3841
lastChangeTimeCh: make(chan interface{}, 1),
3942
}, nil
4043
}
4144

42-
func (f *Firehose) Name() string {
43-
return "jobs"
44-
}
45-
46-
func (f *Firehose) UpdateCh() <-chan interface{} {
45+
func (f *FirehoseBase) UpdateCh() <-chan interface{} {
4746
return f.lastChangeTimeCh
4847
}
4948

50-
func (f *Firehose) SetRestoreValue(restoreValue interface{}) error {
49+
func (f *FirehoseBase) SetRestoreValue(restoreValue interface{}) error {
5150
switch restoreValue.(type) {
5251
case int:
5352
f.lastChangeIndex = uint64(restoreValue.(int))
@@ -60,11 +59,11 @@ func (f *Firehose) SetRestoreValue(restoreValue interface{}) error {
6059
}
6160

6261
// Start the firehose
63-
func (f *Firehose) Start() {
62+
func (f *FirehoseBase) Start(w WatchJobListFunc) {
6463
go f.sink.Start()
6564

6665
// watch for allocation changes
67-
go f.watch()
66+
go f.watch(w)
6867

6968
// Save the last event time every 5s
7069
go f.persistLastChangeTime(5 * time.Second)
@@ -77,15 +76,15 @@ func (f *Firehose) Start() {
7776
}
7877

7978
// Stop the firehose
80-
func (f *Firehose) Stop() {
79+
func (f *FirehoseBase) Stop() {
8180
close(f.stopCh)
8281
f.sink.Stop()
8382
}
8483

8584
// Write the Last Change Time to Consul so if the process restarts,
8685
// it will try to resume from where it left off, not emitting tons of double events for
8786
// old events
88-
func (f *Firehose) persistLastChangeTime(interval time.Duration) {
87+
func (f *FirehoseBase) persistLastChangeTime(interval time.Duration) {
8988
ticker := time.NewTicker(interval)
9089

9190
for {
@@ -99,18 +98,8 @@ func (f *Firehose) persistLastChangeTime(interval time.Duration) {
9998
}
10099
}
101100

102-
// Publish an update from the firehose
103-
func (f *Firehose) Publish(update *nomad.Job) {
104-
b, err := json.Marshal(update)
105-
if err != nil {
106-
log.Error(err)
107-
}
108-
109-
f.sink.Put(b)
110-
}
111-
112101
// Continously watch for changes to the allocation list and publish it as updates
113-
func (f *Firehose) watch() {
102+
func (f *FirehoseBase) watch(w WatchJobListFunc) {
114103
q := &nomad.QueryOptions{
115104
WaitIndex: f.lastChangeIndex,
116105
WaitTime: 5 * time.Minute,
@@ -148,15 +137,7 @@ func (f *Firehose) watch() {
148137
newMax = job.ModifyIndex
149138
}
150139

151-
go func(jobID string) {
152-
fullJob, _, err := f.nomadClient.Jobs().Info(jobID, &nomad.QueryOptions{})
153-
if err != nil {
154-
log.Errorf("Could not read job %s: %s", jobID, err)
155-
return
156-
}
157-
158-
f.Publish(fullJob)
159-
}(job.ID)
140+
w(job)
160141
}
161142

162143
// Update WaitIndex and Last Change Time for next iteration

command/jobs/job.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package jobs
2+
3+
import (
4+
"encoding/json"
5+
6+
nomad "github.com/hashicorp/nomad/api"
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
// Firehose ...
11+
type JobFirehose struct {
12+
FirehoseBase
13+
}
14+
15+
// NewFirehose ...
16+
func NewJobFirehose() (*JobFirehose, error) {
17+
base, err := NewFirehoseBase()
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
return &JobFirehose{FirehoseBase: *base}, nil
23+
}
24+
25+
26+
func (f *JobFirehose) Name() string {
27+
return "jobs"
28+
}
29+
30+
// Publish an update from the firehose
31+
func (f *JobFirehose) Publish(update *nomad.Job) {
32+
b, err := json.Marshal(update)
33+
if err != nil {
34+
log.Error(err)
35+
}
36+
37+
f.sink.Put(b)
38+
}
39+
40+
41+
func (f *JobFirehose) Start() {
42+
f.FirehoseBase.Start(f.watchJobList)
43+
}
44+
45+
func (f *JobFirehose) watchJobList(job *nomad.JobListStub) {
46+
go func(jobID string) {
47+
fullJob, _, err := f.nomadClient.Jobs().Info(jobID, &nomad.QueryOptions{})
48+
if err != nil {
49+
log.Errorf("Could not read job %s: %s", jobID, err)
50+
return
51+
}
52+
53+
f.Publish(fullJob)
54+
}(job.ID)
55+
}

command/jobs/jobsliststub.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package jobs
2+
3+
import (
4+
"encoding/json"
5+
6+
nomad "github.com/hashicorp/nomad/api"
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
// Firehose ...
11+
type JobListStubFirehose struct {
12+
FirehoseBase
13+
}
14+
15+
// NewFirehose ...
16+
func NewJobListStubFirehose() (*JobListStubFirehose, error) {
17+
base, err := NewFirehoseBase()
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
return &JobListStubFirehose{FirehoseBase: *base}, nil
23+
}
24+
25+
func (f *JobListStubFirehose) Name() string {
26+
return "jobliststub"
27+
}
28+
29+
func (f *JobListStubFirehose) Start() {
30+
f.FirehoseBase.Start(f.watchJobList)
31+
}
32+
33+
// Publish an update from the firehose
34+
func (f *JobListStubFirehose) Publish(update *nomad.JobListStub) {
35+
b, err := json.Marshal(update)
36+
if err != nil {
37+
log.Error(err)
38+
}
39+
40+
f.sink.Put(b)
41+
}
42+
43+
func (f *JobListStubFirehose) watchJobList(job *nomad.JobListStub) {
44+
f.Publish(job)
45+
}
46+
47+

main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,25 @@ func main() {
9494
Name: "jobs",
9595
Usage: "Firehose nomad job changes",
9696
Action: func(c *cli.Context) error {
97-
firehose, err := jobs.NewFirehose()
97+
firehose, err := jobs.NewJobFirehose()
98+
if err != nil {
99+
return err
100+
}
101+
102+
manager := helper.NewManager(firehose)
103+
if err := manager.Start(); err != nil {
104+
log.Fatal(err)
105+
return err
106+
}
107+
108+
return nil
109+
},
110+
},
111+
{
112+
Name: "jobliststubs",
113+
Usage: "Firehose nomad job info changes",
114+
Action: func(c *cli.Context) error {
115+
firehose, err := jobs.NewJobListStubFirehose()
98116
if err != nil {
99117
return err
100118
}

0 commit comments

Comments
 (0)