Better task logging
This commit is contained in:
parent
8e779f76ec
commit
c0c5de5a44
|
@ -15,7 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Authenticate(code string) (token, login string, err error) {
|
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 {
|
if token, err = FetchAccessToken(code); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ func Authenticate(code string) (token, login string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchAccessToken(code string) (token 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 := url.Values{}
|
||||||
payload.Set("client_id", config.C().ClientID)
|
payload.Set("client_id", config.C().ClientID)
|
||||||
payload.Set("client_secret", config.C().ClientSecret)
|
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) {
|
func FetchUserInfoWithToken(token string) (u *db.User, err error) {
|
||||||
defer report("FetchUserInfoWithToken", time.Now())
|
defer report(time.Now(), "FetchUserInfoWithToken")
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
|
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
|
||||||
return
|
return
|
||||||
|
|
24
task/sync.go
24
task/sync.go
|
@ -7,14 +7,14 @@ import (
|
||||||
"github.com/localhots/empact/db"
|
"github.com/localhots/empact/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SyncRepos(token, owner string) {
|
func SyncOrgRepos(token string, org *db.Org) {
|
||||||
defer report("SyncRepos", time.Now())
|
defer report(time.Now(), "SyncOrgRepos (%s)", org.Login)
|
||||||
client := newGithubClient(token)
|
client := newGithubClient(token)
|
||||||
opt := &github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{PerPage: 100}}
|
opt := &github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{PerPage: 100}}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
opt.Page++
|
opt.Page++
|
||||||
repos, resp, err := client.Repositories.ListByOrg(owner, opt)
|
repos, resp, err := client.Repositories.ListByOrg(org.Login, opt)
|
||||||
saveResponseMeta(token, resp)
|
saveResponseMeta(token, resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -24,13 +24,16 @@ func SyncRepos(token, owner string) {
|
||||||
if repo.Description != nil {
|
if repo.Description != nil {
|
||||||
descr = *repo.Description
|
descr = *repo.Description
|
||||||
}
|
}
|
||||||
|
// pretty.Println(repo)
|
||||||
r := &db.Repo{
|
r := &db.Repo{
|
||||||
OrgID: *repo.Organization.ID,
|
ID: *repo.ID,
|
||||||
|
OrgID: *repo.Owner.ID,
|
||||||
Name: *repo.Name,
|
Name: *repo.Name,
|
||||||
Description: descr,
|
Description: descr,
|
||||||
IsPrivate: *repo.Private,
|
IsPrivate: *repo.Private,
|
||||||
IsFork: *repo.Fork,
|
IsFork: *repo.Fork,
|
||||||
}
|
}
|
||||||
|
go SyncContrib(token, org.Login, r)
|
||||||
db.Queue(func() { r.Save() })
|
db.Queue(func() { r.Save() })
|
||||||
}
|
}
|
||||||
if opt.Page >= resp.LastPage {
|
if opt.Page >= resp.LastPage {
|
||||||
|
@ -40,7 +43,7 @@ func SyncRepos(token, owner string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SyncContrib(token, owner string, repo *db.Repo) {
|
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)
|
client := newGithubClient(token)
|
||||||
|
|
||||||
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo.Name)
|
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) {
|
func SyncUserOrgs(token string) (err error) {
|
||||||
defer report("SyncUserOrgs", time.Now())
|
defer report(time.Now(), "SyncUserOrgs")
|
||||||
client := newGithubClient(token)
|
client := newGithubClient(token)
|
||||||
opt := &github.ListOptions{PerPage: 100}
|
opt := &github.ListOptions{PerPage: 100}
|
||||||
|
|
||||||
|
@ -102,6 +105,7 @@ func SyncUserOrgs(token string) (err error) {
|
||||||
}
|
}
|
||||||
go SyncOrgTeams(token, o)
|
go SyncOrgTeams(token, o)
|
||||||
go SyncOrgMembers(token, o)
|
go SyncOrgMembers(token, o)
|
||||||
|
go SyncOrgRepos(token, o)
|
||||||
db.Queue(func() { o.Save() })
|
db.Queue(func() { o.Save() })
|
||||||
}
|
}
|
||||||
if opt.Page >= resp.LastPage {
|
if opt.Page >= resp.LastPage {
|
||||||
|
@ -113,7 +117,7 @@ func SyncUserOrgs(token string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SyncOrgTeams(token string, org *db.Org) (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)
|
client := newGithubClient(token)
|
||||||
opt := &github.ListOptions{PerPage: 100}
|
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) {
|
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)
|
client := newGithubClient(token)
|
||||||
opt := &github.ListMembersOptions{ListOptions: github.ListOptions{PerPage: 100}}
|
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) {
|
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)
|
client := newGithubClient(token)
|
||||||
opt := &github.ListOptions{PerPage: 100}
|
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) {
|
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)
|
client := newGithubClient(token)
|
||||||
opt := &github.ListOptions{PerPage: 100}
|
opt := &github.ListOptions{PerPage: 100}
|
||||||
|
|
||||||
|
|
12
task/task.go
12
task/task.go
|
@ -1,6 +1,7 @@
|
||||||
package task
|
package task
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -29,12 +30,15 @@ func saveResponseMeta(token string, res *github.Response) {
|
||||||
db.Queue(func() { tok.Save() })
|
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()
|
duration := time.Since(start).Nanoseconds()
|
||||||
outcome := "succeeded"
|
outcome := "succeeded"
|
||||||
if err := recover(); err != nil {
|
// if err := recover(); err != nil {
|
||||||
outcome = "failed"
|
// log.Println(err)
|
||||||
}
|
// outcome = "failed"
|
||||||
|
// }
|
||||||
|
|
||||||
|
task := fmt.Sprintf(format, args...)
|
||||||
log.Printf("Task %s %s; time: %d (%dms)\n", task, outcome, duration, duration/1000000)
|
log.Printf("Task %s %s; time: %d (%dms)\n", task, outcome, duration, duration/1000000)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func FetchUserInfo(token, login string) (u *db.User, err error) {
|
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)
|
client := newGithubClient(token)
|
||||||
var user *github.User
|
var user *github.User
|
||||||
var resp *github.Response
|
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) {
|
func SyncUserInfo(token, login string) (err error) {
|
||||||
defer report("SyncUserInfo", time.Now())
|
defer report(time.Now(), "SyncUserInfo (%s)", login)
|
||||||
var u *db.User
|
var u *db.User
|
||||||
if u, err = FetchUserInfo(token, login); err == nil {
|
if u, err = FetchUserInfo(token, login); err == nil {
|
||||||
db.Queue(func() { u.Save() })
|
db.Queue(func() { u.Save() })
|
||||||
|
|
Loading…
Reference in New Issue