forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cockroachdb#27 from crowdflux/himanshu-github
persistent channel
- Loading branch information
Showing
23 changed files
with
568 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package rabbitmq | ||
package internal | ||
|
||
// | ||
//package main | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package feed_line | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/crowdflux/angel/app/DAL/clients/rabbitmq" | ||
"github.com/crowdflux/angel/app/models" | ||
"github.com/crowdflux/angel/app/plog" | ||
"github.com/streadway/amqp" | ||
"sync" | ||
) | ||
|
||
// ShortHand for channel of FLUs i.e. FeedLine | ||
type Fl struct { | ||
amqpChan *amqp.Channel | ||
queueName string | ||
once sync.Once | ||
} | ||
|
||
func New(name string) Fl { | ||
|
||
ch := rabbitmq.GetNewChannel() | ||
|
||
q, err := ch.QueueDeclare( | ||
name, // name | ||
true, // durable | ||
false, // delete when unused | ||
false, // exclusive | ||
false, // no-wait | ||
nil, // arguments | ||
) | ||
|
||
if err != nil { | ||
plog.Error("Feedline", err, "error declaring queue, name: ", name) | ||
panic(err) | ||
} | ||
|
||
return Fl{ | ||
amqpChan: ch, | ||
queueName: q.Name, | ||
} | ||
} | ||
|
||
func (fl *Fl) Push(flu FLU) { | ||
|
||
// Send only the models.Feedline part of the flu in bytes | ||
bty, _ := json.Marshal(flu.FeedLineUnit) | ||
|
||
// This is async | ||
// TODO Think about a way to guarantee this operation also | ||
err := fl.amqpChan.Publish( | ||
"", // exchange | ||
fl.queueName, // routing key | ||
false, // mandatory | ||
false, // immediate | ||
amqp.Publishing{ | ||
ContentType: "application/json", | ||
Body: bty, | ||
}) | ||
if err != nil { | ||
plog.Error("Feedline", err, "error publishing to channel", "flu_id: "+flu.ID.String()) | ||
panic(err) | ||
} | ||
|
||
// Just for safety: if someone forgets | ||
// to ConfirmReceive the flu received from a queue | ||
// then reconfirm it here as it will most | ||
// probably be a bug | ||
if flu.delivery.Acknowledger != nil { | ||
flu.ConfirmReceive() | ||
} | ||
} | ||
|
||
func (fl *Fl) Receiver() <-chan FLU { | ||
|
||
println("Feedline, subscribe request: ", fl.queueName) | ||
|
||
var fluChan chan FLU | ||
var flag bool = false | ||
|
||
fl.once.Do(func() { | ||
|
||
fluChan = make(chan FLU, 50000) | ||
|
||
deliveryChan, err := fl.amqpChan.Consume( | ||
fl.queueName, // queue | ||
"", // consumer | ||
false, // auto-ack | ||
false, // exclusive | ||
false, // no-local | ||
false, // no-wait | ||
nil, // args | ||
) | ||
if err != nil { | ||
plog.Error("Feedline", err, "error consuming queue, name:", fl.queueName) | ||
panic(err) | ||
} | ||
|
||
go func() { | ||
|
||
for msg := range deliveryChan { | ||
|
||
flu := models.FeedLineUnit{} | ||
json.Unmarshal(msg.Body, &flu) | ||
|
||
fluChan <- FLU{ | ||
FeedLineUnit: flu, | ||
delivery: msg, | ||
} | ||
} | ||
}() | ||
|
||
flag = true | ||
}) | ||
|
||
if flag { | ||
return (<-chan FLU)(fluChan) | ||
} else { | ||
panic(errors.New("Feedline already subscribed, name: " + fl.queueName)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ func TestNew(t *testing.T) { | |
}, | ||
}) | ||
|
||
flu := <-fl.Out() | ||
flu := <-fl.Receiver() | ||
|
||
flu.ConfirmReceive() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package feed_line | ||
|
||
import ( | ||
"database/sql" | ||
"github.com/crowdflux/angel/app/models" | ||
"github.com/crowdflux/angel/app/models/uuid" | ||
"github.com/crowdflux/angel/app/services/work_flow_svc/feed_line" | ||
"github.com/lib/pq" | ||
"time" | ||
) | ||
|
||
func LogStepEntry(flu feed_line.FLU, metaData ...models.JsonF) { | ||
|
||
log(flu, true, metaData) | ||
} | ||
|
||
func LogStepExit(flu feed_line.FLU, metaData ...models.JsonF) { | ||
|
||
log(flu, false, metaData) | ||
} | ||
|
||
func log(flu feed_line.FLU, stepEntry bool, metaData ...models.JsonF) { | ||
|
||
metaDataMerged := models.JsonF{} | ||
|
||
for _, m := range metaData { | ||
metaDataMerged.Merge(m) | ||
} | ||
|
||
metaDataMerged.Merge(models.JsonF{"build": flu.Build}) | ||
|
||
fluLog := models.FeedLineLog{ | ||
FluId: flu.ID, | ||
Message: sql.NullString{"asfas", true}, | ||
MetaData: metaDataMerged, | ||
StepType: sql.NullInt64{-1, false}, | ||
StepEntry: sql.NullBool{stepEntry, true}, | ||
StepExit: sql.NullBool{!stepEntry, true}, | ||
StepId: flu.StepId, | ||
WorkFlowId: uuid.Nil, | ||
CreatedAt: pq.NullTime{time.Now(), true}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package feed_line | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/crowdflux/angel/app/DAL/clients/rabbitmq" | ||
"github.com/crowdflux/angel/app/models" | ||
"github.com/crowdflux/angel/app/plog" | ||
) | ||
|
||
type feedLineLogChan struct { | ||
} | ||
|
||
func newLoggerChan() feedLineLogChan { | ||
|
||
ch := rabbitmq.GetNewChannel() | ||
|
||
q, err := ch.QueueDeclare( | ||
"FeedlineLogQueue", // name | ||
true, // durable | ||
false, // delete when unused | ||
false, // exclusive | ||
false, // no-wait | ||
nil, // arguments | ||
) | ||
|
||
if err != nil { | ||
plog.Error("Feedline Logger", err, "error declaring queue") | ||
panic(err) | ||
} | ||
|
||
outChan, err := ch.Consume( | ||
q.Name, // queue | ||
"", // consumer | ||
false, // auto-ack | ||
false, // exclusive | ||
false, // no-local | ||
false, // no-wait | ||
nil, // args | ||
) | ||
if err != nil { | ||
plog.Error("Feedline", err, "error consuming queue") | ||
} | ||
|
||
fl := Fl{make(chan FLU, 50000), outChan, ch, q.Name, false} | ||
|
||
go func() { | ||
|
||
for msg := range outChan { | ||
|
||
flu := models.FeedLineUnit{} | ||
json.Unmarshal(msg.Body, &flu) | ||
|
||
fl.ch <- FLU{ | ||
FeedLineUnit: flu, | ||
delivery: msg, | ||
} | ||
} | ||
|
||
}() | ||
|
||
return fl | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.