empact/task/task.go

41 lines
825 B
Go
Raw Normal View History

2015-03-05 02:08:36 +07:00
package task
import (
2015-03-06 20:23:01 +07:00
"log"
"time"
2015-03-05 02:08:36 +07:00
"code.google.com/p/goauth2/oauth"
"github.com/google/go-github/github"
2015-03-05 15:07:18 +07:00
"github.com/localhots/empact/db"
2015-03-05 02:08:36 +07:00
)
func newGithubClient(token string) *github.Client {
trans := &oauth.Transport{
Token: &oauth.Token{AccessToken: token},
}
return github.NewClient(trans.Client())
}
2015-03-05 02:15:05 +07:00
2015-03-05 03:14:14 +07:00
func saveResponseMeta(token string, res *github.Response) {
if res == nil {
return
}
2015-03-05 22:04:44 +07:00
tok := &db.Token{
2015-03-05 03:14:14 +07:00
Token: token,
2015-03-06 18:18:15 +07:00
Quota: uint64(res.Limit),
Remaining: uint64(res.Remaining),
2015-03-05 03:14:14 +07:00
ResetAt: res.Reset.Time,
2015-03-05 22:04:44 +07:00
}
tok.Save()
2015-03-05 03:14:14 +07:00
}
2015-03-06 20:23:01 +07:00
func report(task string, start time.Time) {
duration := time.Since(start).Nanoseconds()
2015-03-06 20:35:13 +07:00
outcome := "succeeded"
2015-03-06 20:23:01 +07:00
if err := recover(); err != nil {
outcome = "failed"
}
log.Printf("Task %s %s; time: %d (%dms)\n", task, outcome, duration, duration/1000000)
}