Skip to content

Commit

Permalink
Get rid of unnecessary fn variable
Browse files Browse the repository at this point in the history
`fn` is only ever assigned to once and then just called. This is no
different than just calling the underlying functions, so lets do that.

Signed-off-by: Manuel Mendez <[email protected]>
  • Loading branch information
mmlb committed Sep 8, 2020
1 parent ce59e89 commit f5f8c2d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 41 deletions.
8 changes: 2 additions & 6 deletions grpc-server/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,20 @@ func (s *server) Push(ctx context.Context, in *hardware.PushRequest) (*hardware.
return &hardware.Empty{}, err
}

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

labels["op"] = "insert"
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 @@ -260,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"
const msg = "deleting into DB"
fn = func() error { return s.db.DeleteFromDB(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.DeleteFromDB(ctx, in.Id)
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand Down
12 changes: 4 additions & 8 deletions grpc-server/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ func (s *server) CreateTemplate(ctx context.Context, in *template.WorkflowTempla
const msg = "creating a new Template"
labels["op"] = "createtemplate"
id := uuid.NewV4()
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 @@ -53,13 +52,12 @@ func (s *server) GetTemplate(ctx context.Context, in *template.GetRequest) (*tem
const msg = "getting a template"
labels["op"] = "get"

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,14 +79,13 @@ func (s *server) DeleteTemplate(ctx context.Context, in *template.GetRequest) (*

const msg = "deleting a template"
labels["op"] = "delete"
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 @@ -141,14 +138,13 @@ func (s *server) UpdateTemplate(ctx context.Context, in *template.WorkflowTempla

const msg = "updating a template"
labels["op"] = "updatetemplate"
fn := func() error { return s.db.UpdateTemplate(ctx, in.Name, in.Data, uuid.FromStringOrNil(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.FromStringOrNil(in.Id))
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand Down
47 changes: 20 additions & 27 deletions grpc-server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,28 @@ func (s *server) CreateWorkflow(ctx context.Context, in *workflow.CreateRequest)
const msg = "creating a new workflow"
labels["op"] = "createworkflow"
id := uuid.NewV4()
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 @@ -67,6 +65,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,13 +81,12 @@ func (s *server) GetWorkflow(ctx context.Context, in *workflow.GetRequest) (*wor
const msg = "getting a workflow"
labels["op"] = "get"

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 @@ -123,17 +121,13 @@ func (s *server) DeleteWorkflow(ctx context.Context, in *workflow.GetRequest) (*
const msg = "deleting a workflow"
labels["op"] = "delete"
l := logger.With("workflowID", in.GetId())
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 @@ -193,13 +187,12 @@ func (s *server) GetWorkflowContext(ctx context.Context, in *workflow.GetRequest
const msg = "getting a workflow"
labels["op"] = "get"

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

0 comments on commit f5f8c2d

Please sign in to comment.