mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-03-25 09:25:49 +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:"-,"`
|
||||
}
|
||||
|
||||
func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) {
|
||||
key := datastore.NewKey(c, "Account", "", int64(gitHubUserId), nil)
|
||||
func getAccount(c appengine.Context, githubUserId int) (*Account, error) {
|
||||
key := datastore.NewKey(c, "Account", "", int64(githubUserId), nil)
|
||||
account := new(Account)
|
||||
err := datastore.Get(c, key, account)
|
||||
if err != nil {
|
||||
|
|
@ -52,19 +52,18 @@ func initAccount(account *Account) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func getAllAccounts(c appengine.Context, accounts *[]Account) error {
|
||||
func getAllAccountGithubUserIds(c appengine.Context) ([]int, error) {
|
||||
q := datastore.NewQuery("Account")
|
||||
_, err := q.GetAll(c, accounts)
|
||||
var accounts []Account
|
||||
_, err := q.GetAll(c, &accounts)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
for i, _ := range *accounts {
|
||||
err = initAccount(&(*accounts)[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result := make([]int, len(accounts))
|
||||
for i := range accounts {
|
||||
result[i] = accounts[i].GitHubUserId
|
||||
}
|
||||
return nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (account *Account) put(c appengine.Context) error {
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import (
|
|||
"time"
|
||||
|
||||
"appengine"
|
||||
"appengine/delay"
|
||||
"appengine/mail"
|
||||
"appengine/urlfetch"
|
||||
|
||||
|
|
@ -237,12 +238,30 @@ func sendDigestHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func digestCronHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var accounts []Account
|
||||
c := appengine.NewContext(r)
|
||||
getAllAccounts(c, &accounts)
|
||||
for _, account := range accounts {
|
||||
c.Infof("Sending digest for %d...", account.GitHubUserId)
|
||||
sent, err := sendDigestForAccount(&account, c)
|
||||
githubUserIds, err := getAllAccountGithubUserIds(c)
|
||||
if err != nil {
|
||||
c.Errorf("Error looking up accounts: %s", err.Error())
|
||||
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 {
|
||||
c.Errorf(" Error: %s", err.Error())
|
||||
} else if sent {
|
||||
|
|
@ -250,9 +269,8 @@ func digestCronHandler(w http.ResponseWriter, r *http.Request) {
|
|||
} else {
|
||||
c.Infof(" Not sent, digest was empty")
|
||||
}
|
||||
}
|
||||
fmt.Fprint(w, "Done")
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) {
|
||||
oauthTransport := githubOAuthTransport(c)
|
||||
|
|
|
|||
Loading…
Reference in a new issue