1
0
Fork 0

Better task logging

This commit is contained in:
Gregory Eremin 2015-03-21 21:19:17 +07:00
parent 8e779f76ec
commit c0c5de5a44
4 changed files with 27 additions and 20 deletions

View File

@ -15,7 +15,7 @@ import (
)
func Authenticate(code string) (token, login string, err error) {
defer report("Authenticate", time.Now())
defer report(time.Now(), "Authenticate")
if token, err = FetchAccessToken(code); err != nil {
return
}
@ -40,7 +40,7 @@ func Authenticate(code string) (token, login string, err error) {
}
func FetchAccessToken(code string) (token string, err error) {
defer report("FetchAccessToken", time.Now())
defer report(time.Now(), "FetchAccessToken")
payload := url.Values{}
payload.Set("client_id", config.C().ClientID)
payload.Set("client_secret", config.C().ClientSecret)
@ -68,7 +68,7 @@ func FetchAccessToken(code string) (token string, err error) {
}
func FetchUserInfoWithToken(token string) (u *db.User, err error) {
defer report("FetchUserInfoWithToken", time.Now())
defer report(time.Now(), "FetchUserInfoWithToken")
var resp *http.Response
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
return

View File

@ -7,14 +7,14 @@ import (
"github.com/localhots/empact/db"
)
func SyncRepos(token, owner string) {
defer report("SyncRepos", time.Now())
func SyncOrgRepos(token string, org *db.Org) {
defer report(time.Now(), "SyncOrgRepos (%s)", org.Login)
client := newGithubClient(token)
opt := &github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{PerPage: 100}}
for {
opt.Page++
repos, resp, err := client.Repositories.ListByOrg(owner, opt)
repos, resp, err := client.Repositories.ListByOrg(org.Login, opt)
saveResponseMeta(token, resp)
if err != nil {
panic(err)
@ -24,13 +24,16 @@ func SyncRepos(token, owner string) {
if repo.Description != nil {
descr = *repo.Description
}
// pretty.Println(repo)
r := &db.Repo{
OrgID: *repo.Organization.ID,
ID: *repo.ID,
OrgID: *repo.Owner.ID,
Name: *repo.Name,
Description: descr,
IsPrivate: *repo.Private,
IsFork: *repo.Fork,
}
go SyncContrib(token, org.Login, r)
db.Queue(func() { r.Save() })
}
if opt.Page >= resp.LastPage {
@ -40,7 +43,7 @@ func SyncRepos(token, owner string) {
}
func SyncContrib(token, owner string, repo *db.Repo) {
defer report("SyncContrib", time.Now())
defer report(time.Now(), "SyncContrib (%s/%s)", owner, repo.Name)
client := newGithubClient(token)
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo.Name)
@ -73,7 +76,7 @@ func SyncContrib(token, owner string, repo *db.Repo) {
}
func SyncUserOrgs(token string) (err error) {
defer report("SyncUserOrgs", time.Now())
defer report(time.Now(), "SyncUserOrgs")
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
@ -102,6 +105,7 @@ func SyncUserOrgs(token string) (err error) {
}
go SyncOrgTeams(token, o)
go SyncOrgMembers(token, o)
go SyncOrgRepos(token, o)
db.Queue(func() { o.Save() })
}
if opt.Page >= resp.LastPage {
@ -113,7 +117,7 @@ func SyncUserOrgs(token string) (err error) {
}
func SyncOrgTeams(token string, org *db.Org) (err error) {
defer report("SyncOrgTeams", time.Now())
defer report(time.Now(), "SyncOrgTeams (%s)", org.Login)
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
@ -147,7 +151,7 @@ func SyncOrgTeams(token string, org *db.Org) (err error) {
}
func SyncOrgMembers(token string, org *db.Org) (err error) {
defer report("SyncOrgMembers", time.Now())
defer report(time.Now(), "SyncOrgMembers (%s)", org.Login)
client := newGithubClient(token)
opt := &github.ListMembersOptions{ListOptions: github.ListOptions{PerPage: 100}}
@ -175,7 +179,7 @@ func SyncOrgMembers(token string, org *db.Org) (err error) {
}
func SyncTeamMembers(token string, team *db.Team) (err error) {
defer report("SyncTeamMembers", time.Now())
defer report(time.Now(), "SyncTeamMembers (%d/%s)", team.OrgID, team.Name)
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
@ -202,7 +206,7 @@ func SyncTeamMembers(token string, team *db.Team) (err error) {
}
func SyncTeamRepos(token string, team *db.Team) (err error) {
defer report("SyncTeamRepos", time.Now())
defer report(time.Now(), "SyncTeamRepos (%d/%s)", team.OrgID, team.Name)
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}

View File

@ -1,6 +1,7 @@
package task
import (
"fmt"
"log"
"time"
@ -29,12 +30,15 @@ func saveResponseMeta(token string, res *github.Response) {
db.Queue(func() { tok.Save() })
}
func report(task string, start time.Time) {
func report(start time.Time, format string, args ...interface{}) {
duration := time.Since(start).Nanoseconds()
outcome := "succeeded"
if err := recover(); err != nil {
outcome = "failed"
}
// if err := recover(); err != nil {
// log.Println(err)
// outcome = "failed"
// }
task := fmt.Sprintf(format, args...)
log.Printf("Task %s %s; time: %d (%dms)\n", task, outcome, duration, duration/1000000)
}

View File

@ -8,8 +8,7 @@ import (
)
func FetchUserInfo(token, login string) (u *db.User, err error) {
defer report("FetchUserInfo", time.Now())
defer report(time.Now(), "FetchUserInfo (%s)", login)
client := newGithubClient(token)
var user *github.User
var resp *github.Response
@ -39,7 +38,7 @@ func FetchUserInfo(token, login string) (u *db.User, err error) {
}
func SyncUserInfo(token, login string) (err error) {
defer report("SyncUserInfo", time.Now())
defer report(time.Now(), "SyncUserInfo (%s)", login)
var u *db.User
if u, err = FetchUserInfo(token, login); err == nil {
db.Queue(func() { u.Save() })