Skip to content
Open
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
183 changes: 183 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,189 @@ func JobHtml(c *gin.Context) {
}

```
## Custom Logger
If you use your own or customized logger package, you can integrate your logger package by following the steps below.

```go
package main

import (
"fmt"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
"go.uber.org/zap"
"log"
"os"
"strings"
"testing"
"time"
)

type CustomJob struct {}

func (j CustomJob) Run() {
fmt.Println("Custom job run with default logger")
}

type CustomLogger struct {
cron.Logger
Log *log.Logger
}

func (l CustomLogger) Info(format string, keysAndValues ...interface{}) {
l.Log.Printf(format, keysAndValues...)
}

func (l CustomLogger) Error(err error, format string, keysAndValues ...interface{}) {
l.Log.Println(err)
l.Log.Printf(format, keysAndValues...)
}

func logger1() {
now := time.Now().UTC()
defaultLogger := log.New(os.Stderr, now.String(), 1)

logger := CustomLogger{Log: defaultLogger}

StartWithLogger(logger)
Schedule("@every 1s", CustomJob{})

ch := make(chan bool)

time.AfterFunc(5 * time.Second, func() {
ch<-true
})

<-ch
}

type CustomJob2 struct {}

func (j CustomJob2) Run() {
fmt.Println("Custom job run with zerolog.Logger")
}

type CustomLogger2 struct {
cron.Logger
Log *zerolog.Logger
}

func (l CustomLogger2) Info(format string, keysAndValues ...interface{}) {
l.Log.Info().Msgf(format, keysAndValues...)
}

func (l CustomLogger2) Error(err error, format string, keysAndValues ...interface{}) {
l.Log.Err(err).Msgf(format, keysAndValues...)
}

func logger2() {
zeroLogger := zerolog.New(os.Stderr).With().Timestamp().Logger()

logger := CustomLogger2{Log: &zeroLogger}

StartWithLogger(logger)
Schedule("@every 1s", CustomJob2{})

ch := make(chan bool)

time.AfterFunc(5 * time.Second, func() {
ch<-true
})

<-ch
}

type CustomJob3 struct {}

func (j CustomJob3) Run() {
fmt.Println("Custom job run with zap.Logger")
}

type CustomLogger3 struct {
cron.Logger
Log *zap.Logger
}

func (l CustomLogger3) Info(format string, keysAndValues ...interface{}) {
l.Log.Sugar().Infow(format, keysAndValues...)
}

func (l CustomLogger3) Error(err error, format string, keysAndValues ...interface{}) {
l.Log.Sugar().Errorw(strings.Join([]string{err.Error(), format}, ""), keysAndValues...)
}

func logger3() {
zapLogger, _ := zap.NewDevelopment()

logger := CustomLogger3{Log: zapLogger}

StartWithLogger(logger)
Schedule("@every 1s", CustomJob3{})

ch := make(chan bool)

time.AfterFunc(5 * time.Second, func() {
ch<-true
})

<-ch
}

func main() {
logger1()
logger2()
logger3()
}
```
Log Output
```
// default Logger
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 JobRunner Started
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 start
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 added%!(EXTRA string=now, time.Time=2019-12-12 14:42:00.871014911 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-12 14:42:01 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 wake%!(EXTRA string=now, time.Time=2019-12-12 14:42:01.000175995 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 run%!(EXTRA string=now, time.Time=2019-12-12 14:42:01.000175995 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-12 14:42:02 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 wake%!(EXTRA string=now, time.Time=2019-12-12 14:42:02.000453377 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 run%!(EXTRA string=now, time.Time=2019-12-12 14:42:02.000453377 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-12 14:42:03 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 wake%!(EXTRA string=now, time.Time=2019-12-12 14:42:03.000297283 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 run%!(EXTRA string=now, time.Time=2019-12-12 14:42:03.000297283 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-12 14:42:04 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 wake%!(EXTRA string=now, time.Time=2019-12-12 14:42:04.000363516 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 run%!(EXTRA string=now, time.Time=2019-12-12 14:42:04.000363516 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-12 14:42:05 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 wake%!(EXTRA string=now, time.Time=2019-12-12 14:42:05.00030873 +0300 +03)
2019-12-12 11:42:00.870926568 +0000 UTC2019/12/12 run%!(EXTRA string=now, time.Time=2019-12-12 14:42:05.00030873 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-12 14:42:06 +0300 +03)

// zerolog.Logger
{"level":"info","time":"2019-12-11T19:19:11+03:00","message":"JobRunner Started"}
{"level":"info","time":"2019-12-11T19:19:11+03:00","message":"start"}
{"level":"info","time":"2019-12-11T19:19:11+03:00","message":"added%!(EXTRA string=now, time.Time=2019-12-11 19:19:11.539557059 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:12 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:12+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:12.000175121 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:12+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:12.000175121 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:13 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:13+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:13.000324711 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:13+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:13.000324711 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:14 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:14+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:14.000597951 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:14+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:14.000597951 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:15 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:15+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:15.0004445 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:15+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:15.0004445 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:16 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:16+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:16.000527496 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:16+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:16.000527496 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:17 +0300 +03)"}

// zap.Logger
2019-12-12T14:42:36.135+0300 INFO jobrunner/init_test.go:101 JobRunner Started
2019-12-12T14:42:36.135+0300 INFO jobrunner/init_test.go:101 start
2019-12-12T14:42:36.136+0300 INFO jobrunner/init_test.go:101 added {"now": "2019-12-12T14:42:36.136+0300", "entry": 1, "next": "2019-12-12T14:42:37.000+0300"}
2019-12-12T14:42:37.000+0300 INFO jobrunner/init_test.go:101 wake {"now": "2019-12-12T14:42:37.000+0300"}
2019-12-12T14:42:37.000+0300 INFO jobrunner/init_test.go:101 run {"now": "2019-12-12T14:42:37.000+0300", "entry": 1, "next": "2019-12-12T14:42:38.000+0300"}
2019-12-12T14:42:38.000+0300 INFO jobrunner/init_test.go:101 wake {"now": "2019-12-12T14:42:38.000+0300"}
2019-12-12T14:42:38.000+0300 INFO jobrunner/init_test.go:101 run {"now": "2019-12-12T14:42:38.000+0300", "entry": 1, "next": "2019-12-12T14:42:39.000+0300"}
2019-12-12T14:42:39.000+0300 INFO jobrunner/init_test.go:101 wake {"now": "2019-12-12T14:42:39.000+0300"}
2019-12-12T14:42:39.000+0300 INFO jobrunner/init_test.go:101 run {"now": "2019-12-12T14:42:39.000+0300", "entry": 1, "next": "2019-12-12T14:42:40.000+0300"}
2019-12-12T14:42:40.000+0300 INFO jobrunner/init_test.go:101 wake {"now": "2019-12-12T14:42:40.000+0300"}
2019-12-12T14:42:40.000+0300 INFO jobrunner/init_test.go:101 run {"now": "2019-12-12T14:42:40.000+0300", "entry": 1, "next": "2019-12-12T14:42:41.000+0300"}
2019-12-12T14:42:41.000+0300 INFO jobrunner/init_test.go:101 wake {"now": "2019-12-12T14:42:41.000+0300"}
2019-12-12T14:42:41.000+0300 INFO jobrunner/init_test.go:101 run {"now": "2019-12-12T14:42:41.000+0300", "entry": 1, "next": "2019-12-12T14:42:42.000+0300"}

```

## WHY?
To reduce our http response latency by 200+%

Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/bamzi/jobrunner

go 1.13

require github.com/robfig/cron/v3 v3.0.0
require (
github.com/robfig/cron/v3 v3.0.0
github.com/rs/zerolog v1.17.2
go.uber.org/zap v1.13.0
)
53 changes: 53 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,55 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.17.2 h1:RMRHFw2+wF7LO0QqtELQwo8hqSmqISyCJeFeAAuWcRo=
github.com/rs/zerolog v1.17.2/go.mod h1:9nvC1axdVrAHcu/s9taAVfBuIdTZLVQmKQyvrUjF5+I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
go.uber.org/atomic v1.5.0 h1:OI5t8sDa1Or+q8AeE+yKeB/SDYioSHAgcVljj9JIETY=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.3.0 h1:sFPn2GLc3poCkfrpIXGhBD2X0CMIo4Q/zSULXrj/+uc=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.13.0 h1:nR6NoDBgAf67s68NhaXbsojM+2gxp3S1hWkHDl27pVU=
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
14 changes: 13 additions & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ func isSelfConcurrent(cocnurrencyFlag int) {
}
}

func StartWithLogger(logger cron.Logger, v ...int) {

MainCron = cron.New(cron.WithLogger(logger))

for i,option := range v {
functions[i].(func(int))(option)
}

MainCron.Start()

logger.Info("JobRunner Started")
}

func Start(v ...int) {
MainCron = cron.New()

Expand All @@ -56,5 +69,4 @@ func Start(v ...int) {

fmt.Printf("%s[JobRunner] %v Started... %s \n",
magenta, time.Now().Format("2006/01/02 - 15:04:05"), reset)

}
Loading