bamzi / jobrunner

Framework for performing work asynchronously, outside of the request flow
MIT License
1.04k stars 99 forks source link

runtime error: invalid memory #31

Open RealLau opened 3 years ago

RealLau commented 3 years ago

I just encountered a runtime error, the logs is as below:

goroutine 1526 [running]:
log.(*Logger).Panic(0xc0005440f0, 0xc000467c00, 0x3, 0x3)
        /Users/smy/sdk/go1.13.8/src/log/log.go:212 +0xaa
github.com/bamzi/jobrunner.(*Job).Run.func1()
        /Users/smy/go/pkg/mod/github.com/bamzi/jobrunner@v1.0.0/jobrunner.go:55 +0x1a5
panic(0xe86880, 0x1804d70)
        /Users/smy/sdk/go1.13.8/src/runtime/panic.go:679 +0x1b2
main.UpdateNews.Run()
        /Users/smy/go/src/GBlog/main.go:29 +0x89
github.com/bamzi/jobrunner.(*Job).Run(0xc0000a7090)
        /Users/smy/go/pkg/mod/github.com/bamzi/jobrunner@v1.0.0/jobrunner.go:75 +0x188
github.com/robfig/cron/v3.(*Cron).startJob.func1(0xc000248b40, 0x10f8a80, 0xc0000a7090)
        /Users/smy/go/pkg/mod/github.com/robfig/cron/v3@v3.0.0/cron.go:307 +0x69
created by github.com/robfig/cron/v3.(*Cron).startJob
        /Users/smy/go/pkg/mod/github.com/robfig/cron/v3@v3.0.0/cron.go:305 +0x73
^[[97;45m[JobRunner] 2020/09/07 - 09:14:54 Started... ^[[0m

my code is simple as send a http get request in a every 5min job:

func (e UpdateNews) Run() {
    resp, err := http.Get("https://sample_site.com/common/update_news/")
    if err != nil {
        fmt.Println("Call update news failed")
    }
    var res map[string]interface{}
    json.NewDecoder(resp.Body).Decode(&res)
    resCode := fmt.Sprintf("%v", res["code"])
    if resCode == "0" {
        fmt.Printf("Every 5 mins update news success.")
    }
}
func main(){
jobrunner.Start() // optional: jobrunner.Start(pool int, concurrent int) (10, 1)
    err := jobrunner.Schedule("@every 300s", UpdateNews{})
    if err != nil {
        panic(err)
    }
}
bamzi commented 3 years ago

You need to declare UpdateNews{}

type UpdateNews struct {}

Also the error response from jobrunner.Schedule... is related to parsing of cron. Maybe 300s should 5m