mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-04-27 15:07:43 +00:00
Use a task queue for sending of digest emails when using the cron job.
This commit is contained in:
parent
4561d17569
commit
0cc4188964
2 changed files with 36 additions and 19 deletions
|
|
@ -21,8 +21,8 @@ type Account struct {
|
||||||
TimezoneLocation *time.Location `datastore:"-,"`
|
TimezoneLocation *time.Location `datastore:"-,"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) {
|
func getAccount(c appengine.Context, githubUserId int) (*Account, error) {
|
||||||
key := datastore.NewKey(c, "Account", "", int64(gitHubUserId), nil)
|
key := datastore.NewKey(c, "Account", "", int64(githubUserId), nil)
|
||||||
account := new(Account)
|
account := new(Account)
|
||||||
err := datastore.Get(c, key, account)
|
err := datastore.Get(c, key, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -52,19 +52,18 @@ func initAccount(account *Account) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAllAccounts(c appengine.Context, accounts *[]Account) error {
|
func getAllAccountGithubUserIds(c appengine.Context) ([]int, error) {
|
||||||
q := datastore.NewQuery("Account")
|
q := datastore.NewQuery("Account")
|
||||||
_, err := q.GetAll(c, accounts)
|
var accounts []Account
|
||||||
|
_, err := q.GetAll(c, &accounts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
for i, _ := range *accounts {
|
result := make([]int, len(accounts))
|
||||||
err = initAccount(&(*accounts)[i])
|
for i := range accounts {
|
||||||
if err != nil {
|
result[i] = accounts[i].GitHubUserId
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (account *Account) put(c appengine.Context) error {
|
func (account *Account) put(c appengine.Context) error {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"appengine"
|
"appengine"
|
||||||
|
"appengine/delay"
|
||||||
"appengine/mail"
|
"appengine/mail"
|
||||||
"appengine/urlfetch"
|
"appengine/urlfetch"
|
||||||
|
|
||||||
|
|
@ -237,12 +238,30 @@ func sendDigestHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func digestCronHandler(w http.ResponseWriter, r *http.Request) {
|
func digestCronHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var accounts []Account
|
|
||||||
c := appengine.NewContext(r)
|
c := appengine.NewContext(r)
|
||||||
getAllAccounts(c, &accounts)
|
githubUserIds, err := getAllAccountGithubUserIds(c)
|
||||||
for _, account := range accounts {
|
if err != nil {
|
||||||
c.Infof("Sending digest for %d...", account.GitHubUserId)
|
c.Errorf("Error looking up accounts: %s", err.Error())
|
||||||
sent, err := sendDigestForAccount(&account, c)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, githubUserId := range githubUserIds {
|
||||||
|
c.Infof("Enqueing task for %d...", githubUserId)
|
||||||
|
sendDigestForAccountFunc.Call(c, githubUserId)
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, "Done")
|
||||||
|
}
|
||||||
|
|
||||||
|
var sendDigestForAccountFunc = delay.Func(
|
||||||
|
"sendDigestForAccount",
|
||||||
|
func(c appengine.Context, githubUserId int) error {
|
||||||
|
c.Infof("Sending digest for %d...", githubUserId)
|
||||||
|
account, err := getAccount(c, githubUserId)
|
||||||
|
if err != nil {
|
||||||
|
c.Errorf(" Error looking up account: %s", err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sent, err := sendDigestForAccount(account, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Errorf(" Error: %s", err.Error())
|
c.Errorf(" Error: %s", err.Error())
|
||||||
} else if sent {
|
} else if sent {
|
||||||
|
|
@ -250,9 +269,8 @@ func digestCronHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
c.Infof(" Not sent, digest was empty")
|
c.Infof(" Not sent, digest was empty")
|
||||||
}
|
}
|
||||||
}
|
return err
|
||||||
fmt.Fprint(w, "Done")
|
})
|
||||||
}
|
|
||||||
|
|
||||||
func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) {
|
func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) {
|
||||||
oauthTransport := githubOAuthTransport(c)
|
oauthTransport := githubOAuthTransport(c)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue