Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple refactors #283

Merged
merged 4 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions grpc-server/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,20 @@ func (s *server) Push(ctx context.Context, in *hardware.PushRequest) (*hardware.
return &hardware.Empty{}, err
}

var fn func() error
msg := ""
const msg = "inserting into DB"
data, err := json.Marshal(hw)
if err != nil {
logger.Error(err)
}

labels["op"] = "insert"
msg = "inserting into DB"
fn = func() error { return s.db.InsertIntoDB(ctx, string(data)) }

metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
err = fn()
err = s.db.InsertIntoDB(ctx, string(data))
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand Down Expand Up @@ -261,17 +258,15 @@ func (s *server) Delete(ctx context.Context, in *hardware.DeleteRequest) (*hardw

logger.With("id", in.Id).Info("data deleted")

var fn func() error
labels["op"] = "delete"
msg := "deleting into DB"
fn = func() error { return s.db.DeleteFromDB(ctx, in.Id) }
const msg = "deleting into DB"

metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
err := fn()
err := s.db.DeleteFromDB(ctx, in.Id)
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand Down
24 changes: 8 additions & 16 deletions grpc-server/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ func (s *server) CreateTemplate(ctx context.Context, in *template.WorkflowTempla
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

msg := ""
const msg = "creating a new Template"
labels["op"] = "createtemplate"
msg = "creating a new Template"
id, _ := uuid.NewUUID()
fn := func() error { return s.db.CreateTemplate(ctx, in.Name, in.Data, id) }

metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
err := fn()
err := s.db.CreateTemplate(ctx, in.Name, in.Data, id)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
l := logger
Expand All @@ -51,17 +49,15 @@ func (s *server) GetTemplate(ctx context.Context, in *template.GetRequest) (*tem
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

msg := ""
const msg = "getting a template"
labels["op"] = "get"
msg = "getting a template"

fn := func() (string, string, error) { return s.db.GetTemplate(ctx, in.Id) }
metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
n, d, err := fn()
n, d, err := s.db.GetTemplate(ctx, in.Id)
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand All @@ -81,17 +77,15 @@ func (s *server) DeleteTemplate(ctx context.Context, in *template.GetRequest) (*
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

msg := ""
const msg = "deleting a template"
labels["op"] = "delete"
msg = "deleting a template"
fn := func() error { return s.db.DeleteTemplate(ctx, in.Id) }

metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
err := fn()
err := s.db.DeleteTemplate(ctx, in.Id)
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand Down Expand Up @@ -142,17 +136,15 @@ func (s *server) UpdateTemplate(ctx context.Context, in *template.WorkflowTempla
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

msg := ""
const msg = "updating a template"
labels["op"] = "updatetemplate"
msg = "updating a template"
fn := func() error { return s.db.UpdateTemplate(ctx, in.Name, in.Data, uuid.MustParse(in.Id)) }

metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
err := fn()
err := s.db.UpdateTemplate(ctx, in.Name, in.Data, uuid.MustParse(in.Id))
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand Down
60 changes: 25 additions & 35 deletions grpc-server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,35 @@ func (s *server) CreateWorkflow(ctx context.Context, in *workflow.CreateRequest)
labels := prometheus.Labels{"method": "CreateWorkflow", "op": ""}
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()
msg := ""

const msg = "creating a new workflow"
labels["op"] = "createworkflow"
msg = "creating a new workflow"
id, err := uuid.NewUUID()
if err != nil {
return &workflow.CreateResponse{}, err
}
fn := func() error {
wf := db.Workflow{
ID: id.String(),
Template: in.Template,
Hardware: in.Hardware,
State: workflow.State_value[workflow.State_PENDING.String()],
}
data, err := createYaml(ctx, s.db, in.Template, in.Hardware)
if err != nil {
return errors.Wrap(err, "Failed to create Yaml")
}
err = s.db.CreateWorkflow(ctx, wf, data, id)
if err != nil {
return err
}
return nil
}

metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
err = fn()

data, err := createYaml(ctx, s.db, in.Template, in.Hardware)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
err = errors.Wrap(err, "failed to create Yaml")
logger.Error(err)
return &workflow.CreateResponse{}, err
}

wf := db.Workflow{
ID: id.String(),
Template: in.Template,
Hardware: in.Hardware,
State: workflow.State_value[workflow.State_PENDING.String()],
}
err = s.db.CreateWorkflow(ctx, wf, data, id)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
l := logger
Expand All @@ -70,6 +68,7 @@ func (s *server) CreateWorkflow(ctx context.Context, in *workflow.CreateRequest)
l.Error(err)
return &workflow.CreateResponse{}, err
}

l := logger.With("workflowID", id.String())
l.Info("done " + msg)
return &workflow.CreateResponse{Id: id.String()}, err
Expand All @@ -82,17 +81,15 @@ func (s *server) GetWorkflow(ctx context.Context, in *workflow.GetRequest) (*wor
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

msg := ""
const msg = "getting a workflow"
labels["op"] = "get"
msg = "getting a workflow"

fn := func() (db.Workflow, error) { return s.db.GetWorkflow(ctx, in.Id) }
metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
w, err := fn()
w, err := s.db.GetWorkflow(ctx, in.Id)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
l := logger
Expand Down Expand Up @@ -124,21 +121,16 @@ func (s *server) DeleteWorkflow(ctx context.Context, in *workflow.GetRequest) (*
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

msg := ""
const msg = "deleting a workflow"
labels["op"] = "delete"
l := logger.With("workflowID", in.GetId())
msg = "deleting a workflow"
fn := func() error {
// update only if not in running state
return s.db.DeleteWorkflow(ctx, in.Id, workflow.State_value[workflow.State_RUNNING.String()])
}

metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

l.Info(msg)
err := fn()
err := s.db.DeleteWorkflow(ctx, in.Id, workflow.State_value[workflow.State_RUNNING.String()])
if err != nil {
metrics.CacheErrors.With(labels).Inc()
l := logger
Expand Down Expand Up @@ -195,17 +187,15 @@ func (s *server) GetWorkflowContext(ctx context.Context, in *workflow.GetRequest
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

msg := ""
const msg = "getting a workflow"
labels["op"] = "get"
msg = "getting a workflow"

fn := func() (*workflowpb.WorkflowContext, error) { return s.db.GetWorkflowContexts(ctx, in.Id) }
metrics.CacheTotals.With(labels).Inc()
timer := prometheus.NewTimer(metrics.CacheDuration.With(labels))
defer timer.ObserveDuration()

logger.Info(msg)
w, err := fn()
w, err := s.db.GetWorkflowContexts(ctx, in.Id)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
l := logger
Expand Down
2 changes: 1 addition & 1 deletion http-server/http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/grpc-ecosystem/grpc-gateway/utilities"
"github.com/tinkerbell/tink/protos/hardware"
"github.com/tinkerbell/tink/pkg"
"github.com/tinkerbell/tink/protos/hardware"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down
1 change: 1 addition & 0 deletions protos/protoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ for proto in hardware packet template workflow; do
echo "Generating ${proto}.pb.go..."
protoc -I ./ -I ./common/ -I "$GOPATH"/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis "${proto}/${proto}.proto" --go_out=plugins=grpc:./ --grpc-gateway_out=logtostderr=true:.
done
goimports -w .
5 changes: 3 additions & 2 deletions protos/workflow/workflow.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.