Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Refactors the schedule package logger for scheduler.go and task.go. A…
Browse files Browse the repository at this point in the history
…llows for better resuse.
  • Loading branch information
lynxbat committed Mar 6, 2016
1 parent abe8e0e commit f6e58df
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
40 changes: 22 additions & 18 deletions scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ import (
)

var (
// logger for the scheduler
schedulerLogger = log.WithFields(log.Fields{
"_module": "scheduler",
})

// HandlerRegistrationName registers a handler with the event manager
HandlerRegistrationName = "scheduler"

Expand Down Expand Up @@ -93,7 +98,6 @@ type scheduler struct {
metricManager managesMetrics
tasks *taskCollection
state schedulerState
logger *log.Entry
eventManager *gomit.EventController
taskWatcherColl *taskWatcherCollection
}
Expand All @@ -108,9 +112,9 @@ type managesWork interface {
func New(opts ...workManagerOption) *scheduler {
s := &scheduler{
tasks: newTaskCollection(),
logger: log.WithFields(log.Fields{
"_module": "scheduler",
}),
// logger: log.WithFields(log.Fields{
// "_module": "scheduler",
// }),
eventManager: gomit.NewEventController(),
taskWatcherColl: newTaskWatcherCollection(),
}
Expand Down Expand Up @@ -150,7 +154,7 @@ func (s *scheduler) CreateTaskTribe(sch schedule.Schedule, wfMap *wmap.WorkflowM
}

func (s *scheduler) createTask(sch schedule.Schedule, wfMap *wmap.WorkflowMap, startOnCreate bool, source string, opts ...core.TaskOption) (core.Task, core.TaskErrors) {
logger := s.logger.WithFields(log.Fields{
logger := schedulerLogger.WithFields(log.Fields{
"_block": "create-task",
"source": source,
})
Expand Down Expand Up @@ -244,7 +248,7 @@ func (s *scheduler) RemoveTaskTribe(id string) error {
}

func (s *scheduler) removeTask(id, source string) error {
logger := s.logger.WithFields(log.Fields{
logger := schedulerLogger.WithFields(log.Fields{
"_block": "remove-task",
"source": source,
})
Expand Down Expand Up @@ -276,7 +280,7 @@ func (s *scheduler) GetTasks() map[string]core.Task {
func (s *scheduler) GetTask(id string) (core.Task, error) {
t, err := s.getTask(id)
if err != nil {
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "get-task",
"_error": ErrTaskNotFound,
"task-id": id,
Expand All @@ -296,13 +300,13 @@ func (s *scheduler) StartTaskTribe(id string) []serror.SnapError {
}

func (s *scheduler) startTask(id, source string) []serror.SnapError {
logger := s.logger.WithFields(log.Fields{
logger := schedulerLogger.WithFields(log.Fields{
"_block": "start-task",
"source": source,
})
t, err := s.getTask(id)
if err != nil {
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "start-task",
"_error": ErrTaskNotFound,
"task-id": id,
Expand Down Expand Up @@ -367,7 +371,7 @@ func (s *scheduler) StopTaskTribe(id string) []serror.SnapError {
}

func (s *scheduler) stopTask(id, source string) []serror.SnapError {
logger := s.logger.WithFields(log.Fields{
logger := schedulerLogger.WithFields(log.Fields{
"_block": "stop-task",
"source": source,
})
Expand Down Expand Up @@ -416,7 +420,7 @@ func (s *scheduler) stopTask(id, source string) []serror.SnapError {
func (s *scheduler) EnableTask(id string) (core.Task, error) {
t, e := s.getTask(id)
if e != nil {
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "enable-task",
"_error": ErrTaskNotFound,
"task-id": id,
Expand All @@ -426,14 +430,14 @@ func (s *scheduler) EnableTask(id string) (core.Task, error) {

err := t.Enable()
if err != nil {
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "enable-task",
"_error": err.Error(),
"task-id": id,
}).Error("error enabling task")
return nil, err
}
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "enable-task",
"task-id": t.ID(),
"task-state": t.State(),
Expand All @@ -444,14 +448,14 @@ func (s *scheduler) EnableTask(id string) (core.Task, error) {
// Start starts the scheduler
func (s *scheduler) Start() error {
if s.metricManager == nil {
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "start-scheduler",
"_error": ErrMetricManagerNotSet.Error(),
}).Error("error on scheduler start")
return ErrMetricManagerNotSet
}
s.state = schedulerStarted
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "start-scheduler",
}).Info("scheduler started")
return nil
Expand All @@ -464,15 +468,15 @@ func (s *scheduler) Stop() {
// Kill ensure another task can't turn it back on while we are shutting down
t.Kill()
}
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "stop-scheduler",
}).Info("scheduler stopped")
}

// Set metricManager for scheduler
func (s *scheduler) SetMetricManager(mm managesMetrics) {
s.metricManager = mm
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "set-metric-manager",
}).Debug("metric manager linked")
}
Expand All @@ -481,7 +485,7 @@ func (s *scheduler) SetMetricManager(mm managesMetrics) {
func (s *scheduler) WatchTask(id string, tw core.TaskWatcherHandler) (core.TaskWatcherCloser, error) {
task, err := s.getTask(id)
if err != nil {
s.logger.WithFields(log.Fields{
schedulerLogger.WithFields(log.Fields{
"_block": "watch-task",
"_error": ErrTaskNotFound,
"task-id": id,
Expand Down
14 changes: 7 additions & 7 deletions scheduler/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const (
)

var (
schedulerLogger = log.WithField("_module", "scheduler-task")
taskLogger = schedulerLogger.WithField("_module", "scheduler-task")

// ErrTaskNotFound - The error message for task not found
ErrTaskNotFound = errors.New("Task not found")
Expand Down Expand Up @@ -256,7 +256,7 @@ func (t *task) Schedule() schedule.Schedule {
func (t *task) spin() {
var consecutiveFailures uint
for {
schedulerLogger.Debug("task spin loop")
taskLogger.Debug("task spin loop")
// Start go routine to wait on schedule
go t.waitForSchedule()
// wait here on
Expand All @@ -273,7 +273,7 @@ func (t *task) spin() {
t.fire()
if t.lastFailureTime == t.lastFireTime {
consecutiveFailures++
schedulerLogger.WithFields(log.Fields{
taskLogger.WithFields(log.Fields{
"_block": "spin",
"task-id": t.id,
"task-name": t.name,
Expand All @@ -285,7 +285,7 @@ func (t *task) spin() {
consecutiveFailures = 0
}
if consecutiveFailures >= t.stopOnFailure {
schedulerLogger.WithFields(log.Fields{
taskLogger.WithFields(log.Fields{
"_block": "spin",
"task-id": t.id,
"task-name": t.name,
Expand Down Expand Up @@ -393,7 +393,7 @@ func (t *taskCollection) add(task *task) error {
//If we don't already have this task in the collection save it
t.table[task.id] = task
} else {
schedulerLogger.WithFields(log.Fields{
taskLogger.WithFields(log.Fields{
"_module": "scheduler-taskCollection",
"_block": "add",
"task id": task.id,
Expand All @@ -411,15 +411,15 @@ func (t *taskCollection) remove(task *task) error {
defer t.Unlock()
if _, ok := t.table[task.id]; ok {
if task.state != core.TaskStopped {
schedulerLogger.WithFields(log.Fields{
taskLogger.WithFields(log.Fields{
"_block": "remove",
"task id": task.id,
}).Error(ErrTaskNotStopped)
return ErrTaskNotStopped
}
delete(t.table, task.id)
} else {
schedulerLogger.WithFields(log.Fields{
taskLogger.WithFields(log.Fields{
"_block": "remove",
"task id": task.id,
}).Error(ErrTaskNotFound)
Expand Down

0 comments on commit f6e58df

Please sign in to comment.