Skip to content

Commit

Permalink
移除 log,采用 g 包 log
Browse files Browse the repository at this point in the history
  • Loading branch information
camry committed Aug 8, 2022
1 parent b813128 commit 6ab652d
Show file tree
Hide file tree
Showing 35 changed files with 86 additions and 1,161 deletions.
4 changes: 2 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"syscall"
"time"

"github.com/camry/dove/log"
"github.com/camry/g/glog"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
)
Expand All @@ -33,7 +33,7 @@ func New(opts ...Option) *App {
o := &option{
ctx: context.Background(),
signals: []os.Signal{syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGHUP, syscall.SIGTERM},
logger: log.NewHelper(log.GetLogger()),
logger: glog.NewHelper(glog.GetLogger()),
stopTimeout: 10 * time.Second,
}
if id, err := uuid.NewUUID(); err == nil {
Expand Down
6 changes: 3 additions & 3 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package dove

import (
"github.com/camry/dove/server/gcron"
"github.com/camry/dove/server/gtcp"
"github.com/camry/dove/server/gudp"
"reflect"
"testing"
"time"

"github.com/camry/dove/server/gcron"
"github.com/camry/dove/server/ghttp"
"github.com/camry/dove/server/grpc"
"github.com/camry/dove/server/gtcp"
"github.com/camry/dove/server/gudp"
)

func TestNew(t *testing.T) {
Expand Down
11 changes: 6 additions & 5 deletions cron/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cron

import (
"fmt"
"github.com/camry/dove/log"
"runtime"
"sync"

"github.com/camry/g/glog"
)

// JobWrapper 用一些行为装饰指定的 Job。
Expand Down Expand Up @@ -35,7 +36,7 @@ func (c Chain) Then(j Job) Job {
}

// Recover 使用日志记录器,记录包装任务中的 panic。
func Recover(logger *log.Helper) JobWrapper {
func Recover(logger *glog.Helper) JobWrapper {
return func(j Job) Job {
return FuncJob(func() {
defer func() {
Expand All @@ -56,23 +57,23 @@ func Recover(logger *log.Helper) JobWrapper {
}

// DelayIfStillRunning 序列化作业,延迟后续运行,直到前一个完成。延迟超过一分钟后运行的作业会在信息中记录延迟。
func DelayIfStillRunning(logger *log.Helper) JobWrapper {
func DelayIfStillRunning(logger *glog.Helper) JobWrapper {
return func(j Job) Job {
var mu sync.Mutex
return FuncJob(func() {
// start := time.Now()
mu.Lock()
defer mu.Unlock()
// if dur := time.Since(start); dur > time.Minute {
// logger.Infow(log.DefaultMessageKey, "Cron", "action", "delay", "duration", dur)
// logger.Infow(glog.DefaultMessageKey, "Cron", "action", "delay", "duration", dur)
// }
j.Run()
})
}
}

// SkipIfStillRunning 如果先前的调用仍在运行,则跳过对 Job 的调用。它记录跳转到信息级别的给定记录器。
func SkipIfStillRunning(logger *log.Helper) JobWrapper {
func SkipIfStillRunning(logger *glog.Helper) JobWrapper {
return func(j Job) Job {
var ch = make(chan struct{}, 1)
ch <- struct{}{}
Expand Down
23 changes: 12 additions & 11 deletions cron/chain_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cron

import (
"github.com/camry/dove/log"
"reflect"
"sync"
"testing"
"time"

"github.com/camry/g/glog"
)

func appendingJob(slice *[]int, value int) Job {
Expand Down Expand Up @@ -56,13 +57,13 @@ func TestChainRecover(t *testing.T) {
})

t.Run("Recovering JobWrapper recovers", func(t *testing.T) {
NewChain(Recover(log.NewHelper(log.GetLogger()))).
NewChain(Recover(glog.NewHelper(glog.GetLogger()))).
Then(panickingJob).
Run()
})

t.Run("composed with the *IfStillRunning wrappers", func(t *testing.T) {
NewChain(Recover(log.NewHelper(log.GetLogger()))).
NewChain(Recover(glog.NewHelper(glog.GetLogger()))).
Then(panickingJob).
Run()
})
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestChainDelayIfStillRunning(t *testing.T) {

t.Run("runs immediately", func(t *testing.T) {
var j countJob
wrappedJob := NewChain(DelayIfStillRunning(log.NewHelper(log.GetLogger()))).Then(&j)
wrappedJob := NewChain(DelayIfStillRunning(glog.NewHelper(glog.GetLogger()))).Then(&j)
go wrappedJob.Run()
time.Sleep(2 * time.Millisecond) // Give the job 2ms to complete.
if c := j.Done(); c != 1 {
Expand All @@ -111,7 +112,7 @@ func TestChainDelayIfStillRunning(t *testing.T) {

t.Run("second run immediate if first done", func(t *testing.T) {
var j countJob
wrappedJob := NewChain(DelayIfStillRunning(log.NewHelper(log.GetLogger()))).Then(&j)
wrappedJob := NewChain(DelayIfStillRunning(glog.NewHelper(glog.GetLogger()))).Then(&j)
go func() {
go wrappedJob.Run()
time.Sleep(time.Millisecond)
Expand All @@ -126,7 +127,7 @@ func TestChainDelayIfStillRunning(t *testing.T) {
t.Run("second run delayed if first not done", func(t *testing.T) {
var j countJob
j.delay = 10 * time.Millisecond
wrappedJob := NewChain(DelayIfStillRunning(log.NewHelper(log.GetLogger()))).Then(&j)
wrappedJob := NewChain(DelayIfStillRunning(glog.NewHelper(glog.GetLogger()))).Then(&j)
go func() {
go wrappedJob.Run()
time.Sleep(time.Millisecond)
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestChainSkipIfStillRunning(t *testing.T) {

t.Run("runs immediately", func(t *testing.T) {
var j countJob
wrappedJob := NewChain(SkipIfStillRunning(log.NewHelper(log.GetLogger()))).Then(&j)
wrappedJob := NewChain(SkipIfStillRunning(glog.NewHelper(glog.GetLogger()))).Then(&j)
go wrappedJob.Run()
time.Sleep(2 * time.Millisecond) // Give the job 2ms to complete.
if c := j.Done(); c != 1 {
Expand All @@ -165,7 +166,7 @@ func TestChainSkipIfStillRunning(t *testing.T) {

t.Run("second run immediate if first done", func(t *testing.T) {
var j countJob
wrappedJob := NewChain(SkipIfStillRunning(log.NewHelper(log.GetLogger()))).Then(&j)
wrappedJob := NewChain(SkipIfStillRunning(glog.NewHelper(glog.GetLogger()))).Then(&j)
go func() {
go wrappedJob.Run()
time.Sleep(time.Millisecond)
Expand All @@ -180,7 +181,7 @@ func TestChainSkipIfStillRunning(t *testing.T) {
t.Run("second run skipped if first not done", func(t *testing.T) {
var j countJob
j.delay = 10 * time.Millisecond
wrappedJob := NewChain(SkipIfStillRunning(log.NewHelper(log.GetLogger()))).Then(&j)
wrappedJob := NewChain(SkipIfStillRunning(glog.NewHelper(glog.GetLogger()))).Then(&j)
go func() {
go wrappedJob.Run()
time.Sleep(time.Millisecond)
Expand All @@ -203,7 +204,7 @@ func TestChainSkipIfStillRunning(t *testing.T) {
t.Run("skip 10 jobs on rapid fire", func(t *testing.T) {
var j countJob
j.delay = 10 * time.Millisecond
wrappedJob := NewChain(SkipIfStillRunning(log.NewHelper(log.GetLogger()))).Then(&j)
wrappedJob := NewChain(SkipIfStillRunning(glog.NewHelper(glog.GetLogger()))).Then(&j)
for i := 0; i < 11; i++ {
go wrappedJob.Run()
}
Expand All @@ -218,7 +219,7 @@ func TestChainSkipIfStillRunning(t *testing.T) {
var j1, j2 countJob
j1.delay = 10 * time.Millisecond
j2.delay = 10 * time.Millisecond
chain := NewChain(SkipIfStillRunning(log.NewHelper(log.GetLogger())))
chain := NewChain(SkipIfStillRunning(glog.NewHelper(glog.GetLogger())))
wrappedJob1 := chain.Then(&j1)
wrappedJob2 := chain.Then(&j2)
for i := 0; i < 11; i++ {
Expand Down
17 changes: 9 additions & 8 deletions cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cron

import (
"context"
"github.com/camry/dove/log"
"sort"
"sync"
"time"

"github.com/camry/g/glog"
)

// Cron 跟踪任意数量的条目,调用调度指定的关联函数。
Expand All @@ -18,7 +19,7 @@ type Cron struct {
remove chan EntryID
snapshot chan chan []Entry
running bool
logger *log.Helper
logger *glog.Helper
runningMu sync.Mutex
location *time.Location
parser ScheduleParser
Expand Down Expand Up @@ -115,7 +116,7 @@ func New(opts ...Option) *Cron {
remove: make(chan EntryID),
running: false,
runningMu: sync.Mutex{},
logger: log.NewHelper(log.GetLogger()),
logger: glog.NewHelper(glog.GetLogger()),
location: time.Local,
parser: standardParser,
}
Expand Down Expand Up @@ -237,7 +238,7 @@ func (c *Cron) run() {
now := c.now()
for _, entry := range c.entries {
entry.Next = entry.Schedule.Next(now)
// c.logger.Infow(log.DefaultMessageKey, "Cron", "action", "schedule", "now", now, "entry", entry.ID, "next", entry.Next)
// c.logger.Infow(glog.DefaultMessageKey, "Cron", "action", "schedule", "now", now, "entry", entry.ID, "next", entry.Next)
}

for {
Expand All @@ -256,7 +257,7 @@ func (c *Cron) run() {
select {
case now = <-timer.C:
now = now.In(c.location)
// c.logger.Infow(log.DefaultMessageKey, "Cron", "action", "wake", "now", now)
// c.logger.Infow(glog.DefaultMessageKey, "Cron", "action", "wake", "now", now)

// 运行下一次小于现在的每个条目
for _, e := range c.entries {
Expand All @@ -266,15 +267,15 @@ func (c *Cron) run() {
c.startJob(e.WrappedJob)
e.Prev = e.Next
e.Next = e.Schedule.Next(now)
// c.logger.Infow(log.DefaultMessageKey, "Cron", "action", "run", "now", now, "entry", e.ID, "next", e.Next)
// c.logger.Infow(glog.DefaultMessageKey, "Cron", "action", "run", "now", now, "entry", e.ID, "next", e.Next)
}

case newEntry := <-c.add:
timer.Stop()
now = c.now()
newEntry.Next = newEntry.Schedule.Next(now)
c.entries = append(c.entries, newEntry)
// c.logger.Infow(log.DefaultMessageKey, "Cron", "action", "added", "now", now, "entry", newEntry.ID, "next", newEntry.Next)
// c.logger.Infow(glog.DefaultMessageKey, "Cron", "action", "added", "now", now, "entry", newEntry.ID, "next", newEntry.Next)

case replyChan := <-c.snapshot:
replyChan <- c.entrySnapshot()
Expand All @@ -289,7 +290,7 @@ func (c *Cron) run() {
timer.Stop()
now = c.now()
c.removeEntry(id)
// c.logger.Infow(log.DefaultMessageKey, "Cron", "action", "removed", "entry", id)
// c.logger.Infow(glog.DefaultMessageKey, "Cron", "action", "removed", "entry", id)
}

break
Expand Down
6 changes: 3 additions & 3 deletions cron/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

"github.com/camry/dove/log"
"github.com/camry/g/glog"
)

const OneSecond = 1*time.Second + 50*time.Millisecond
Expand All @@ -36,7 +36,7 @@ func (sw *syncWriter) String() string {
func TestFuncPanicRecovery(t *testing.T) {
var buf syncWriter
cron := New(WithParser(secondParser),
WithChain(Recover(log.NewHelper(log.NewStdLogger(&buf)))))
WithChain(Recover(glog.NewHelper(glog.NewStdLogger(&buf)))))
cron.Start()
defer cron.Stop(context.Background())
cron.AddFunc("* * * * * ?", func() {
Expand All @@ -63,7 +63,7 @@ func TestJobPanicRecovery(t *testing.T) {

var buf syncWriter
cron := New(WithParser(secondParser),
WithChain(Recover(log.NewHelper(log.NewStdLogger(&buf)))))
WithChain(Recover(glog.NewHelper(glog.NewStdLogger(&buf)))))
cron.Start()
defer cron.Stop(context.Background())
cron.AddJob("* * * * * ?", job)
Expand Down
4 changes: 2 additions & 2 deletions cron/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cron
import (
"time"

"github.com/camry/dove/log"
"github.com/camry/g/glog"
)

// Option 表示对 Cron 的默认行为的修改。
Expand Down Expand Up @@ -39,7 +39,7 @@ func WithChain(wrappers ...JobWrapper) Option {
}

// WithLogger 使用提供的日志记录器。
func WithLogger(logger *log.Helper) Option {
func WithLogger(logger *glog.Helper) Option {
return func(c *Cron) {
c.logger = logger
}
Expand Down
5 changes: 3 additions & 2 deletions cron/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cron

import (
"context"
"github.com/camry/dove/log"
"strings"
"testing"
"time"

"github.com/camry/g/glog"
)

func TestWithLocation(t *testing.T) {
Expand All @@ -25,7 +26,7 @@ func TestWithParser(t *testing.T) {

func TestWithVerboseLogger(t *testing.T) {
var buf syncWriter
logger := log.NewHelper(log.NewStdLogger(&buf))
logger := glog.NewHelper(glog.NewStdLogger(&buf))
c := New(WithLogger(logger))
if c.logger != logger {
t.Error("expected provided logger")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/camry/dove
go 1.18

require (
github.com/camry/g v1.0.0
github.com/google/uuid v1.3.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/grpc v1.46.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/camry/g v1.0.0 h1:AdJGU9rqnPWq28iQT+GYbK/5DdEWhhbMlqgWdXNsPDo=
github.com/camry/g v1.0.0/go.mod h1:byyA0GisLAi0JvBAwWrgPEXN5QRpMTvuLrNGLPRc6ak=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down
34 changes: 0 additions & 34 deletions log/README.md

This file was deleted.

Loading

0 comments on commit 6ab652d

Please sign in to comment.