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