Skip to content

Commit

Permalink
Allow replacing the logger to facilitate those who wish to use no log…
Browse files Browse the repository at this point in the history
…ging or non-standard logging
  • Loading branch information
koesie10 committed Feb 2, 2018
1 parent afe3b64 commit 4ceef9b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
59 changes: 59 additions & 0 deletions log.go
@@ -0,0 +1,59 @@
package faktory_worker

import (
"log"
"os"
)

type Logger interface {
Debug(v ...interface{})
Debugf(format string, args ...interface{})
Info(v ...interface{})
Infof(format string, args ...interface{})
Warn(v ...interface{})
Warnf(format string, args ...interface{})
Error(v ...interface{})
Errorf(format string, args ...interface{})
Fatal(v ...interface{})
Fatalf(format string, args ...interface{})
}

type StdLogger struct {
*log.Logger
}

func NewStdLogger() Logger {
return &StdLogger{log.New(os.Stdout, "Faktory ", log.LstdFlags)}
}

func (l *StdLogger) Debug(v ...interface{}) {
l.Println(v...)
}

func (l *StdLogger) Debugf(format string, v ...interface{}) {
l.Printf(format + "\n", v...)
}

func (l *StdLogger) Error(v ...interface{}) {
l.Println(v...)
}

func (l *StdLogger) Errorf(format string, v ...interface{}) {
l.Printf(format + "\n", v...)
}

func (l *StdLogger) Info(v ...interface{}) {
l.Println(v...)
}

func (l *StdLogger) Infof(format string, v ...interface{}) {
l.Printf(format + "\n", v...)
}

func (l *StdLogger) Warn(v ...interface{}) {
l.Println(v...)
}

func (l *StdLogger) Warnf(format string, v ...interface{}) {
l.Printf(format + "\n", v...)
}
11 changes: 6 additions & 5 deletions runner.go
Expand Up @@ -3,7 +3,6 @@ package faktory_worker
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"strconv"
Expand Down Expand Up @@ -35,6 +34,7 @@ type Manager struct {
Concurrency int
Queues []string
Pool
Logger Logger

quiet bool
// The done channel will always block unless
Expand All @@ -54,20 +54,20 @@ func (mgr *Manager) On(event eventType, fn func()) {
// After calling Quiet(), no more jobs will be pulled
// from Faktory by this process.
func (mgr *Manager) Quiet() {
log.Println("Quieting...")
mgr.Logger.Info("Quieting...")
mgr.quiet = true
mgr.fireEvent(Quiet)
}

// Terminate signals that the various components should shutdown.
// Blocks on the shutdownWaiter until all components have finished.
func (mgr *Manager) Terminate() {
log.Println("Shutting down...")
mgr.Logger.Info("Shutting down...")
close(mgr.done)
mgr.fireEvent(Shutdown)
mgr.shutdownWaiter.Wait()
mgr.Pool.Close()
log.Println("Goodbye")
mgr.Logger.Info("Goodbye")
os.Exit(0)
}

Expand All @@ -76,6 +76,7 @@ func NewManager() *Manager {
return &Manager{
Concurrency: 20,
Queues: []string{"default"},
Logger: NewStdLogger(),

done: make(chan interface{}),
shutdownWaiter: &sync.WaitGroup{},
Expand Down Expand Up @@ -183,7 +184,7 @@ func process(mgr *Manager, idx int) {
})

if err != nil {
log.Println(err)
mgr.Logger.Error(err)
time.Sleep(1 * time.Second)
continue
}
Expand Down

0 comments on commit 4ceef9b

Please sign in to comment.